Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a object in my game that when the main character hits it, all I want to do is have the object explode and then notify the character object that a hit has occurred.

Would I use a b2Body and set it as a sensor for this or would I achieve this some other way?

I'm using cocos2d by the way.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Yes - it would work nicely. Don't forget that you will need to create a ContactListener for your application in order to be notified of the collision.

Here's an implementation I used from before, to get you started - it might need some tweeks due to being a year old

#import "Box2D.h"
#import "b2Contact.h"
class ContactListener : public b2ContactListener
{
public:
    ContactListener();

    void* userData; /// Use this to store application specific body data.
    void BeginContact(b2Contact* contact); // When we first contact
    void EndContact(b2Contact* contact); // When we end contact
};

#import "ContactListener.h"
#import "Box2D.h"
#import "b2Contact.h"
#import "GameScreen.h"

// Implement contact listener.
ContactListener::ContactListener(){};

void ContactListener::BeginContact(b2Contact* contact)
{
    // Box2d objects that collided
    b2Fixture* fixtureA = contact->GetFixtureA();
    b2Fixture* fixtureB = contact->GetFixtureB();
    // Sprites that collided
    CocosNode* actorA = (CocosNode*) fixtureA->GetBody()->GetUserData();
    CocosNode* actorB = (CocosNode*)  fixtureB->GetBody()->GetUserData();

    // This is only true if for example a sprite touched something in your box2d simulation that was not a sprite such as the ground
    // You may not want to return here, so keep that in mind
    if(actorA == nil || actorB == nil) return;

    // Information about the collision, such as where it hit exactly on each body 
    b2WorldManifold* worldManifold = new b2WorldManifold();
    contact->GetWorldManifold(worldManifold);

    // Maybe you wanna handle it differently but for this example, we're going to simply use our global object (see previous post)
    // To store the gamescene where these bodies exist and tell it they collided
    [[Global instance]._gameScene onActorDidStartContact:actorA against:actorB at:worldManifold];
}

    // Implement contact listener.
void ContactListener::EndContact(b2Contact* contact)
{
    // Box2d objects that collided
    b2Fixture* fixtureA = contact->GetFixtureA();
    b2Fixture* fixtureB = contact->GetFixtureB();
    // Sprites that collided
    CocosNode* actorA = (CocosNode*) fixtureA->GetBody()->GetUserData();
    CocosNode* actorB = (CocosNode*)  fixtureB->GetBody()->GetUserData();

    // This is only true if for example a sprite touched something in your box2d simulation that was not a sprite such as the ground
    // You may not want to return here, so keep that in mind
    if(actorA == nil || actorB == nil) return;

    // Information about the collision, such as where it hit exactly on each body 
    b2WorldManifold* worldManifold = new b2WorldManifold();
    contact->GetWorldManifold(worldManifold);

    // Maybe you wanna handle it differently but for this example, we're going to simply use our global object (see previous post)
    // To store the gamescene where these bodies exist and tell it they collided
    [[Global instance]._gameScene onActorDidEndContact:actorA against:actorB at:worldManifold];
}

-(void) onBunnyDidLandOnCar:(GameObject*)actor
{
     // Nothing to see here, game ended already
    if(self._gameIsOver) return;
    // Cast the actor as the car
    Car* car = (Car*) actor;
    // Check if landing was valid
    if(car._body->GetPosition().y + 3.0f > _junny._body->GetPosition().y) { // Too far below the roof of the car to count as landing on it
        //NSLog(@"onJunnyDidLandOnCar: Too far below");
        return; 

// Do some other stuff
...
}
share|improve this answer
    
There's no point in having a void *userData on your b2ContactListener subclass. Since you can only have one ContactListener at a time, you should be adding the data fields you need, typed properly. –  user744 Jul 25 '11 at 14:42

Yes, that sounds like something a sensor should be used for.

As far as I'm aware, sensors should be used for any objects that don't actually cause a collision response with objects it collides with.

In this case, you don't want the player to actually be moved when it hits the sensor (or the reverse). A sensor is definitely what you want.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.