Hi all..!
For some time I've been developing a top down game, and opted for box2d, which worked very well until I encountered the situation below (rebuilt and cleaned it out into a testbed example).
What seems to happen is, a circle or a non-square dynamic body (the player), will collide and slide off a box static body and collide with another box, and bounce back and forth a bit, forever. 🙁 No gravity, no friction, no restitution, no rotation, fixed timestep. The player body's linearvelocity is set (every step) in the example, but it's the same using applylinearimpulse/Applyforce. Altering timestep, iterations etc doesn't seem to help. The effect might seem minimal, but when zoomed in a bit in the engine, and using a camera centered on player it becomes quite apparent. I'd very much like to use box2d, but have been stuck on this for weeks now.
Any help would be greatly appreciated.
For some time I've been developing a top down game, and opted for box2d, which worked very well until I encountered the situation below (rebuilt and cleaned it out into a testbed example).
What seems to happen is, a circle or a non-square dynamic body (the player), will collide and slide off a box static body and collide with another box, and bounce back and forth a bit, forever. 🙁 No gravity, no friction, no restitution, no rotation, fixed timestep. The player body's linearvelocity is set (every step) in the example, but it's the same using applylinearimpulse/Applyforce. Altering timestep, iterations etc doesn't seem to help. The effect might seem minimal, but when zoomed in a bit in the engine, and using a camera centered on player it becomes quite apparent. I'd very much like to use box2d, but have been stuck on this for weeks now.
Any help would be greatly appreciated.
Code:
class DumpShell : public Test
{
public:
DumpShell()
{
b2Vec2 g(0.000000000000000e+000f, 0.000000000000000e+000f);
m_world->SetGravity(g);
b2Body** bodies = (b2Body**)b2Alloc(41 * sizeof(b2Body*));
b2Joint** joints = (b2Joint**)b2Alloc(0 * sizeof(b2Joint*));
{
b2BodyDef bd;
bd.type = b2BodyType(0);
bd.position.Set(1.0f,13);
bd.angle = -2;
bd.linearVelocity.Set(0.000000000000000e+000f, 0.000000000000000e+000f);
bd.angularVelocity = 0.000000000000000e+000f;
bd.linearDamping = 0.000000000000000e+000f;
bd.angularDamping = 0.000000000000000e+000f;
bd.allowSleep = bool(4);
bd.awake = bool(2);
bd.fixedRotation = bool(16);
bd.bullet = bool(0);
bd.active = bool(32);
bd.gravityScale = 1.000000000000000e+000f;
bodies[38] = m_world->CreateBody(&bd);
{
b2FixtureDef fd;
fd.friction = 0.000000000000000e+000f;
fd.restitution = 0;
fd.density = 0.000000000000000e+000f;
fd.isSensor = bool(0);
fd.filter.categoryBits = uint16(1);
fd.filter.maskBits = uint16(65535);
fd.filter.groupIndex = int16(0);
b2PolygonShape shape;
b2Vec2 vs[8];
vs[0].Set(-7.149510383605957e-001f, -7.149510383605957e-001f);
vs[1].Set(7.149510383605957e-001f, -7.149510383605957e-001f);
vs[2].Set(7.149510383605957e-001f, 7.149510383605957e-001f);
vs[3].Set(-7.149510383605957e-001f, 7.149510383605957e-001f);
shape.Set(vs, 4);
fd.shape = &shape;
bodies[38]->CreateFixture(&fd);
}
}
{
b2BodyDef bd;
bd.type = b2BodyType(0);
bd.position.Set(-0.7,12);
bd.angle = 1.444287061691284e+000f;
bd.linearVelocity.Set(0.000000000000000e+000f, 0.000000000000000e+000f);
bd.angularVelocity = 0.000000000000000e+000f;
bd.linearDamping = 0.000000000000000e+000f;
bd.angularDamping = 0.000000000000000e+000f;
bd.allowSleep = bool(4);
bd.awake = bool(2);
bd.fixedRotation = bool(16);
bd.bullet = bool(0);
bd.active = bool(32);
bd.gravityScale = 1.000000000000000e+000f;
bodies[39] = m_world->CreateBody(&bd);
{
b2FixtureDef fd;
fd.friction = 0.000000000000000e+000f;
fd.restitution = 0;
fd.density = 0.000000000000000e+000f;
fd.isSensor = bool(0);
fd.filter.categoryBits = uint16(1);
fd.filter.maskBits = uint16(65535);
fd.filter.groupIndex = int16(0);
b2PolygonShape shape;
b2Vec2 vs[8];
vs[0].Set(-7.149510383605957e-001f, -7.149510383605957e-001f);
vs[1].Set(7.149510383605957e-001f, -7.149510383605957e-001f);
vs[2].Set(7.149510383605957e-001f, 7.149510383605957e-001f);
vs[3].Set(-7.149510383605957e-001f, 7.149510383605957e-001f);
shape.Set(vs, 4);
fd.shape = &shape;
bodies[39]->CreateFixture(&fd);
}
}
{
b2BodyDef bd;
bd.type = b2BodyType(2);
bd.position.Set(-5,20);
bd.angle = 1.571467757225037e+000f;
bd.angularVelocity = 0;
bd.linearDamping = 0.000000000000000e+000f;
bd.angularDamping = 0.000000000000000e+000f;
bd.allowSleep = bool(0);
bd.awake = bool(2);
bd.fixedRotation = bool(0);
bd.bullet = bool(8);
bd.active = bool(32);
bd.gravityScale = 1.000000000000000e+000f;
playerBody = m_world->CreateBody(&bd);
{
b2FixtureDef fd;
fd.friction = 0;
fd.restitution = 0;
fd.density = 1.000000000000000e+000f;
fd.isSensor = bool(0);
fd.filter.categoryBits = uint16(1);
fd.filter.maskBits = uint16(65535);
fd.filter.groupIndex = int16(0);
b2CircleShape shape;
shape.m_radius = 9.350000619888306e-001f;
shape.m_p.Set(0.000000000000000e+000f, 0.000000000000000e+000f);
fd.shape = &shape;
playerBody->CreateFixture(&fd);
}
}
b2Free(joints);
b2Free(bodies);
joints = NULL;
bodies = NULL;
}
void Step(Settings* settings)
{
b2Vec2 pos = playerBody->GetPosition();
m_debugDraw.DrawString(5, 40, "player pos={%4.5f, %4.5f}", pos.x, pos.y);
playerBody->SetLinearVelocity(b2Vec2(6.526862478256226e+000f, -10.791315078735352e+000f));
Test::Step(settings);
}
b2Body *playerBody;
static Test* Create()
{
return new DumpShell;
}
};
{
public:
DumpShell()
{
b2Vec2 g(0.000000000000000e+000f, 0.000000000000000e+000f);
m_world->SetGravity(g);
b2Body** bodies = (b2Body**)b2Alloc(41 * sizeof(b2Body*));
b2Joint** joints = (b2Joint**)b2Alloc(0 * sizeof(b2Joint*));
{
b2BodyDef bd;
bd.type = b2BodyType(0);
bd.position.Set(1.0f,13);
bd.angle = -2;
bd.linearVelocity.Set(0.000000000000000e+000f, 0.000000000000000e+000f);
bd.angularVelocity = 0.000000000000000e+000f;
bd.linearDamping = 0.000000000000000e+000f;
bd.angularDamping = 0.000000000000000e+000f;
bd.allowSleep = bool(4);
bd.awake = bool(2);
bd.fixedRotation = bool(16);
bd.bullet = bool(0);
bd.active = bool(32);
bd.gravityScale = 1.000000000000000e+000f;
bodies[38] = m_world->CreateBody(&bd);
{
b2FixtureDef fd;
fd.friction = 0.000000000000000e+000f;
fd.restitution = 0;
fd.density = 0.000000000000000e+000f;
fd.isSensor = bool(0);
fd.filter.categoryBits = uint16(1);
fd.filter.maskBits = uint16(65535);
fd.filter.groupIndex = int16(0);
b2PolygonShape shape;
b2Vec2 vs[8];
vs[0].Set(-7.149510383605957e-001f, -7.149510383605957e-001f);
vs[1].Set(7.149510383605957e-001f, -7.149510383605957e-001f);
vs[2].Set(7.149510383605957e-001f, 7.149510383605957e-001f);
vs[3].Set(-7.149510383605957e-001f, 7.149510383605957e-001f);
shape.Set(vs, 4);
fd.shape = &shape;
bodies[38]->CreateFixture(&fd);
}
}
{
b2BodyDef bd;
bd.type = b2BodyType(0);
bd.position.Set(-0.7,12);
bd.angle = 1.444287061691284e+000f;
bd.linearVelocity.Set(0.000000000000000e+000f, 0.000000000000000e+000f);
bd.angularVelocity = 0.000000000000000e+000f;
bd.linearDamping = 0.000000000000000e+000f;
bd.angularDamping = 0.000000000000000e+000f;
bd.allowSleep = bool(4);
bd.awake = bool(2);
bd.fixedRotation = bool(16);
bd.bullet = bool(0);
bd.active = bool(32);
bd.gravityScale = 1.000000000000000e+000f;
bodies[39] = m_world->CreateBody(&bd);
{
b2FixtureDef fd;
fd.friction = 0.000000000000000e+000f;
fd.restitution = 0;
fd.density = 0.000000000000000e+000f;
fd.isSensor = bool(0);
fd.filter.categoryBits = uint16(1);
fd.filter.maskBits = uint16(65535);
fd.filter.groupIndex = int16(0);
b2PolygonShape shape;
b2Vec2 vs[8];
vs[0].Set(-7.149510383605957e-001f, -7.149510383605957e-001f);
vs[1].Set(7.149510383605957e-001f, -7.149510383605957e-001f);
vs[2].Set(7.149510383605957e-001f, 7.149510383605957e-001f);
vs[3].Set(-7.149510383605957e-001f, 7.149510383605957e-001f);
shape.Set(vs, 4);
fd.shape = &shape;
bodies[39]->CreateFixture(&fd);
}
}
{
b2BodyDef bd;
bd.type = b2BodyType(2);
bd.position.Set(-5,20);
bd.angle = 1.571467757225037e+000f;
bd.angularVelocity = 0;
bd.linearDamping = 0.000000000000000e+000f;
bd.angularDamping = 0.000000000000000e+000f;
bd.allowSleep = bool(0);
bd.awake = bool(2);
bd.fixedRotation = bool(0);
bd.bullet = bool(8);
bd.active = bool(32);
bd.gravityScale = 1.000000000000000e+000f;
playerBody = m_world->CreateBody(&bd);
{
b2FixtureDef fd;
fd.friction = 0;
fd.restitution = 0;
fd.density = 1.000000000000000e+000f;
fd.isSensor = bool(0);
fd.filter.categoryBits = uint16(1);
fd.filter.maskBits = uint16(65535);
fd.filter.groupIndex = int16(0);
b2CircleShape shape;
shape.m_radius = 9.350000619888306e-001f;
shape.m_p.Set(0.000000000000000e+000f, 0.000000000000000e+000f);
fd.shape = &shape;
playerBody->CreateFixture(&fd);
}
}
b2Free(joints);
b2Free(bodies);
joints = NULL;
bodies = NULL;
}
void Step(Settings* settings)
{
b2Vec2 pos = playerBody->GetPosition();
m_debugDraw.DrawString(5, 40, "player pos={%4.5f, %4.5f}", pos.x, pos.y);
playerBody->SetLinearVelocity(b2Vec2(6.526862478256226e+000f, -10.791315078735352e+000f));
Test::Step(settings);
}
b2Body *playerBody;
static Test* Create()
{
return new DumpShell;
}
};