I have 2 bodies colliding. One is created like so in its own class:
Code:
b2BodyDef bodyDefShip;
bodyDefShip.type = b2_dynamicBody;
bodyDefShip.userData = self;
bodyDefShip.position.Set(location.x/PTM_RATIO, location.y/PTM_RATIO);
shipBody = world->CreateBody(&bodyDefShip);
b2PolygonShape shapeShip;
shapeShip.SetAsBox(self.contentSize.width/2/PTM_RATIO, self.contentSize.height/2/PTM_RATIO);
b2FixtureDef fixtureDefShip;
fixtureDefShip.shape = &shapeShip;
fixtureDefShip.density = 0.0;
fixtureDefShip.friction = 0.0;
fixtureDefShip.restitution = 0.0;
shipBody->CreateFixture(&fixtureDefShip);
//for collision detection
fixtureDefShip.filter.categoryBits = 0x1; //(default is 0x1 so this body belongs to a different category)
fixtureDefShip.filter.maskBits = 0xFFFF; //(0xFFFF means it collides with all categories)
the second one is created in the action layer, in an addTarget method:
Code:
target = [WeakAndFastShipTarget target];
//////////////////////////////////BOX2d/////////////////////////////////////////
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.userData = target;
bodyDef.position.Set(winSize.width/PTM_RATIO, winSize.height/2/PTM_RATIO);
cometBody = world->CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox(target.contentSize.width/2/PTM_RATIO, target.contentSize.height/2/PTM_RATIO);
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 0.0;
fixtureDef.friction = 0.0;
fixtureDef.restitution = 0.0;
cometBody->CreateFixture(&fixtureDef);
//for collision detection
fixtureDef.filter.categoryBits = 0x4; //(default is 0x1 so this body belongs to a different category)
fixtureDef.filter.maskBits = 0xFFFF; //(0xFFFF means it collides with all categories)
Then I override the beginContact method in my action layer at the end:
Code:
- (void)beginContact:(b2Contact *)contact {
b2Fixture *fixtureA = contact->GetFixtureA();
b2Fixture *fixtureB = contact->GetFixtureB();
b2Body *bodyA = fixtureA->GetBody();
b2Body *bodyB = fixtureB->GetBody();
GameObject *spriteA = (GameObject *) bodyA->GetUserData();
GameObject *spriteB = (GameObject *) bodyB->GetUserData();
if (!spriteA.visible || !spriteB.visible) return;
b2WorldManifold manifold;
contact->GetWorldManifold(&manifold);
b2Vec2 b2ContactPoint = manifold.points[0];
CGPoint contactPoint = ccp(b2ContactPoint.x * PTM_RATIO, b2ContactPoint.y * PTM_RATIO);
CGSize winSize = [CCDirector sharedDirector].winSize;
//////////////////////////////////////////////////////////////////////////SHIP COLLIDES WITH ENEMYSHIP OR ENEMY
if ((fixtureA->GetFilterData().categoryBits & kCategoryShip && fixtureB->GetFilterData().categoryBits & kCategoryEnemy) ||
(fixtureB->GetFilterData().categoryBits & kCategoryShip && fixtureA->GetFilterData().categoryBits & kCategoryEnemy)) {
NSLog(@"Collision happened");
// Determine enemy ship
GameObject *enemyShip = (GameObject*) spriteA;
if (fixtureB->GetFilterData().categoryBits & kCategoryEnemy) {
enemyShip = spriteB;
}
[[SimpleAudioEngine sharedEngine] playEffect:@"explode.caf" pitch:1.0f pan:0.0f gain:0.25f];
//TAKE A HIT
[_player takeHit];
}
and above the action layer I define the categories like so:
Code:
#define kCategoryShip 0x1
#define kCategoryShipLaser 0x2
#define kCategoryEnemy 0x4
As you can see I have an NSLog inside the beginContact if test. If the collision occurs, which it does, the NSLog should appear in the console but it doesn't. I am not clear as to why the bodies aren't colliding.