I have this typical setup for detecting box2d collisions:
`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();
if ([spriteA isKindOfClass:[Rover class]] && [spriteB isKindOfClass:[BMeteor class]]) {
NSLog(@"A-Rover && B-Meteor");
if (std::find(toDestroy.begin(), toDestroy.end(), bodyA) == 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);
}
}
else if ([spriteB isKindOfClass:[BMeteor class]] && [spriteB isKindOfClass:[Rover class]]) {
NSLog(@"A-Meteor && B-Rover");
if (std::find(toDestroy.begin(), toDestroy.end(), bodyB) == toDestroy.end()) {
toDestroy.push_back(bodyA);
toDestroy.push_back(rover.wheelLBody);
toDestroy.push_back(rover.wheelRBody);
}
}
}
}
//9. 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);
//Signal GameOver
gameOver = true;
[uiLayer displayText:@"Watch out for meteors!" andOnCompleteCallTarget:self selector:@selector(gameOver:)];
}
if (toDestroy.size() > 0) {
PLAYSOUNDEFFECT(MINECART_HIT);
}`
Which uses this custom class:
`#import "ContactListener.h"
ContactListener::ContactListener() : _contacts() {
}
ContactListener::~ContactListener() {
}
void ContactListener::BeginContact(b2Contact* contact) {
// We need to copy out the data because the b2Contact passed in
// is reused.
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
}
void ContactListener::EndContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector<MyContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
_contacts.erase(pos);
}
}
void ContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
}
void ContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {}`
But I have this other option I found in RW Learn Cocos2d Book which imports Box2DHelpers:
`if (isBodyCollidingWithObjectType(geyserSensorBody,kCartType)) {
//RUN CODE
NSLog(@"Geyser collided with rover...push rover");
[rover geyserPush];
[rover changeState:kStateTakingDamage];
}`
Here is Box2dHelpers:
`#import "Box2DHelpers.h"
#import "Box2DSprite.h"
bool isBodyCollidingWithObjectType(b2Body *body, GameObjectType
objectType) {
b2ContactEdge* edge = body->GetContactList();
while (edge)
{
b2Contact* contact = edge->contact;
if (contact->IsTouching()) {
b2Fixture* fixtureA = contact->GetFixtureA();
b2Fixture* fixtureB = contact->GetFixtureB();
b2Body *bodyA = fixtureA->GetBody();
b2Body *bodyB = fixtureB->GetBody();
Box2DSprite *spriteA =
(Box2DSprite *) bodyA->GetUserData();
Box2DSprite *spriteB =
(Box2DSprite *) bodyB->GetUserData();
if ((spriteA != NULL &&
spriteA.gameObjectType == objectType) ||
(spriteB != NULL &&
spriteB.gameObjectType == objectType)) {
return true;
} }
edge = edge->next;
}
return false;
}
`
But the second one seems to crash. In other words, when the rover touches a meteor, the game crashes. The rover does have a sensor at the bottom and im not sure if the sensor is causing the crash or the entire rover.
`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();
if ([spriteA isKindOfClass:[Rover class]] && [spriteB isKindOfClass:[BMeteor class]]) {
NSLog(@"A-Rover && B-Meteor");
if (std::find(toDestroy.begin(), toDestroy.end(), bodyA) == 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);
}
}
else if ([spriteB isKindOfClass:[BMeteor class]] && [spriteB isKindOfClass:[Rover class]]) {
NSLog(@"A-Meteor && B-Rover");
if (std::find(toDestroy.begin(), toDestroy.end(), bodyB) == toDestroy.end()) {
toDestroy.push_back(bodyA);
toDestroy.push_back(rover.wheelLBody);
toDestroy.push_back(rover.wheelRBody);
}
}
}
}
//9. 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);
//Signal GameOver
gameOver = true;
[uiLayer displayText:@"Watch out for meteors!" andOnCompleteCallTarget:self selector:@selector(gameOver:)];
}
if (toDestroy.size() > 0) {
PLAYSOUNDEFFECT(MINECART_HIT);
}`
Which uses this custom class:
`#import "ContactListener.h"
ContactListener::ContactListener() : _contacts() {
}
ContactListener::~ContactListener() {
}
void ContactListener::BeginContact(b2Contact* contact) {
// We need to copy out the data because the b2Contact passed in
// is reused.
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
}
void ContactListener::EndContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector<MyContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
_contacts.erase(pos);
}
}
void ContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
}
void ContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {}`
But I have this other option I found in RW Learn Cocos2d Book which imports Box2DHelpers:
`if (isBodyCollidingWithObjectType(geyserSensorBody,kCartType)) {
//RUN CODE
NSLog(@"Geyser collided with rover...push rover");
[rover geyserPush];
[rover changeState:kStateTakingDamage];
}`
Here is Box2dHelpers:
`#import "Box2DHelpers.h"
#import "Box2DSprite.h"
bool isBodyCollidingWithObjectType(b2Body *body, GameObjectType
objectType) {
b2ContactEdge* edge = body->GetContactList();
while (edge)
{
b2Contact* contact = edge->contact;
if (contact->IsTouching()) {
b2Fixture* fixtureA = contact->GetFixtureA();
b2Fixture* fixtureB = contact->GetFixtureB();
b2Body *bodyA = fixtureA->GetBody();
b2Body *bodyB = fixtureB->GetBody();
Box2DSprite *spriteA =
(Box2DSprite *) bodyA->GetUserData();
Box2DSprite *spriteB =
(Box2DSprite *) bodyB->GetUserData();
if ((spriteA != NULL &&
spriteA.gameObjectType == objectType) ||
(spriteB != NULL &&
spriteB.gameObjectType == objectType)) {
return true;
} }
edge = edge->next;
}
return false;
}
`
But the second one seems to crash. In other words, when the rover touches a meteor, the game crashes. The rover does have a sensor at the bottom and im not sure if the sensor is causing the crash or the entire rover.