I have this collision code so far:
Code:
//Rover-Meteor Collision
std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
// Sprite A = ball, Sprite B = Block
if ([spriteA isKindOfClass:[Rover class]]) {
NSLog(@"A-Rover && B-Meteor");
if (std::find(toDestroy.begin(), toDestroy.end(), bodyB) == toDestroy.end()) {
//DONT DESTROY body, push it into an array? and it will be cleaned up later
toDestroy.push_back(bodyA);
toDestroy.push_back(rover.wheelLBody);
toDestroy.push_back(rover.wheelRBody);
}
}
// Sprite B = block, Sprite A = ball
else if ([spriteB isKindOfClass:[BMeteor class]]) {
NSLog(@"A-Meteor && B-Rover");
if (std::find(toDestroy.begin(), toDestroy.end(), bodyA) == toDestroy.end()) {
toDestroy.push_back(bodyA);
}
}
}
}
// Clean up array of bodies to destroy?
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL) {
NSLog(@"is vector iterator being called....YES");
CCSprite *sprite = (CCSprite *) body->GetUserData();
[sceneSpriteBatchNode removeChild:sprite cleanup:YES];
}
isRoverAlive = FALSE;
world->DestroyBody(body);
}
if (toDestroy.size() > 0) {
PLAYSOUNDEFFECT(CAR_HIT);
}
It works fine. However, Im thinking of adding another collision detection test, for the collision between meteor & ground because I would like to make the meteor explode when it hits the ground.
Do I simply add a new if/else test inside this code? Or do I need to create a new code block? I don't understand all the non-ObjC code so Im not to clear on the method or function names and their structure in the code block above.