switch from global to local damping

This commit is contained in:
Irlan
2017-04-12 10:54:28 -03:00
parent 7a438d0fba
commit e0d2580fa1
3 changed files with 78 additions and 14 deletions

View File

@ -79,6 +79,8 @@ b3Body::b3Body(const b3BodyDef& def, b3World* world)
m_xf.position = m_sweep.worldCenter;
m_xf.rotation = b3ConvertQuatToMat(m_sweep.orientation);
m_linearDamping = def.linearDamping;
m_angularDamping = def.angularDamping;
m_gravityScale = def.gravityScale;
m_userData = def.userData;
m_islandID = -1;

View File

@ -108,14 +108,6 @@ void b3Island::Solve(const b3Vec3& gravity, float32 dt, u32 velocityIterations,
{
float32 h = dt;
// Apply some damping.
// ODE: dv/dt + c * v = 0
// Solution: v(t) = v0 * exp(-c * t)
// Step: v(t + dt) = v0 * exp(-c * (t + dt)) = v0 * exp(-c * t) * exp(-c * dt) = v * exp(-c * dt)
// v2 = exp(-c * dt) * v1
const float32 k_d = 0.005f;
float32 d = exp(-k_d * h);
// 1. Integrate velocities
for (u32 i = 0; i < m_bodyCount; ++i)
{
@ -165,9 +157,15 @@ void b3Island::Solve(const b3Vec3& gravity, float32 dt, u32 velocityIterations,
// Clear torques
b->m_torque.SetZero();
// Apply damping
v *= d;
w *= d;
// Apply local damping.
// ODE: dv/dt + c * v = 0
// Solution: v(t) = v0 * exp(-c * t)
// Step: v(t + dt) = v0 * exp(-c * (t + dt)) = v0 * exp(-c * t) * exp(-c * dt) = v * exp(-c * dt)
// v2 = exp(-c * dt) * v1
// Pad<61> approximation:
// 1 / (1 + c * dt)
v *= 1.0f / (1.0f + h * b->m_linearDamping);
w *= 1.0f / (1.0f + h * b->m_angularDamping);
}
m_velocities[i].v = v;