It would be nice if you could set the linear/angular sleep threshold values on a per-body basis. I have an application that is using some ragdoll physics and it is next to impossible to get the ragdolls to sleep without increasing the sleep threshold values by a factor of 5. Given that I don't want to affect the rest of the simulation I will probably end up implementing this functionality myself in b2Island.cpp:
Are there any unforeseen issues I might run into doing this?
Code:
void b2Island::Solve(const b2TimeStep& step, const b2Vec2& gravity, bool allowSleep)
{
...
if (allowSleep)
{
float32 minSleepTime = b2_maxFloat;
for (int32 i = 0; i < m_bodyCount; ++i)
{
b2Body* b = m_bodies[i];
if (b->GetType() == b2_staticBody)
{
continue;
}
if ((b->m_flags & b2Body::e_autoSleepFlag) == 0)
{
b->m_sleepTime = 0.0f;
minSleepTime = 0.0f;
}
// cached as a squared value on b2Body to avoid multiplications every step.
const float32 linTolSqr = b->GetLinearSleepToleranceSquared();
const float32 angTolSqr = b->GetAngularSleepToleranceSquared();
if ((b->m_flags & b2Body::e_autoSleepFlag) == 0 ||
b->m_angularVelocity * b->m_angularVelocity > angTolSqr ||
b2Dot(b->m_linearVelocity, b->m_linearVelocity) > linTolSqr)
{
b->m_sleepTime = 0.0f;
minSleepTime = 0.0f;
}
else
{
b->m_sleepTime += step.dt;
minSleepTime = b2Min(minSleepTime, b->m_sleepTime);
}
}
{
...
if (allowSleep)
{
float32 minSleepTime = b2_maxFloat;
for (int32 i = 0; i < m_bodyCount; ++i)
{
b2Body* b = m_bodies[i];
if (b->GetType() == b2_staticBody)
{
continue;
}
if ((b->m_flags & b2Body::e_autoSleepFlag) == 0)
{
b->m_sleepTime = 0.0f;
minSleepTime = 0.0f;
}
// cached as a squared value on b2Body to avoid multiplications every step.
const float32 linTolSqr = b->GetLinearSleepToleranceSquared();
const float32 angTolSqr = b->GetAngularSleepToleranceSquared();
if ((b->m_flags & b2Body::e_autoSleepFlag) == 0 ||
b->m_angularVelocity * b->m_angularVelocity > angTolSqr ||
b2Dot(b->m_linearVelocity, b->m_linearVelocity) > linTolSqr)
{
b->m_sleepTime = 0.0f;
minSleepTime = 0.0f;
}
else
{
b->m_sleepTime += step.dt;
minSleepTime = b2Min(minSleepTime, b->m_sleepTime);
}
}
Are there any unforeseen issues I might run into doing this?