diff --git a/include/bounce/dynamics/body.h b/include/bounce/dynamics/body.h index 34de0b9..af0c867 100644 --- a/include/bounce/dynamics/body.h +++ b/include/bounce/dynamics/body.h @@ -59,18 +59,47 @@ struct b3BodyDef linearVelocity.SetZero(); angularVelocity.SetZero(); gravityScale = 1.0f; + linearDamping = 0.0f; + angularDamping = 0.0f; } + // b3BodyType type; + + // bool awake; + + // bool fixedRotationX; + + // bool fixedRotationY; + + // bool fixedRotationZ; + + // void* userData; + + // b3Vec3 position; + + // b3Quat orientation; + + // b3Vec3 linearVelocity; + + // b3Vec3 angularVelocity; + + // + float32 linearDamping; + + // + float32 angularDamping; + + // float32 gravityScale; }; @@ -210,10 +239,22 @@ public: // Transform a frame to the world space. b3Transform GetWorldFrame(const b3Transform& localFrame) const; - // Get the gravity scale of the body. One is used by default. + // Get the linear damping of the body. + float32 GetLinearDamping() const; + + // Set the linear damping of the body. Zero is set by default. + void SetLinearDamping(float32 damping); + + // Get the angular damping of the body. + float32 GetAngularDamping() const; + + // Set the angular damping of the body. Zero is set by default. + void SetAngularDamping(float32 damping); + + // Get the gravity scale of the body. float32 GetGravityScale() const; - // Set the gravity scale of the body. + // Set the gravity scale of the body. One is set by default. void SetGravityScale(float32 scale); // See if the body is awake. @@ -310,12 +351,15 @@ private: // Inverse inertia about the body world center of mass. b3Mat33 m_worldInvI; - float32 m_gravityScale; b3Vec3 m_force; b3Vec3 m_torque; b3Vec3 m_linearVelocity; b3Vec3 m_angularVelocity; + float32 m_linearDamping; + float32 m_angularDamping; + float32 m_gravityScale; + // Motion proxy for CCD. b3Sweep m_sweep; @@ -477,6 +521,26 @@ inline void b3Body::SetAwake(bool flag) } } +inline float32 b3Body::GetLinearDamping() const +{ + return m_linearDamping; +} + +inline void b3Body::SetLinearDamping(float32 damping) +{ + m_linearDamping = damping; +} + +inline float32 b3Body::GetAngularDamping() const +{ + return m_angularDamping; +} + +inline void b3Body::SetAngularDamping(float32 damping) +{ + m_angularDamping = damping; +} + inline float32 b3Body::GetGravityScale() const { return m_gravityScale; diff --git a/src/bounce/dynamics/body.cpp b/src/bounce/dynamics/body.cpp index b3c82e9..6c5786e 100644 --- a/src/bounce/dynamics/body.cpp +++ b/src/bounce/dynamics/body.cpp @@ -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; diff --git a/src/bounce/dynamics/island.cpp b/src/bounce/dynamics/island.cpp index 6ec8f8b..da22566 100644 --- a/src/bounce/dynamics/island.cpp +++ b/src/bounce/dynamics/island.cpp @@ -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é 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;