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
...
}