This commit is contained in:
Erin Catto 2026-10-15 23:23:34 -07:00
commit 89e18ba067
8 changed files with 82 additions and 69 deletions

View file

@ -214,10 +214,11 @@ B3_API void b3World_SetMaximumLinearSpeed( b3WorldId worldId, float maximumLinea
/// Get the maximum linear speed. Usually in m/s.
B3_API float b3World_GetMaximumLinearSpeed( b3WorldId worldId );
// Set the tolerance for gyroscopic torque. [0, 1]
/// Set the tolerance for gyroscopic torque. [0, 1]
/// @note modifying this only affects new bodies or bodies that get their mass updated.
B3_API void b3World_SetIsotropicInertiaTolerance( b3WorldId worldId, float tolerance );
// Get the tolerance for gyroscopic torque. [0, 1]
/// Get the tolerance for gyroscopic torque. [0, 1]
B3_API float b3World_GetIsotropicInertiaTolerance( b3WorldId worldId );
/// Enable/disable constraint warm starting. Advanced feature for testing. Disabling

View file

@ -625,8 +625,6 @@ public:
m_capsuleId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
//shapeDef.baseMaterial.rollingResistance = 0.0f;
//shapeDef.baseMaterial.friction = 0.2f;
b3CreateCapsuleShape( m_capsuleId, &shapeDef, &capsule );
}

View file

@ -254,6 +254,7 @@ b3BodyId b3CreateBody( b3WorldId worldId, const b3BodyDef* def )
bodySim->force = b3Vec3_zero;
bodySim->torque = b3Vec3_zero;
bodySim->invMass = 0.0f;
bodySim->inertia = b3Mat3_zero;
bodySim->invInertiaLocal = b3Mat3_zero;
bodySim->minExtent = B3_HUGE;
bodySim->maxExtent = b3Vec3_zero;
@ -266,7 +267,7 @@ b3BodyId b3CreateBody( b3WorldId worldId, const b3BodyDef* def )
bodySim->flags |= def->allowFastRotation ? b3_allowFastRotation : 0;
bodySim->flags |= def->type == b3_dynamicBody ? b3_dynamicFlag : 0;
bodySim->flags |= def->enableSleep ? b3_enableSleep : 0;
bodySim->flags |= def->enableContactRecycling ? b3_bodyEnableContactRecycling : 0;
bodySim->flags |= def->enableContactRecycling ? b3_enableContactRecycling : 0;
if ( setId == b3_awakeSet )
{
@ -309,7 +310,6 @@ b3BodyId b3CreateBody( b3WorldId worldId, const b3BodyDef* def )
body->sleepVelocity = 0.0f;
body->safetyFactor = def->safetyFactor;
body->mass = 0.0f;
body->inertia = b3Mat3_zero;
body->nameId = b3AddName( &world->names, def->name );
body->type = def->type;
body->flags = bodySim->flags;
@ -893,9 +893,9 @@ b3BodyTOIResult b3Body_TimeOfImpactMover( b3BodyId bodyId, b3Pos origin, const b
}
// https://en.wikipedia.org/wiki/Cauchy_stress_tensor#Stress_deviator_tensor
static void b3UpdateIsotropicInertia( b3World* world, b3Body* body )
static void b3UpdateIsotropicInertia( b3World* world, b3Body* body, b3BodySim* bodySim )
{
b3Matrix3 inertia = body->inertia;
b3Matrix3 inertia = bodySim->inertia;
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;
@ -908,13 +908,13 @@ static void b3UpdateIsotropicInertia( b3World* world, b3Body* body )
float tolerance = world->isotropicInertiaTolerance;
if ( 2.0f * deviation <= 3.0f * tolerance * tolerance * mean * mean )
if ( 2.0f * deviation > 3.0f * tolerance * tolerance * mean * mean )
{
body->flags |= b3_isotropicInertia;
body->flags |= b3_enableGyroscopicTorque;
}
else
{
body->flags &= ~b3_isotropicInertia;
body->flags &= ~b3_enableGyroscopicTorque;
}
b3SyncBodyFlags( world, body );
@ -926,13 +926,12 @@ void b3UpdateBodyMassData( b3World* world, b3Body* body )
// Mass is no longer dirty
body->flags &= ~b3_dirtyMass;
body->flags |= b3_isotropicInertia;
body->flags &= ~b3_enableGyroscopicTorque;
b3SyncBodyFlags( world, body );
// Compute mass data from shapes. Each shape has its own density.
body->mass = 0.0f;
body->inertia = b3Mat3_zero;
bodySim->inertia = b3Mat3_zero;
bodySim->invMass = 0.0f;
bodySim->invInertiaLocal = b3Mat3_zero;
bodySim->invInertiaWorld = b3Mat3_zero;
@ -1016,19 +1015,19 @@ void b3UpdateBodyMassData( b3World* world, b3Body* body )
// Shift to center of mass. This is safe because it can only increase.
b3Vec3 offset = b3Sub( localCenter, massData.center );
b3Matrix3 inertia = b3AddMM( massData.inertia, b3Steiner( massData.mass, offset ) );
body->inertia = b3AddMM( body->inertia, inertia );
bodySim->inertia = b3AddMM( bodySim->inertia, inertia );
}
b3StackFree( &world->stack, masses );
masses = NULL;
float det = b3Det( body->inertia );
float det = b3Det( bodySim->inertia );
B3_ASSERT( det >= 0.0f );
if ( det > 0.0f )
{
// This call is faster than b3Invert
bodySim->invInertiaLocal = b3InvertT( body->inertia );
bodySim->invInertiaLocal = b3InvertT( bodySim->inertia );
b3Matrix3 rotationMatrix = b3MakeMatrixFromQuat( bodySim->transform.q );
bodySim->invInertiaWorld = b3MulMM( b3MulMM( rotationMatrix, bodySim->invInertiaLocal ), b3Transpose( rotationMatrix ) );
@ -1077,12 +1076,12 @@ void b3UpdateBodyMassData( b3World* world, b3Body* body )
// Apply fixed rotation
if ( ( bodySim->flags & b3_fixedRotation ) == b3_fixedRotation )
{
body->inertia = b3Mat3_zero;
bodySim->inertia = b3Mat3_zero;
bodySim->invInertiaLocal = b3Mat3_zero;
bodySim->invInertiaWorld = b3Mat3_zero;
}
b3UpdateIsotropicInertia( world, body );
b3UpdateIsotropicInertia( world, body, bodySim );
}
b3Pos b3Body_GetPosition( b3BodyId bodyId )
@ -1849,7 +1848,8 @@ b3Matrix3 b3Body_GetLocalRotationalInertia( b3BodyId bodyId )
{
b3World* world = b3GetWorld( bodyId.world0 );
b3Body* body = b3GetBodyFullId( world, bodyId );
return body->inertia;
b3BodySim* bodySim = b3GetBodySim( world, body );
return bodySim->inertia;
}
float b3Body_GetInverseMass( b3BodyId bodyId )
@ -1906,7 +1906,6 @@ void b3Body_SetMassData( b3BodyId bodyId, b3MassData massData )
b3SyncBodyFlags( world, body );
body->mass = massData.mass;
body->inertia = massData.inertia;
bodySim->localCenter = massData.center;
b3Pos oldCenter = bodySim->center;
@ -1914,6 +1913,7 @@ void b3Body_SetMassData( b3BodyId bodyId, b3MassData massData )
bodySim->center = center;
bodySim->center0 = center;
bodySim->invMass = body->mass > 0.0f ? 1.0f / body->mass : 0.0f;
bodySim->inertia = massData.inertia;
// Update center of mass velocity
b3BodyState* state = b3GetBodyState( world, body );
@ -1923,13 +1923,13 @@ void b3Body_SetMassData( b3BodyId bodyId, b3MassData massData )
state->linearVelocity = b3Add( state->linearVelocity, deltaLinear );
}
float det = b3Det( body->inertia );
float det = b3Det( bodySim->inertia );
B3_ASSERT( det >= 0.0f );
if ( det > 0.0f )
{
// This call is faster than b3Invert
bodySim->invInertiaLocal = b3InvertT( body->inertia );
bodySim->invInertiaLocal = b3InvertT( bodySim->inertia );
b3Matrix3 rotationMatrix = b3MakeMatrixFromQuat( bodySim->transform.q );
bodySim->invInertiaWorld = b3MulMM( b3MulMM( rotationMatrix, bodySim->invInertiaLocal ), b3Transpose( rotationMatrix ) );
@ -1943,12 +1943,12 @@ void b3Body_SetMassData( b3BodyId bodyId, b3MassData massData )
// Apply fixed rotation
if ( ( bodySim->flags & b3_fixedRotation ) == b3_fixedRotation )
{
body->inertia = b3Mat3_zero;
bodySim->inertia = b3Mat3_zero;
bodySim->invInertiaLocal = b3Mat3_zero;
bodySim->invInertiaWorld = b3Mat3_zero;
}
b3UpdateIsotropicInertia( world, body );
b3UpdateIsotropicInertia( world, body, bodySim );
// Update extents using supplied mass center.
bodySim->minExtent = B3_HUGE;
@ -1982,7 +1982,7 @@ b3MassData b3Body_GetMassData( b3BodyId bodyId )
b3World* world = b3GetWorld( bodyId.world0 );
b3Body* body = b3GetBodyFullId( world, bodyId );
b3BodySim* bodySim = b3GetBodySim( world, body );
b3MassData massData = { body->mass, bodySim->localCenter, body->inertia };
b3MassData massData = { body->mass, bodySim->localCenter, bodySim->inertia };
return massData;
}
@ -2533,15 +2533,15 @@ void b3Body_EnableContactRecycling( b3BodyId bodyId, bool flag )
B3_REC( world, BodyEnableContactRecycling, bodyId, flag );
uint32_t newFlag = flag ? b3_bodyEnableContactRecycling : 0;
uint32_t newFlag = flag ? b3_enableContactRecycling : 0;
b3Body* body = b3GetBodyFullId( world, bodyId );
if ( ( body->flags & b3_bodyEnableContactRecycling ) == newFlag )
if ( ( body->flags & b3_enableContactRecycling ) == newFlag )
{
return;
}
body->flags &= ~b3_bodyEnableContactRecycling;
body->flags &= ~b3_enableContactRecycling;
body->flags |= newFlag;
b3SyncBodyFlags( world, body );
@ -2551,7 +2551,7 @@ bool b3Body_IsContactRecyclingEnabled( b3BodyId bodyId )
{
b3World* world = b3GetWorld( bodyId.world0 );
b3Body* body = b3GetBodyFullId( world, bodyId );
return ( body->flags & b3_bodyEnableContactRecycling ) != 0;
return ( body->flags & b3_enableContactRecycling ) != 0;
}
void b3Body_EnableHitEvents( b3BodyId bodyId, bool flag )

View file

@ -58,13 +58,13 @@ enum b3BodyFlags
b3_enableSleep = 0x00002000,
b3_bodyEnableContactRecycling = 0x00004000,
b3_enableContactRecycling = 0x00004000,
// The user deferred mass computation via the updateBodyMass shape option and mass
// data still hasn't been set.
b3_dirtyMass = 0x00008000,
b3_isotropicInertia = 0x00010000,
b3_enableGyroscopicTorque = 0x00010000,
// All lock flags
b3_allLocks = b3_lockLinearX | b3_lockLinearY | b3_lockLinearZ | b3_lockAngularX | b3_lockAngularY | b3_lockAngularZ,
@ -116,9 +116,6 @@ typedef struct b3Body
float safetyFactor;
float mass;
// local space inertia
b3Matrix3 inertia;
// this is used to adjust the fellAsleep flag in the body move array
int bodyMoveIndex;
@ -215,6 +212,9 @@ typedef struct b3BodySim
float invMass;
// Local space inertia
b3Matrix3 inertia;
// Rotational inertia about the center of mass. The world space inverse inertia tensor
// must be updated whenever the body rotation is modified.
b3Matrix3 invInertiaLocal;

View file

@ -5,7 +5,6 @@
#include "algorithm.h"
#include "body.h"
#include "compound.h"
#include "island.h"
#include "manifold.h"
#include "physics_world.h"
@ -250,7 +249,7 @@ void b3CreateContact( b3World* world, b3Shape* shapeA, b3Shape* shapeB, int chil
}
// Disabling speculative collision also disables recycling.
if ( ( bodyA->flags & b3_bodyEnableContactRecycling ) != 0 && ( bodyB->flags & b3_bodyEnableContactRecycling ) != 0 &&
if ( ( bodyA->flags & b3_enableContactRecycling ) != 0 && ( bodyB->flags & b3_enableContactRecycling ) != 0 &&
( contact->flags & b3_enableSpeculativePoints ) != 0 )
{
contact->flags |= b3_contactRecycleFlag;

View file

@ -106,26 +106,23 @@ static void b3IntegrateVelocitiesTask( b3SolverBlock block, b3StepContext* conte
// I * (w2 - w1) + h * cross(w2, I * w2) = 0
// This is all done in local coordinates where the Jacobian is easier to compute.
// This improves the simulation of long skinny bodies.
if ( ( sim->flags & b3_isotropicInertia ) == 0 )
if ( sim->flags & b3_enableGyroscopicTorque )
{
// Get current rotation.
b3Quat q0 = sim->transform.q;
b3Quat q = b3MulQuat( state->deltaRotation, q0 );
// todo wasteful computation
b3Matrix3 inertiaLocal = b3InvertMatrix( sim->invInertiaLocal );
// Compute local angular velocity
b3Vec3 omega1 = b3InvRotateVector( q, w );
b3Vec3 omega2 = omega1;
// Symmetric inertia tensor: 6 unique entries (column-major)
float i00 = inertiaLocal.cx.x;
float i01 = inertiaLocal.cy.x;
float i02 = inertiaLocal.cz.x;
float i11 = inertiaLocal.cy.y;
float i12 = inertiaLocal.cz.y;
float i22 = inertiaLocal.cz.z;
float i00 = sim->inertia.cx.x;
float i01 = sim->inertia.cy.x;
float i02 = sim->inertia.cz.x;
float i11 = sim->inertia.cy.y;
float i12 = sim->inertia.cz.y;
float i22 = sim->inertia.cz.z;
for ( int gyroIteration = 0; gyroIteration < B3_GYROSCOPIC_ITERATIONS; ++gyroIteration )
{

View file

@ -540,11 +540,11 @@ static b3BodyId CreateSpinningHullBody( b3WorldId worldId, float x, const b3Hull
return bodyId;
}
static bool IsIsotropic( b3BodyId bodyId )
static bool HasGyroscopicTorque( b3BodyId bodyId )
{
b3World* world = b3GetWorld( bodyId.world0 );
b3Body* body = b3GetBodyFullId( world, bodyId );
return ( body->flags & b3_isotropicInertia ) != 0;
return ( body->flags & b3_enableGyroscopicTorque ) != 0;
}
// Bodies within the isotropic tolerance skip the gyroscopic solve, so in free flight the world
@ -571,11 +571,11 @@ static int IsotropicInertiaTolerance( void )
b3BodyId rotatedCubeId = CreateSpinningHullBody( worldId, 15.0f, &rotatedCube.base );
b3BodyId plankId = CreateSpinningHullBody( worldId, 20.0f, &plank.base );
ENSURE( IsIsotropic( rockId ) );
ENSURE( IsIsotropic( complexId ) );
ENSURE( IsIsotropic( cubeId ) );
ENSURE( IsIsotropic( rotatedCubeId ) );
ENSURE( IsIsotropic( plankId ) == false );
ENSURE( HasGyroscopicTorque( rockId ) == false );
ENSURE( HasGyroscopicTorque( complexId ) == false );
ENSURE( HasGyroscopicTorque( cubeId ) == false );
ENSURE( HasGyroscopicTorque( rotatedCubeId ) == false );
ENSURE( HasGyroscopicTorque( plankId ) );
for ( int i = 0; i < 10; ++i )
{
@ -592,7 +592,25 @@ static int IsotropicInertiaTolerance( void )
ENSURE( b3Length( b3Sub( w, w0 ) ) > 0.01f );
b3Body_SetMotionLocks( plankId, (b3MotionLocks){ .angularX = true, .angularY = true, .angularZ = true } );
ENSURE( IsIsotropic( plankId ) );
ENSURE( HasGyroscopicTorque( plankId ) == false );
// Gyroscopic torque stays off until a mass update proves the body anisotropic
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position.x = 25.0f;
bodyDef.angularVelocity = w0;
b3BodyId emptyId = b3CreateBody( worldId, &bodyDef );
ENSURE( HasGyroscopicTorque( emptyId ) == false );
bodyDef.position.x = 30.0f;
b3BodyId deferredId = b3CreateBody( worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.updateBodyMass = false;
b3CreateHullShape( deferredId, &shapeDef, &plank.base );
ENSURE( HasGyroscopicTorque( deferredId ) == false );
b3Body_ApplyMassFromShapes( deferredId );
ENSURE( HasGyroscopicTorque( deferredId ) );
b3DestroyWorld( worldId );
}
@ -606,8 +624,8 @@ static int IsotropicInertiaTolerance( void )
b3BodyId rockId = CreateSpinningHullBody( worldId, 0.0f, rock );
b3BodyId cubeId = CreateSpinningHullBody( worldId, 10.0f, &cube.base );
ENSURE( IsIsotropic( rockId ) == false );
ENSURE( IsIsotropic( cubeId ) );
ENSURE( HasGyroscopicTorque( rockId ) );
ENSURE( HasGyroscopicTorque( cubeId ) == false );
for ( int i = 0; i < 10; ++i )
{

View file

@ -19,27 +19,27 @@
// Double precision accumulates body positions in double, so the settle/sleep step and the
// state hash differ from the float build. Both modes are internally deterministic.
#if defined( BOX3D_DOUBLE_PRECISION )
#define RAGDOLL_SLEEP_STEP 266
#define RAGDOLL_HASH 0x1680A33C
#define RAGDOLL_SLEEP_STEP 263
#define RAGDOLL_HASH 0xA5423F98
#define WAVE_PILE_SLEEP_STEP 272
#define WAVE_PILE_HASH 0xF7EF20F7
#define WAVE_PILE_HASH 0xC8E5F3D6
#define QUERY_SPAWN_SLEEP_STEP 242
#define QUERY_SPAWN_HASH 0xCA68A74D
#define QUERY_SPAWN_HASH 0x489585E6
#define QUERY_SPAWN_HIT_COUNT 59
#define QUERY_SPAWN_QUERY_HASH 0x5B4429DC
#define MESH_DROP_SLEEP_STEP 218
#define MESH_DROP_HASH 0x10051259
#define MESH_DROP_HASH 0xD378CF04
#else
#define RAGDOLL_SLEEP_STEP 281
#define RAGDOLL_HASH 0xE9DB034E
#define WAVE_PILE_SLEEP_STEP 275
#define WAVE_PILE_HASH 0xA6B9D38B
#define RAGDOLL_SLEEP_STEP 291
#define RAGDOLL_HASH 0xFF4C605B
#define WAVE_PILE_SLEEP_STEP 272
#define WAVE_PILE_HASH 0x266F83B0
#define QUERY_SPAWN_SLEEP_STEP 242
#define QUERY_SPAWN_HASH 0x2DA37C5A
#define QUERY_SPAWN_HASH 0xF3AD9BA6
#define QUERY_SPAWN_HIT_COUNT 59
#define QUERY_SPAWN_QUERY_HASH 0xE3271F3D
#define MESH_DROP_SLEEP_STEP 218
#define MESH_DROP_HASH 0x983F27EA
#define MESH_DROP_SLEEP_STEP 226
#define MESH_DROP_HASH 0xD2FF069D
#endif
// The goldens above pin exact values for the default four point manifold. A build that