Johnathan
Johnathan

Reputation: 827

Box2D collision detection failure

I have recently began using Box2D version 2.1 in combination with Allegro5. Currently, I built a test with a ground and 4 boxes. 3 boxes are stacked, and the other one smashes causing them to flip. During this demonstration, I noticed got two glitches.

One is that creating a box in Box2D "SetAsBox( width, height )", only gives half the size of a normal box drawn to the screen using allegro. Example: In Box2D, I create a box the size of (15, 15). When I come to draw the shape using allegro, I must make an offset of -15 on the y, and scale the shape twice its size.

The other issue is during the collision detection while my boxes rotate due to impact. Most squares hit the ground, but some of them have an offset from the ground of its height making it floating.

Here is the code for making my boxes:

cBox2D::cBox2D( int width, int height ) {

  // Note: In Box2D, 30 pixels = 1 meter
  velocityIterations  = 10;
  positionIterations  = 10;
  worldGravity  = 9.81f;
  timeStep    = ( 1.0f / 60.0f );
  isBodySleep   = false;

  gravity.Set( 0.0f, worldGravity );
  world = new b2World( gravity, isBodySleep );

  groundBodyDef.position.Set( 0.0f, height ); // ground location
  groundBody = world->CreateBody( &groundBodyDef );

  groundBox.SetAsBox( width, 0.0f ); // Ground size
  groundBody->CreateFixture( &groundBox, 0.0f );

 }

 cBox2D::~cBox2D( void ) {}

 void cBox2D::makeSquare( int width, int height, int locX, int locY, float xVelocity, float yVelocity, float angle, float angleVelocity ) {

  sSquare square;

  square.bodyDef.type = b2_dynamicBody;
  square.bodyDef.position.Set( locX, locY ); // Box location
  square.bodyDef.angle = angle; // Box angle
  square.bodyDef.angularVelocity = angleVelocity;
  square.bodyDef.linearVelocity.Set( xVelocity, yVelocity ); // Box Velocity
  square.body = world->CreateBody( &square.bodyDef );

  square.dynamicBox.SetAsBox( width, height ); // Box size
  square.fixtureDef.shape = &square.dynamicBox;
  square.fixtureDef.density = 1.0f;
  square.fixtureDef.friction = 0.3f;
  square.fixtureDef.restitution = 0.0f; // Bouncyness
  square.body->CreateFixture( &square.fixtureDef );

  squareVec.push_back( square );

 }

 int cBox2D::getVecSize( void ) {
  return squareVec.size();
 }

 b2Body* cBox2D::getSquareAt( int loc ) {
  return squareVec.at( loc ).body;
 }

 void cBox2D::update( void ) {

  world->Step(timeStep, velocityIterations, positionIterations);
  world->ClearForces();

 }

Edit: Thank you Chris Burt-Brown for explaining the first issue to me, as for the second issue, It was a good idea, but it did not solve it. I tried both rounding methods you showed me.

Edit: I think I found the answer to my second issue. Turns out that Allegro has a different coordinate system than OpenGL. As a result, instead of doing -gravity, I had to do +gravity which caused Box2D to become unstable and behave weird.

Edit: My bad, I thought it was the issue, but turns out it did not change a thing.

Upvotes: 1

Views: 1490

Answers (1)

Chris Burt-Brown
Chris Burt-Brown

Reputation: 2727

It's actually SetAsBox(halfwidth, halfheight). I know it sounds weird but take a look inside SetAsBox. Passing in the parameters 15 and 15 will give a box with corners (-15,-15) and (15,15) i.e. a box of size 30x30.

I think it's intended as an optimisation, but it's a pretty silly one.

I don't know what's causing your other problem, but when you draw the boxes with Allegro, try seeing if it's fixed when you round the coordinates. (If that doesn't work, try ceil.)

Upvotes: 4

Related Questions