Hi! I'm making my first iPhone game with Box2D engine.
I have some dynamic bodies that I want to remove from the screen after colliding with a sensor.
I have used Ray Wenderlich' tutorial for creating it.But every time a body collides with the sensor, the
program crashes.
My code:
And here is my update method :
The program fails at
Looks like m_type is NULL.
I can't find the problem.
I have some dynamic bodies that I want to remove from the screen after colliding with a sensor.
I have used Ray Wenderlich' tutorial for creating it.But every time a body collides with the sensor, the
program crashes.
My code:
Code:
//contact listener .h
struct ChContact {
b2Fixture *fixtureA;
b2Fixture *fixtureB;
bool operator==(const ChContact& other) const
{
return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
}
};
class ChContactListener : public b2ContactListener {
public:
std::vector<ChContact>_contacts;
ChContactListener();
~ChContactListener();
virtual void BeginContact(b2Contact* contact);
virtual void EndContact(b2Contact* contact);
virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
};
struct ChContact {
b2Fixture *fixtureA;
b2Fixture *fixtureB;
bool operator==(const ChContact& other) const
{
return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
}
};
class ChContactListener : public b2ContactListener {
public:
std::vector<ChContact>_contacts;
ChContactListener();
~ChContactListener();
virtual void BeginContact(b2Contact* contact);
virtual void EndContact(b2Contact* contact);
virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
};
Code:
// .mm
#import "ChContactListener.h"
ChContactListener::ChContactListener() : _contacts() {
}
ChContactListener::~ChContactListener() {
}
void ChContactListener::BeginContact(b2Contact* contact) {
// We need to copy out the data because the b2Contact passed in
// is reused.
ChContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
}
void ChContactListener::EndContact(b2Contact* contact) {
ChContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector<ChContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
_contacts.erase(pos);
}
}
void ChContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
}
void ChContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
}
#import "ChContactListener.h"
ChContactListener::ChContactListener() : _contacts() {
}
ChContactListener::~ChContactListener() {
}
void ChContactListener::BeginContact(b2Contact* contact) {
// We need to copy out the data because the b2Contact passed in
// is reused.
ChContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
}
void ChContactListener::EndContact(b2Contact* contact) {
ChContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector<ChContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
_contacts.erase(pos);
}
}
void ChContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
}
void ChContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
}
And here is my update method :
Code:
-(void)update:(ccTime)dt {
world->Step(dt, /*velocityIterations*/3, /*positionIterations*/2);
for(b2Body *bdy = (world->GetBodyList()); bdy != NULL; bdy = bdy->GetNext())
{
if (bdy->GetType()!=b2_staticBody && bdy->GetUserData() != NULL) {
Box2DSprite *sp = (Box2DSprite *) bdy->GetUserData();
sp.position = ccp(bdy->GetPosition().x * PTM_RATIO*RETSIZE,bdy->GetPosition().y * PTM_RATIO*RETSIZE);
sp.rotation = CC_RADIANS_TO_DEGREES(bdy->GetAngle() * -1);
}
}
//Collision detection
std::vector<b2Body *>toDestroy;
std::vector<ChContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();pos != _contactListener->_contacts.end(); ++pos) {
ChContact contact = *pos;
if (contact.fixtureB->IsSensor() && !contact.fixtureA->IsSensor()) {
toDestroy.push_back(contact.fixtureA->GetBody());
NSLog(@"bodyA passed");
}
if (contact.fixtureA->IsSensor() && !contact.fixtureB->IsSensor()) {
toDestroy.push_back(contact.fixtureB->GetBody());
NSLog(@"bodyB passed");
}
}
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) body->GetUserData();
[self removeChild:sprite cleanup:YES];
NSLog(@"sprite removed");
}
NSLog(@"bodyToDestroy = %p",body);
body->SetAwake(false);
world->DestroyBody(body); //HERE FAILS EVERY TIME
}
}
world->Step(dt, /*velocityIterations*/3, /*positionIterations*/2);
for(b2Body *bdy = (world->GetBodyList()); bdy != NULL; bdy = bdy->GetNext())
{
if (bdy->GetType()!=b2_staticBody && bdy->GetUserData() != NULL) {
Box2DSprite *sp = (Box2DSprite *) bdy->GetUserData();
sp.position = ccp(bdy->GetPosition().x * PTM_RATIO*RETSIZE,bdy->GetPosition().y * PTM_RATIO*RETSIZE);
sp.rotation = CC_RADIANS_TO_DEGREES(bdy->GetAngle() * -1);
}
}
//Collision detection
std::vector<b2Body *>toDestroy;
std::vector<ChContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();pos != _contactListener->_contacts.end(); ++pos) {
ChContact contact = *pos;
if (contact.fixtureB->IsSensor() && !contact.fixtureA->IsSensor()) {
toDestroy.push_back(contact.fixtureA->GetBody());
NSLog(@"bodyA passed");
}
if (contact.fixtureA->IsSensor() && !contact.fixtureB->IsSensor()) {
toDestroy.push_back(contact.fixtureB->GetBody());
NSLog(@"bodyB passed");
}
}
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) body->GetUserData();
[self removeChild:sprite cleanup:YES];
NSLog(@"sprite removed");
}
NSLog(@"bodyToDestroy = %p",body);
body->SetAwake(false);
world->DestroyBody(body); //HERE FAILS EVERY TIME
}
}
The program fails at
Code:
b2Shape::GetType const
function with EXC_BAD_ACCESS(code=2,0x...).Looks like m_type is NULL.
I can't find the problem.