I have a rectangle in my player class with the following properties
b2BodyDef player; player.type = b2_dynamicBody; player.position.Set(x + width, 10.17f - (y + height)); m_PhysicsBody = level->GetPhysicsWorld().CreateBody(&player); b2PolygonShape shape; shape.SetAsBox(width, height); b2FixtureDef fixture; fixture.shape = &shape; fixture.density = 1.0f; fixture.friction = 1.0f; m_PhysicsFixture = m_PhysicsBody->CreateFixture(&fixture); m_PhysicsFixture->SetUserData(this);
and another rectangle in my level class with the following properties
b2BodyDef ground; ground.position.Set(x + width, 10.17 - (y + height)); b2PolygonShape groundBox; groundBox.SetAsBox(width, height); b2Body* m_Body = m_PhysicsWorld.CreateBody(&ground); b2FixtureDef fixture; fixture.shape = &groundBox; fixture.friction = 1.0f; m_PhysicsFixture = m_Body->CreateFixture(&fixture); m_PhysicsFixture->GetShape()->ComputeAABB(&aabb, m_Body->GetTransform(), 0);
The problem is that unless the player object is a perfect square (the width and the height are exactly the same) it won't sit exactly on top of the ground object I created in the level class. It intersects it a little bit before coming to a rest. I've attached a picture so you guys can see.
The way that I render the shapes is as follows
b2AABB aabb; m_PhysicsFixture->GetShape()->ComputeAABB(&aabb, m_PhysicsBody->GetTransform(), 0); DrawRectangle(aabb.lowerBound.x * 100.0f, (m_Window.GetHeight() - (aabb.lowerBound.y * 100.0f)), (aabb.upperBound.x - aabb.lowerBound.x) * 100.0f, (aabb.upperBound.y - aabb.lowerBound.y) * 100.0f);
I multiply the values by 100.0f to make it bigger, and y is the height of the window minus y. This is because I'm using SDL, which sets the upper-left corner as the 0,0 origin. So all the way at the very top, y = 0, and all the way at the bottom, y = the window height.