Hi,
i am building a small game using box2D, but i am having trouble getting the bodies behave correctly.
The restitution is not handled at all - it always removes the impulse along the normal when it collides :-( What i want is a ball bouncing around a fixed static area - without losing any energy when colliding. So what is wrong with my definitions?
- I am using the latest Box2D from the github master branch -> Source is fully compiled in always.
- My world is 20 meters in width and 11.25 in height. w = half width, h = half height.
This i what i have (On initialization):
b2World *world;
b2Body *body;
state.world = world = new b2World(b2Vec2(0, 0));
//
// Field
//
b2BodyDef fieldDef = b2BodyDef();
fieldDef.type = b2BodyType::b2_staticBody;
fieldDef.position = b2Vec2(0, 0);
fieldDef.angle = 0;
fieldDef.fixedRotation = true;
fieldDef.linearDamping = 0;
fieldDef.angularDamping = 0;
state.field = body = world->CreateBody(&fieldDef);
b2Vec2 fieldVertices[4] = {
b2Vec2(w, h),
b2Vec2(-w, h),
b2Vec2(-w, -h),
b2Vec2(w, -h),
};
b2ChainShape fieldShape = b2ChainShape();
fieldShape.CreateLoop(fieldVertices, 4);
b2FixtureDef fieldFixtureDef = b2FixtureDef();
fieldFixtureDef.shape = &fieldShape;
fieldFixtureDef.restitution = 1;
fieldFixtureDef.friction = 0;
body->CreateFixture(&fieldFixtureDef);
//
// Ball
//
b2BodyDef ballDef = b2BodyDef();
ballDef.type = b2BodyType::b2_dynamicBody;
ballDef.allowSleep = false;
ballDef.bullet = true;
ballDef.position = b2Vec2(0, 0);
ballDef.angle = 0;
ballDef.fixedRotation = true;
ballDef.linearDamping = 0;
ballDef.angularDamping = 0;
state.ball = body = world->CreateBody(&ballDef);
b2CircleShape ballShape = b2CircleShape();
ballShape.m_radius = ballRadius;
ballShape.m_p = b2Vec2(0, 0);
b2FixtureDef ballFixtureDef = b2FixtureDef();
ballFixtureDef.shape = &ballShape;
ballFixtureDef.restitution = 1.0f;
ballFixtureDef.friction = 0.0f;
ballFixtureDef.density = 1.0f;
body->CreateFixture(&ballFixtureDef);
body->ApplyLinearImpulse(b2Vec2(0.25f, 0), state.ball->GetPosition(), true);
On updating:
state.world->Step(input.deltaTime, 10, 10);