switch from global to local damping
This commit is contained in:
@ -59,18 +59,47 @@ struct b3BodyDef
|
|||||||
linearVelocity.SetZero();
|
linearVelocity.SetZero();
|
||||||
angularVelocity.SetZero();
|
angularVelocity.SetZero();
|
||||||
gravityScale = 1.0f;
|
gravityScale = 1.0f;
|
||||||
|
linearDamping = 0.0f;
|
||||||
|
angularDamping = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
b3BodyType type;
|
b3BodyType type;
|
||||||
|
|
||||||
|
//
|
||||||
bool awake;
|
bool awake;
|
||||||
|
|
||||||
|
//
|
||||||
bool fixedRotationX;
|
bool fixedRotationX;
|
||||||
|
|
||||||
|
//
|
||||||
bool fixedRotationY;
|
bool fixedRotationY;
|
||||||
|
|
||||||
|
//
|
||||||
bool fixedRotationZ;
|
bool fixedRotationZ;
|
||||||
|
|
||||||
|
//
|
||||||
void* userData;
|
void* userData;
|
||||||
|
|
||||||
|
//
|
||||||
b3Vec3 position;
|
b3Vec3 position;
|
||||||
|
|
||||||
|
//
|
||||||
b3Quat orientation;
|
b3Quat orientation;
|
||||||
|
|
||||||
|
//
|
||||||
b3Vec3 linearVelocity;
|
b3Vec3 linearVelocity;
|
||||||
|
|
||||||
|
//
|
||||||
b3Vec3 angularVelocity;
|
b3Vec3 angularVelocity;
|
||||||
|
|
||||||
|
//
|
||||||
|
float32 linearDamping;
|
||||||
|
|
||||||
|
//
|
||||||
|
float32 angularDamping;
|
||||||
|
|
||||||
|
//
|
||||||
float32 gravityScale;
|
float32 gravityScale;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -210,10 +239,22 @@ public:
|
|||||||
// Transform a frame to the world space.
|
// Transform a frame to the world space.
|
||||||
b3Transform GetWorldFrame(const b3Transform& localFrame) const;
|
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;
|
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);
|
void SetGravityScale(float32 scale);
|
||||||
|
|
||||||
// See if the body is awake.
|
// See if the body is awake.
|
||||||
@ -310,12 +351,15 @@ private:
|
|||||||
// Inverse inertia about the body world center of mass.
|
// Inverse inertia about the body world center of mass.
|
||||||
b3Mat33 m_worldInvI;
|
b3Mat33 m_worldInvI;
|
||||||
|
|
||||||
float32 m_gravityScale;
|
|
||||||
b3Vec3 m_force;
|
b3Vec3 m_force;
|
||||||
b3Vec3 m_torque;
|
b3Vec3 m_torque;
|
||||||
b3Vec3 m_linearVelocity;
|
b3Vec3 m_linearVelocity;
|
||||||
b3Vec3 m_angularVelocity;
|
b3Vec3 m_angularVelocity;
|
||||||
|
|
||||||
|
float32 m_linearDamping;
|
||||||
|
float32 m_angularDamping;
|
||||||
|
float32 m_gravityScale;
|
||||||
|
|
||||||
// Motion proxy for CCD.
|
// Motion proxy for CCD.
|
||||||
b3Sweep m_sweep;
|
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
|
inline float32 b3Body::GetGravityScale() const
|
||||||
{
|
{
|
||||||
return m_gravityScale;
|
return m_gravityScale;
|
||||||
|
@ -79,6 +79,8 @@ b3Body::b3Body(const b3BodyDef& def, b3World* world)
|
|||||||
m_xf.position = m_sweep.worldCenter;
|
m_xf.position = m_sweep.worldCenter;
|
||||||
m_xf.rotation = b3ConvertQuatToMat(m_sweep.orientation);
|
m_xf.rotation = b3ConvertQuatToMat(m_sweep.orientation);
|
||||||
|
|
||||||
|
m_linearDamping = def.linearDamping;
|
||||||
|
m_angularDamping = def.angularDamping;
|
||||||
m_gravityScale = def.gravityScale;
|
m_gravityScale = def.gravityScale;
|
||||||
m_userData = def.userData;
|
m_userData = def.userData;
|
||||||
m_islandID = -1;
|
m_islandID = -1;
|
||||||
|
@ -108,14 +108,6 @@ void b3Island::Solve(const b3Vec3& gravity, float32 dt, u32 velocityIterations,
|
|||||||
{
|
{
|
||||||
float32 h = dt;
|
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
|
// 1. Integrate velocities
|
||||||
for (u32 i = 0; i < m_bodyCount; ++i)
|
for (u32 i = 0; i < m_bodyCount; ++i)
|
||||||
{
|
{
|
||||||
@ -165,9 +157,15 @@ void b3Island::Solve(const b3Vec3& gravity, float32 dt, u32 velocityIterations,
|
|||||||
// Clear torques
|
// Clear torques
|
||||||
b->m_torque.SetZero();
|
b->m_torque.SetZero();
|
||||||
|
|
||||||
// Apply damping
|
// Apply local damping.
|
||||||
v *= d;
|
// ODE: dv/dt + c * v = 0
|
||||||
w *= d;
|
// 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;
|
m_velocities[i].v = v;
|
||||||
|
Reference in New Issue
Block a user