Hi, I'm new to Box2D and am testing it out to do some physical simulation. The library itself is very impressive, however even with a very simple setup - i.e., dropping a circular object from a fixed height - I am noticing quite a bit of energy loss. Several posts suggest that Box2D uses a symplectic integrator (semi-implicit Euler), but even with small (1e-3) timesteps it still loses energy.
I have a small example here which can easily be plugged into the standard TestBed. If you run it you should see from the on-screen debugging output that the energy levels steadily drop while the ball is falling. (No contacts or collisions.) Can somebody please tell me if I am doing anything wrong, or if this is just to be expected?
Thanks! My guess is that there is something about Box2D that I'm not yet understanding.
I have a small example here which can easily be plugged into the standard TestBed. If you run it you should see from the on-screen debugging output that the energy levels steadily drop while the ball is falling. (No contacts or collisions.) Can somebody please tell me if I am doing anything wrong, or if this is just to be expected?
Thanks! My guess is that there is something about Box2D that I'm not yet understanding.
Code:
#ifndef Box2D_EnergyLoss_h
#define Box2D_EnergyLoss_h
// This test shows that a falling object dissipates energy over time :-(
class EnergyLoss
: public Test
{
public:
static Test* Create()
{
return new EnergyLoss;
}
EnergyLoss()
: m_ground(0)
, m_ball(0)
, m_ballRadius(1)
, m_ballOnGround(false)
, m_potentialEnergy0(0)
, m_kineticEnergy0(0)
, m_potentialEnergy(0)
, m_kineticEnergy(0)
{
// ground
b2BodyDef groundBodyDef;
m_ground = m_world->CreateBody(&groundBodyDef);
b2EdgeShape groundShape;
groundShape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
m_ground->CreateFixture(&groundShape, 0.0f);
// ball
b2CircleShape ballShape;
ballShape.m_radius = m_ballRadius;
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(0, 20);
ballBodyDef.angle = 0.0f;
ballBodyDef.allowSleep = false;
m_ball = m_world->CreateBody(&ballBodyDef);
m_ball->CreateFixture(&ballShape, 1.0f);
m_potentialEnergy0 = GetPotentialEnergy(m_ball);
m_kineticEnergy0 = GetKineticEnergy(m_ball);
}
float GetPotentialEnergy(b2Body* body)
{
float m = body->GetMass();
float g = m_world->GetGravity().Length();
b2Vec2 p = body->GetPosition();
float h = p.y - m_ballRadius;
float E = m*g*h;
return E;
}
float GetKineticEnergy(b2Body* body)
{
float m = body->GetMass();
b2Vec2 v = body->GetLinearVelocityFromLocalPoint(body->GetLocalCenter());
float w = body->GetAngularVelocity();
float I = body->GetInertia();
float E = 0.5*m*v.LengthSquared() + 0.5*I*w*w;
return E;
}
void Step(Settings* settings)
{
Test::Step(settings);
// calculate energy if ball still in air
if (!m_ballOnGround)
{
m_potentialEnergy = GetPotentialEnergy(m_ball);
m_kineticEnergy = GetKineticEnergy(m_ball);
}
m_textLine += 15;
m_debugDraw.DrawString(5, m_textLine, "Ball height = %4.4f", m_ball->GetPosition().y - m_ballRadius);
m_textLine += 15;
m_debugDraw.DrawString(5, m_textLine, "Potential Energy = %4.4f", m_potentialEnergy);
m_textLine += 15;
m_debugDraw.DrawString(5, m_textLine, "Kinetic Energy = %4.4f", m_kineticEnergy);
m_textLine += 15;
float total0 = m_potentialEnergy0 + m_kineticEnergy0;
float total = m_potentialEnergy + m_kineticEnergy;
m_debugDraw.DrawString(5, m_textLine, "Total Energy (cur / orig) = %4.4f / %4.4f", total, total0);
m_textLine += 15;
float delta = 100*(total - total0) / total0;
m_debugDraw.DrawString(5, m_textLine, "Energy %% Delta = %4.4f", delta);
}
virtual void BeginContact(b2Contact* contact)
{
m_ballOnGround = true;
}
private:
b2Body* m_ground;
b2Body* m_ball;
float m_ballRadius;
bool m_ballOnGround;
float m_potentialEnergy0;
float m_kineticEnergy0;
float m_potentialEnergy;
float m_kineticEnergy;
};
#endif
#define Box2D_EnergyLoss_h
// This test shows that a falling object dissipates energy over time :-(
class EnergyLoss
: public Test
{
public:
static Test* Create()
{
return new EnergyLoss;
}
EnergyLoss()
: m_ground(0)
, m_ball(0)
, m_ballRadius(1)
, m_ballOnGround(false)
, m_potentialEnergy0(0)
, m_kineticEnergy0(0)
, m_potentialEnergy(0)
, m_kineticEnergy(0)
{
// ground
b2BodyDef groundBodyDef;
m_ground = m_world->CreateBody(&groundBodyDef);
b2EdgeShape groundShape;
groundShape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
m_ground->CreateFixture(&groundShape, 0.0f);
// ball
b2CircleShape ballShape;
ballShape.m_radius = m_ballRadius;
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(0, 20);
ballBodyDef.angle = 0.0f;
ballBodyDef.allowSleep = false;
m_ball = m_world->CreateBody(&ballBodyDef);
m_ball->CreateFixture(&ballShape, 1.0f);
m_potentialEnergy0 = GetPotentialEnergy(m_ball);
m_kineticEnergy0 = GetKineticEnergy(m_ball);
}
float GetPotentialEnergy(b2Body* body)
{
float m = body->GetMass();
float g = m_world->GetGravity().Length();
b2Vec2 p = body->GetPosition();
float h = p.y - m_ballRadius;
float E = m*g*h;
return E;
}
float GetKineticEnergy(b2Body* body)
{
float m = body->GetMass();
b2Vec2 v = body->GetLinearVelocityFromLocalPoint(body->GetLocalCenter());
float w = body->GetAngularVelocity();
float I = body->GetInertia();
float E = 0.5*m*v.LengthSquared() + 0.5*I*w*w;
return E;
}
void Step(Settings* settings)
{
Test::Step(settings);
// calculate energy if ball still in air
if (!m_ballOnGround)
{
m_potentialEnergy = GetPotentialEnergy(m_ball);
m_kineticEnergy = GetKineticEnergy(m_ball);
}
m_textLine += 15;
m_debugDraw.DrawString(5, m_textLine, "Ball height = %4.4f", m_ball->GetPosition().y - m_ballRadius);
m_textLine += 15;
m_debugDraw.DrawString(5, m_textLine, "Potential Energy = %4.4f", m_potentialEnergy);
m_textLine += 15;
m_debugDraw.DrawString(5, m_textLine, "Kinetic Energy = %4.4f", m_kineticEnergy);
m_textLine += 15;
float total0 = m_potentialEnergy0 + m_kineticEnergy0;
float total = m_potentialEnergy + m_kineticEnergy;
m_debugDraw.DrawString(5, m_textLine, "Total Energy (cur / orig) = %4.4f / %4.4f", total, total0);
m_textLine += 15;
float delta = 100*(total - total0) / total0;
m_debugDraw.DrawString(5, m_textLine, "Energy %% Delta = %4.4f", delta);
}
virtual void BeginContact(b2Contact* contact)
{
m_ballOnGround = true;
}
private:
b2Body* m_ground;
b2Body* m_ball;
float m_ballRadius;
bool m_ballOnGround;
float m_potentialEnergy0;
float m_kineticEnergy0;
float m_potentialEnergy;
float m_kineticEnergy;
};
#endif