Gyro tolerance (#191)

This commit is contained in:
Erin Catto 2026-10-15 23:30:56 -07:00 • committed by GitHub
commit efca0c184d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 420 additions and 63 deletions

View file

@ -583,6 +583,107 @@ public:
static int sampleGyroscopicPrecession = RegisterSample( "Bodies", "Gyroscopic Precession", GyroscopicPrecession::Create );
// This show that gyroscopic torque is necessary for plausible behavior on long skinny objects.
class SpinningCapsule : public Sample
{
public:
explicit SpinningCapsule( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 30.0f, 25.0f, 15.0f, { 0.0f, 1.0f, 0.0f } );
}
m_length = 4.0f;
m_tolerance = b3World_GetIsotropicInertiaTolerance( m_worldId );
CreateScene();
}
void CreateScene()
{
// The tolerance only applies to bodies created after it is set
b3Capacity capacity = {};
CreateWorld( &capacity );
b3World_SetIsotropicInertiaTolerance( m_worldId, m_tolerance );
AddGroundBox( 20.0f );
float radius = 0.2f;
float halfLength = 0.5f * m_length;
b3Capsule capsule = { { 0.0f, -halfLength, 0.0f }, { 0.0f, halfLength, 0.0f }, radius };
// Stand on the lower cap, tilted about it
b3Quat rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, 15.0f * B3_PI / 180.0f );
b3Vec3 axis = b3RotateVector( rotation, b3Vec3_axisY );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = b3OffsetPos( { 0.0f, radius, 0.0f }, halfLength * axis );
bodyDef.rotation = rotation;
bodyDef.angularVelocity = 20.0f * axis;
m_capsuleId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateCapsuleShape( m_capsuleId, &shapeDef, &capsule );
}
bool DrawControls() override
{
bool changed = false;
ImGui::PushItemWidth( 10.0f * ImGui::GetFontSize() );
changed |= ImGui::SliderFloat( "Length", &m_length, 0.0f, 8.0f, "%.1f" );
changed |= ImGui::SliderFloat( "Tolerance", &m_tolerance, 0.0f, 1.0f, "%.3f" );
ImGui::PopItemWidth();
if ( ImGui::Button( "Reset" ) || changed )
{
CreateScene();
}
return true;
}
void Step() override
{
Sample::Step();
// Same measure the world uses to skip the gyroscopic solve: the deviatoric part of the inertia
// relative to its mean.
b3Matrix3 inertia = b3Body_GetLocalRotationalInertia( m_capsuleId );
float mean = ( inertia.cx.x + inertia.cy.y + inertia.cz.z ) / 3.0f;
float dx = inertia.cx.x - mean;
float dy = inertia.cy.y - mean;
float dz = inertia.cz.z - mean;
float offDiagonal = inertia.cy.x * inertia.cy.x + inertia.cz.x * inertia.cz.x + inertia.cz.y * inertia.cz.y;
float deviation = dx * dx + dy * dy + dz * dz + 2.0f * offDiagonal;
float anisotropy = mean > 0.0f ? sqrtf( 2.0f * deviation / 3.0f ) / mean : 0.0f;
bool isGyroscopic = anisotropy > m_tolerance;
b3Pos center = b3Body_GetWorldCenter( m_capsuleId );
b3Vec3 axis = b3RotateVector( b3Body_GetRotation( m_capsuleId ), b3Vec3_axisY );
b3Vec3 omega = b3Body_GetAngularVelocity( m_capsuleId );
float tilt = acosf( b3ClampFloat( b3AbsFloat( axis.y ), 0.0f, 1.0f ) );
DrawLine( center, b3OffsetPos( center, 0.2f * omega ), MakeColor( b3_colorYellow ) );
DrawTextLine( "anisotropy %.2f, gyroscopic torque %s", anisotropy, isGyroscopic ? "on" : "off" );
DrawTextLine( "tilt %.0f deg, spin %.1f rad/s", ( 180.0f / B3_PI ) * tilt, b3Dot( omega, axis ) );
}
static Sample* Create( SampleContext* context )
{
return new SpinningCapsule( context );
}
b3BodyId m_capsuleId;
float m_length;
float m_tolerance;
};
static int sampleSpinningCapsule = RegisterSample( "Bodies", "Spinning Capsule", SpinningCapsule::Create );
class Weeble : public Sample
{
public: