- Added b3BodyDef::safetyFactor for CCD tuning
- #155
- #153
- #148 
- #149
This commit is contained in:
Erin Catto 2026-09-16 15:46:31 -07:00 • committed by GitHub
commit 954cf879e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 1944 additions and 318 deletions

View file

@ -1027,3 +1027,118 @@ public:
};
static int sampleStall = RegisterSample( "Continuous", "Stall", Stall::Create );
// This shows how adjusting the CCD safety factor can engage continuous collision at
// lower speeds to avoid the slight overlap that happens with discrete collision detection.
// The best way to run this sample is:
// 1. press pause (P)
// 2. restart (R) or press the Drop button
// 3. then single step (O)
// Then look at the metrics and overlap. The shape is drawn orange while it is using
// continuous collision detection.
class SafetyFactor : public Sample
{
public:
explicit SafetyFactor( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 0.0f, 3.0f, 8.0f, { 0.0f, 1.0f, 0.0f } );
}
AddGroundBox( 20.0f );
m_bodyId = b3_nullBodyId;
m_extent = 0.5f;
m_height = 0.3f;
m_safetyFactor = 0.1f;
m_overlap = 0.0f;
Launch();
}
void Launch()
{
if ( B3_IS_NON_NULL( m_bodyId ) )
{
b3DestroyBody( m_bodyId );
}
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = { 0.0f, m_height + m_extent, 0.0f };
bodyDef.safetyFactor = m_safetyFactor;
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3BoxHull box = b3MakeBoxHull( m_extent, m_extent, m_extent );
b3CreateHullShape( m_bodyId, &shapeDef, &box.base );
m_overlap = 0.0f;
}
void Step() override
{
Sample::Step();
if ( m_didStep )
{
// Contacts are computed at the beginning of the step, so the manifold holds the initial separation.
b3ContactData data;
int count = b3Body_GetContactData( m_bodyId, &data, 1 );
if ( count == 1 )
{
for ( int i = 0; i < data.manifoldCount; ++i )
{
const b3Manifold* manifold = data.manifolds + i;
for ( int j = 0; j < manifold->pointCount; ++j )
{
m_overlap = b3MaxFloat( m_overlap, -manifold->points[j].separation );
}
}
}
}
// The overlap line sits below the ground surface, so dash it where the ground hides it
DrawLineEx( { -3.0f, -m_overlap, 0.0f }, { 3.0f, -m_overlap, 0.0f }, MakeColor( b3_colorRed ),
DEFAULT_LINE_THICKNESS_PX, OVERLAY_THICKNESS_PIXELS, OVERLAY_OCCLUSION_DASHED );
float timeStep = m_context->hertz > 0.0f ? 1.0f / m_context->hertz : 0.0f;
float motion = timeStep * b3Length( b3Body_GetLinearVelocity( m_bodyId ) );
DrawTextLine( "actual movement = %.3f m", motion );
DrawTextLine( "fast movement = %.3f m", m_safetyFactor * m_extent );
DrawTextLine( "overlap = %.4f m", m_overlap );
}
bool DrawControls() override
{
bool changed = false;
ImGui::PushItemWidth( 10.0f * ImGui::GetFontSize() );
changed |= ImGui::SliderFloat( "Height", &m_height, 0.0f, 2.0f, "%.2f" );
changed |= ImGui::SliderFloat( "Safety", &m_safetyFactor, 0.0f, 1.0f, "%.2f" );
ImGui::PopItemWidth();
if ( ImGui::Button( "Drop" ) || changed )
{
Launch();
}
return true;
}
static Sample* Create( SampleContext* context )
{
return new SafetyFactor( context );
}
b3BodyId m_bodyId;
float m_extent;
float m_height;
float m_safetyFactor;
float m_overlap;
};
static int sampleSafetyFactor = RegisterSample( "Continuous", "Safety Factor", SafetyFactor::Create );