Hello,
I'm trying to handle collision between two bodies, however the "boxes" of the bodies just overlap with each other, they are not "stopping" as soon as they detect collision.
Picture of the "boxes" overlapping instead of stopping as soon as they collide:

This are my "valuable" ( I think ) parameters.
World declaration:
Here's the code I use to create a GameEntity Body + it fixtures, it get's a GameEntity, and assign a box 1/3 ( .33) of the height and width of the GameEntity
Finally this is the code that I use to detect when a collision happens:
1) Sync bodies with my Entities:
2) Iterate and "handle" the collisions:
I have to say that in the last part //Handle the collision between A and B, if I print in console "A and B" are colliding it works perfectly.
Any help is appreciated.
I'm trying to handle collision between two bodies, however the "boxes" of the bodies just overlap with each other, they are not "stopping" as soon as they detect collision.
Picture of the "boxes" overlapping instead of stopping as soon as they collide:

This are my "valuable" ( I think ) parameters.
World declaration:
Code:
void GandoBox2D::initBaseWorld()
{
//Init a World with no gravity and with no sleeping entities
//this setup is the ideal one if we want to control our entities
//directly from our own engine.
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
bool doSleep = false;
world = new b2World(gravity, doSleep);
//Initialize the contact listener for this world.
contactListener = new GContactListener();
world->SetContactListener(contactListener);
}
{
//Init a World with no gravity and with no sleeping entities
//this setup is the ideal one if we want to control our entities
//directly from our own engine.
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
bool doSleep = false;
world = new b2World(gravity, doSleep);
//Initialize the contact listener for this world.
contactListener = new GContactListener();
world->SetContactListener(contactListener);
}
Here's the code I use to create a GameEntity Body + it fixtures, it get's a GameEntity, and assign a box 1/3 ( .33) of the height and width of the GameEntity
Code:
#define PTM_RATIO 32 //Pixels to Meters ratio.
void gecBoxCollision::setOwnerGE(GameEntity *ge)
{
if(ge == NULL)
{
std::cout << "ERROR: Specified OwnerGameEntity can't be NULL" << std::endl;
assert(ge != NULL);
}
//Assign our ownerGE
ownerGE = ge;
//Define our Box2d body
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(ownerGE->x/PTM_RATIO, ownerGE->y/PTM_RATIO);
spriteBodyDef.userData = ownerGE;
entityBody = GBOX_2D_WORLD->CreateBody(&spriteBodyDef);
//Define our Box2d Shape
b2PolygonShape entityShape;
entityShape.SetAsBox((ownerGE->width/PTM_RATIO)*0.33f, (ownerGE->height/PTM_RATIO)*0.33f);
//Make our fixture definition.
b2FixtureDef entityShapeDef;
entityShapeDef.shape = &entityShape;
entityShapeDef.density = 1.0f;
entityShapeDef.isSensor = true;
//Finally asociate the spriteShapeDef to our entityBody.
entityBody->CreateFixture(&entityShapeDef);
}
void gecBoxCollision::setOwnerGE(GameEntity *ge)
{
if(ge == NULL)
{
std::cout << "ERROR: Specified OwnerGameEntity can't be NULL" << std::endl;
assert(ge != NULL);
}
//Assign our ownerGE
ownerGE = ge;
//Define our Box2d body
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(ownerGE->x/PTM_RATIO, ownerGE->y/PTM_RATIO);
spriteBodyDef.userData = ownerGE;
entityBody = GBOX_2D_WORLD->CreateBody(&spriteBodyDef);
//Define our Box2d Shape
b2PolygonShape entityShape;
entityShape.SetAsBox((ownerGE->width/PTM_RATIO)*0.33f, (ownerGE->height/PTM_RATIO)*0.33f);
//Make our fixture definition.
b2FixtureDef entityShapeDef;
entityShapeDef.shape = &entityShape;
entityShapeDef.density = 1.0f;
entityShapeDef.isSensor = true;
//Finally asociate the spriteShapeDef to our entityBody.
entityBody->CreateFixture(&entityShapeDef);
}
Finally this is the code that I use to detect when a collision happens:
1) Sync bodies with my Entities:
Code:
//Iterate over the bodies in the physics world and apply transformations
//To keep in Sync with our Entity World
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the Sprites position and rotation with the corresponding body
GameEntity* ge = static_cast<GameEntity*>(b->GetUserData());
b2Vec2 b2Position = b2Vec2(ge->x/PTM_RATIO, ge->y/PTM_RATIO);
float32 b2Angle = 0.0f;
b->SetTransform(b2Position, b2Angle);
}
}
//To keep in Sync with our Entity World
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the Sprites position and rotation with the corresponding body
GameEntity* ge = static_cast<GameEntity*>(b->GetUserData());
b2Vec2 b2Position = b2Vec2(ge->x/PTM_RATIO, ge->y/PTM_RATIO);
float32 b2Angle = 0.0f;
b->SetTransform(b2Position, b2Angle);
}
}
2) Iterate and "handle" the collisions:
Code:
const vector<GContact> *contacts = contactListener->getContacts(); //Get's me a reference to the contact vector
vector<GContact>::const_iterator it;
for(it = contacts->begin(); it != contacts->end(); ++it)
{
GContact contact = *it;
//Check contact between two bodies.
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
//If we have an entity in each of the bodies
if(bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
{
GameEntity *geA = (GameEntity *)(bodyA->GetUserData());
GameEntity *geB = (GameEntity *)(bodyB->GetUserData());
//Handle the collision between Entity A and B here.
}
}
vector<GContact>::const_iterator it;
for(it = contacts->begin(); it != contacts->end(); ++it)
{
GContact contact = *it;
//Check contact between two bodies.
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
//If we have an entity in each of the bodies
if(bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
{
GameEntity *geA = (GameEntity *)(bodyA->GetUserData());
GameEntity *geB = (GameEntity *)(bodyB->GetUserData());
//Handle the collision between Entity A and B here.
}
}
I have to say that in the last part //Handle the collision between A and B, if I print in console "A and B" are colliding it works perfectly.
Any help is appreciated.