Reduce ghosts (#212)

Full ghost collision handling for sphere, capsule, and hull versus a
single mesh or heightfield. This requires mesh to be built with edge
identification.

Optimized mesh builder.

Removed B3_MAX_MESH_CONTACT_TRIANGLES. You will still hit performance
problems for dense meshes. Fixes #184
This commit is contained in:
Erin Catto 2026-11-10 21:53:10 -08:00 • committed by GitHub
commit 38f2ae1e19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
61 changed files with 17756 additions and 1492 deletions

View file

@ -2320,6 +2320,8 @@ public:
m_upperSteeringDegrees = 45.0f;
m_maxSteeringTorque = 5.0f;
m_targetSteeringDegrees = 0.0f;
m_wheelType = WheelType::sphere;
m_cylinderSides = 16;
b3BodyId groundId;
{
@ -2371,15 +2373,10 @@ public:
b3CreateParallelJoint( m_worldId, &parallelJointDef );
}
shapeDef.density = 2.0f;
shapeDef.baseMaterial.friction = 3.0f;
bodyDef.type = b3_dynamicBody;
bodyDef.allowFastRotation = true;
bodyDef.rotation = b3ComputeQuatBetweenUnitVectors( b3Vec3_axisY, b3Vec3_axisZ );
// b3HullData* hull = b3CreateCylinder( 0.25f, 0.4f, 0.0f, 16 );
b3WheelJointDef jointDef = b3DefaultWheelJointDef();
jointDef.base.bodyIdA = m_chassisId;
jointDef.base.localFrameA.q = b3ComputeQuatBetweenUnitVectors( b3Vec3_axisX, b3Vec3_axisY );
@ -2401,15 +2398,11 @@ public:
jointDef.lowerSteeringLimit = B3_PI / 180.0f * m_lowerSteeringDegrees;
jointDef.upperSteeringLimit = B3_PI / 180.0f * m_upperSteeringDegrees;
b3Sphere sphere = { b3Vec3_zero, 0.4f };
{
bodyDef.position = { 1.5f, 2.0f, 0.8f };
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
// b3CreateHullShape( bodyId, &shapeDef, hull );
m_wheelIds[0] = b3CreateBody( m_worldId, &bodyDef );
jointDef.base.bodyIdB = bodyId;
jointDef.base.bodyIdB = m_wheelIds[0];
jointDef.base.localFrameA.p = { 1.5f, -0.5f, 0.8f };
jointDef.enableSteering = true;
jointDef.enableSpinMotor = false;
@ -2418,11 +2411,9 @@ public:
{
bodyDef.position = { 1.5f, 2.0f, -0.8f };
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
// b3CreateHullShape( bodyId, &shapeDef, hull );
m_wheelIds[1] = b3CreateBody( m_worldId, &bodyDef );
jointDef.base.bodyIdB = bodyId;
jointDef.base.bodyIdB = m_wheelIds[1];
jointDef.base.localFrameA.p = { 1.5f, -0.5f, -0.8f };
jointDef.enableSteering = true;
jointDef.enableSpinMotor = false;
@ -2431,11 +2422,9 @@ public:
{
bodyDef.position = { -1.5f, 2.0f, 0.8f };
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
// b3CreateHullShape( bodyId, &shapeDef, hull );
m_wheelIds[2] = b3CreateBody( m_worldId, &bodyDef );
jointDef.base.bodyIdB = bodyId;
jointDef.base.bodyIdB = m_wheelIds[2];
jointDef.base.localFrameA.p = { -1.5f, -0.5f, 0.8f };
jointDef.enableSteering = false;
jointDef.enableSpinMotor = true;
@ -2444,18 +2433,21 @@ public:
{
bodyDef.position = { -1.5f, 2.0f, -0.8f };
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
// b3CreateHullShape( bodyId, &shapeDef, hull );
m_wheelIds[3] = b3CreateBody( m_worldId, &bodyDef );
jointDef.base.bodyIdB = bodyId;
jointDef.base.bodyIdB = m_wheelIds[3];
jointDef.base.localFrameA.p = { -1.5f, -0.5f, -0.8f };
jointDef.enableSteering = false;
jointDef.enableSpinMotor = true;
m_rearRightId = b3CreateWheelJoint( m_worldId, &jointDef );
}
// b3DestroyHull( hull );
for ( int i = 0; i < 4; ++i )
{
m_wheelShapeIds[i] = b3_nullShapeId;
}
CreateWheelShapes();
m_camera->m_thirdPerson = false;
@ -2474,6 +2466,59 @@ public:
b3DestroyHeightField( m_heightField );
}
void CreateWheelShapes()
{
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 2.0f;
shapeDef.baseMaterial.friction = 3.0f;
float radius = 0.4f;
float width = 0.25f;
b3HullData* cylinder = nullptr;
if ( m_wheelType == WheelType::cylinder )
{
float w = 1.5f * width;
cylinder = b3CreateCylinder( w, radius, -0.5f * w, m_cylinderSides );
}
for ( int i = 0; i < 4; ++i )
{
if ( B3_IS_NON_NULL( m_wheelShapeIds[i] ) )
{
b3DestroyShape( m_wheelShapeIds[i], false );
}
b3BodyId bodyId = m_wheelIds[i];
switch ( m_wheelType )
{
case WheelType::sphere:
{
b3Sphere sphere = { b3Vec3_zero, radius };
m_wheelShapeIds[i] = b3CreateSphereShape( bodyId, &shapeDef, &sphere );
}
break;
case WheelType::capsule:
{
b3Capsule capsule = { { 0.0f, -0.5f * width, 0.0f }, { 0.0f, 0.5f * width, 0.0f }, radius };
m_wheelShapeIds[i] = b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
}
break;
case WheelType::cylinder:
m_wheelShapeIds[i] = b3CreateHullShape( bodyId, &shapeDef, cylinder );
break;
}
}
if ( cylinder != nullptr )
{
b3DestroyHull( cylinder );
}
}
void Keyboard( int key, int action, int mods ) override
{
if ( key == KEY_T && action == ACTION_PRESS )
@ -2484,6 +2529,32 @@ public:
bool DrawControls() override
{
ImGui::Text( "Wheels" );
if ( ImGui::RadioButton( "Sphere", m_wheelType == WheelType::sphere ) )
{
m_wheelType = WheelType::sphere;
CreateWheelShapes();
}
if ( ImGui::RadioButton( "Capsule", m_wheelType == WheelType::capsule ) )
{
m_wheelType = WheelType::capsule;
CreateWheelShapes();
}
if ( ImGui::RadioButton( "Cylinder", m_wheelType == WheelType::cylinder ) )
{
m_wheelType = WheelType::cylinder;
CreateWheelShapes();
}
if ( ImGui::SliderInt( "Sides", &m_cylinderSides, 3, 32 ) && m_wheelType == WheelType::cylinder )
{
CreateWheelShapes();
}
ImGui::Separator();
ImGui::Text( "Suspension" );
if ( ImGui::SliderFloat( "Min##Suspension", &m_lowerTranslation, -10.0f, 10.0f, "%.1f" ) )
{
@ -2659,8 +2730,19 @@ public:
return new Driving( context );
}
enum class WheelType
{
sphere,
capsule,
cylinder,
};
b3HeightFieldData* m_heightField;
b3BodyId m_chassisId;
b3BodyId m_wheelIds[4];
b3ShapeId m_wheelShapeIds[4];
WheelType m_wheelType;
int m_cylinderSides;
b3JointId m_frontLeftId;
b3JointId m_frontRightId;
b3JointId m_rearLeftId;