Thursday, 5 September 2013

detecting contact end in box2d cocos2d

detecting contact end in box2d cocos2d

I've been using ray wenderlich's contact listener and it's been working
well for detecting contacts but I don't understand how to modify it to
also tell me when a contact ends.
I want to add a sensor to my enemies so they can detect when a floor ends
and turn around instead of falling .
can anyone explain how to modify this listener or point me to a different
listener than detect contact ends?
//MyContactListener.mm
MyContactListener::MyContactListener() : _contacts() {
}
MyContactListener::~MyContactListener() {
}
void MyContactListener::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 MyContactListener::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 MyContactListener::PreSolve(b2Contact* contact, const b2Manifold*
oldManifold) {
}
void MyContactListener::PostSolve(b2Contact* contact, const
b2ContactImpulse* impulse) {
}
For detecting contacts I use the following..
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
//...do stuff
}

No comments:

Post a Comment