Added a compile time option to use double precision body positions. Most
math remains single precision with minimal performance loss.

`BOX3D_DOUBLE_PRECISION`

Improved accuracy of body inertia computation when shapes are far from
the body origin.

WARNING: several APIs changed but migration should be straight forward
if doubles are not enabled.
This commit is contained in:
Erin Catto 2026-06-18 20:51:16 -07:00 • committed by GitHub
commit 09c65b08ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
115 changed files with 7591 additions and 5295 deletions

View file

@ -30,7 +30,7 @@ public:
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 6.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 6.0f, b3Pos_zero );
}
b3BodyDef bodyDef = b3DefaultBodyDef();
@ -171,7 +171,7 @@ public:
DrawTextLine( "triangle count = %d, bytes = %d", m_gridMesh->triangleCount, m_gridMesh->byteCount );
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( transform, 1.0f );
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
}
void Step() override
@ -215,7 +215,7 @@ public:
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 6.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 6.0f, b3Pos_zero );
}
b3BodyDef bodyDef = b3DefaultBodyDef();
@ -345,7 +345,7 @@ public:
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( transform, 1.0f );
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
}
void Step() override
@ -389,7 +389,7 @@ public:
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 6.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 6.0f, b3Pos_zero );
}
@ -563,7 +563,7 @@ public:
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Pos_zero );
}
b3BodyDef bodyDef = b3DefaultBodyDef();
@ -643,7 +643,7 @@ public:
m_humans[humanIndex] = {};
int groupIndex = humanIndex;
b3Vec3 position = { -14.0f + 1.5f * humanIndex, 8.0f, 0.0f };
b3Pos position = { -14.0f + 1.5f * humanIndex, 8.0f, 0.0f };
CreateHuman( &m_humans[humanIndex], m_worldId, position, frictionTorque, hertz, dampingRatio, groupIndex, nullptr,
colorize );
}
@ -741,7 +741,7 @@ static int sampleMeshReflection = RegisterSample( "Mesh", "Reflection", MeshRefl
struct CastContext
{
b3Vec3 point;
b3Pos point;
b3Vec3 normal;
float fraction;
bool hit;
@ -760,7 +760,7 @@ public:
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Pos_zero );
}
#if defined( NDEBUG )
@ -899,11 +899,11 @@ public:
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.1f, 0.0f }, b3Quat_identity };
DrawAxes( transform, 0.5f );
DrawAxes( b3MakeWorldTransform( transform ), 0.5f );
}
// This callback finds the closest hit.
static float CastCallback( b3ShapeId shapeId, b3Vec3 point, b3Vec3 normal, float fraction, uint64_t surfaceType,
static float CastCallback( b3ShapeId shapeId, b3Pos point, b3Vec3 normal, float fraction, uint64_t surfaceType,
int triangleIndex, int childIndex, void* context )
{
(void)shapeId;
@ -933,16 +933,17 @@ public:
// m_rayOrigin = { 0.0f, -FLT_EPSILON, 0.0f };
// m_rayTranslation = { -1000.0f, 0.0f, 0.0 };
b3RayResult result = b3World_CastRayClosest( m_worldId, m_rayOrigin, m_rayTranslation, b3DefaultQueryFilter() );
b3Pos origin = b3ToPos( m_rayOrigin );
b3RayResult result = b3World_CastRayClosest( m_worldId, origin, m_rayTranslation, b3DefaultQueryFilter() );
DrawPoint( m_rayOrigin, 6.0f, MakeColor( b3_colorGreenYellow ) );
DrawPoint( m_rayOrigin + m_rayTranslation, 6.0f, MakeColor( b3_colorRed ) );
DrawLine( m_rayOrigin, m_rayOrigin + m_rayTranslation, MakeColor( b3_colorGray ) );
DrawPoint( origin, 6.0f, MakeColor( b3_colorGreenYellow ) );
DrawPoint( b3OffsetPos( origin, m_rayTranslation ), 6.0f, MakeColor( b3_colorRed ) );
DrawLine( origin, b3OffsetPos( origin, m_rayTranslation ), MakeColor( b3_colorGray ) );
if ( result.hit )
{
b3Vec3 point = result.point;
DrawLine( point, point + 0.5f * result.normal, MakeColor( b3_colorGray ) );
b3Pos point = result.point;
DrawLine( point, b3OffsetPos( point, 0.5f * result.normal ), MakeColor( b3_colorGray ) );
DrawPoint( point, 10.0f, MakeColor( b3_colorOrange ) );
}
}
@ -957,19 +958,21 @@ public:
CastContext result = {};
result.fraction = 1.0f;
b3World_CastShape( m_worldId, &proxy, m_rayTranslation, b3DefaultQueryFilter(), CastCallback, &result );
b3World_CastShape( m_worldId, b3Pos_zero, &proxy, m_rayTranslation, b3DefaultQueryFilter(), CastCallback, &result );
DrawPoint( m_rayOrigin, 2.0f, MakeColor( b3_colorGreen ) );
DrawPoint( m_rayOrigin + m_rayTranslation, 2.0f, MakeColor( b3_colorRed ) );
DrawLine( m_rayOrigin, m_rayOrigin + m_rayTranslation, MakeColor( b3_colorYellow ) );
b3Pos origin = b3ToPos( m_rayOrigin );
DrawPoint( origin, 2.0f, MakeColor( b3_colorGreen ) );
DrawPoint( b3OffsetPos( origin, m_rayTranslation ), 2.0f, MakeColor( b3_colorRed ) );
DrawLine( origin, b3OffsetPos( origin, m_rayTranslation ), MakeColor( b3_colorYellow ) );
b3Sphere sphere = { b3Vec3_zero, m_radius };
DrawSolidSphere( { m_rayOrigin + result.fraction * m_rayTranslation, b3Quat_identity }, sphere, MakeColor( b3_colorOrange ) );
b3Transform sphereXf = { m_rayOrigin + result.fraction * m_rayTranslation, b3Quat_identity };
DrawSolidSphere( b3MakeWorldTransform( sphereXf ), sphere, MakeColor( b3_colorOrange ) );
if ( result.hit )
{
b3Vec3 point = result.point;
DrawLine( point, point + 0.5f * result.normal, MakeColor( b3_colorGreen ) );
b3Pos point = result.point;
DrawLine( point, b3OffsetPos( point, 0.5f * result.normal ), MakeColor( b3_colorGreen ) );
DrawPoint( point, 6.0f, MakeColor( b3_colorPurple ) );
}
}
@ -1023,7 +1026,7 @@ public:
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 50.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 50.0f, b3Pos_zero );
}
m_meshIndex = 0;
@ -1149,21 +1152,22 @@ public:
int axis = node->data.asNode.axis;
b3Vec3 center = b3AABB_Center( box );
b3Pos centerPos = b3ToPos( center );
if ( axis == 0 )
{
DrawArrow( center, center + 0.1f * b3Vec3_axisX, MakeColor( b3_colorRed ) );
DrawArrow( centerPos, b3OffsetPos( centerPos, 0.1f * b3Vec3_axisX ), MakeColor( b3_colorRed ) );
}
else if ( axis == 1 )
{
DrawArrow( center, center + 0.1f * b3Vec3_axisY, MakeColor( b3_colorGreen ) );
DrawArrow( centerPos, b3OffsetPos( centerPos, 0.1f * b3Vec3_axisY ), MakeColor( b3_colorGreen ) );
}
else if ( axis == 2 )
{
DrawArrow( center, center + 0.1f * b3Vec3_axisZ, MakeColor( b3_colorBlue ) );
DrawArrow( centerPos, b3OffsetPos( centerPos, 0.1f * b3Vec3_axisZ ), MakeColor( b3_colorBlue ) );
}
else
{
DrawSolidSphere( b3Transform_identity, { center, 0.03f }, MakeColor( b3_colorOrange ) );
DrawSolidSphere( b3WorldTransform_identity, { center, 0.03f }, MakeColor( b3_colorOrange ) );
}
}
@ -1225,7 +1229,7 @@ public:
void Render() override
{
Sample::Render();
DrawAxes( b3Transform_identity, 1.0f );
DrawAxes( b3WorldTransform_identity, 1.0f );
DrawTextLine( "triangle count = %d", m_mesh->triangleCount );
DrawTextLine( "vertex count = %d", m_mesh->vertexCount );
@ -1251,18 +1255,20 @@ public:
(void)area;
b3Vec3 p = ( 1.0f / 3.0f ) * ( v1 + v2 + v3 );
DrawPoint( p, 10.0f, MakeColor( b3_colorCyan ) );
DrawWorldString( p + offset, MakeColor( b3_colorOrange ), "%d", triangleIndex );
b3Pos pPos = b3ToPos( p );
DrawPoint( pPos, 10.0f, MakeColor( b3_colorCyan ) );
DrawString3D( b3OffsetPos( pPos, offset ), MakeColor( b3_colorOrange ), "%d", triangleIndex );
{
DrawPoint( v1, 10.0f, MakeColor( b3_colorRed ) );
DrawPoint( v2, 10.0f, MakeColor( b3_colorGreen ) );
DrawPoint( v3, 10.0f, MakeColor( b3_colorBlue ) );
DrawWorldString( v1 + offset, MakeColor( b3_colorRed ), "%d", i1 );
DrawWorldString( v2 + offset, MakeColor( b3_colorGreen ), "%d", i2 );
DrawWorldString( v3 + offset, MakeColor( b3_colorBlue ), "%d", i3 );
b3Pos p1 = b3ToPos( v1 );
b3Pos p2 = b3ToPos( v2 );
b3Pos p3 = b3ToPos( v3 );
DrawPoint( p1, 10.0f, MakeColor( b3_colorRed ) );
DrawPoint( p2, 10.0f, MakeColor( b3_colorGreen ) );
DrawPoint( p3, 10.0f, MakeColor( b3_colorBlue ) );
DrawString3D( b3OffsetPos( p1, offset ), MakeColor( b3_colorRed ), "%d", i1 );
DrawString3D( b3OffsetPos( p2, offset ), MakeColor( b3_colorGreen ), "%d", i2 );
DrawString3D( b3OffsetPos( p3, offset ), MakeColor( b3_colorBlue ), "%d", i3 );
}
}
}
@ -1303,7 +1309,7 @@ public:
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Pos_zero );
}
LoadTempMesh( "data/meshes/voxel_mesh_01.obj", m_tempMeshes + 0, 0.01f, true );
@ -1378,11 +1384,11 @@ public:
explicit VoxelMesh( SampleContext* context )
: Sample( context )
{
b3Vec3 origin = { 5000.0f, 3500.0f, -7000.0f };
b3Pos origin = { 5000.0f, 3500.0f, -7000.0f };
if ( context->restart == false )
{
m_camera->SetView( -115.0f, 5.0f, 5.0f, b3Vec3{ 0.0f, 10.0f, 0.0f } + origin );
m_camera->SetView( -115.0f, 5.0f, 5.0f, b3OffsetPos( origin, b3Vec3{ 0.0f, 10.0f, 0.0f } ) );
}
m_launchSpeedScale = 1.0f;
@ -1447,7 +1453,7 @@ public:
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.name = "cylinder";
bodyDef.type = b3_dynamicBody;
bodyDef.position = b3Vec3{ 5020.27734, 3506.22559, -6986.48584 };
bodyDef.position = { 5020.27734f, 3506.22559f, -6986.48584f };
bodyDef.rotation = { { 0.664546967, 0.669287264, 0.135021493 }, 0.303646326 };
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
@ -1497,7 +1503,7 @@ public:
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 30.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 30.0f, b3Pos_zero );
}
{
@ -1518,7 +1524,7 @@ public:
{
b3HullData* cylinderHull = b3CreateCylinder( 1.0f, 0.25f, 0.0f, 8 );
b3Vec3 positions[6] = {
b3Pos positions[6] = {
{ 0.0f, -10.2f, 0.0f }, { 0.0f, 9.2f, 0.0f }, { -9.8f, 0.0f, 0.0f },
{ 9.8f, 0.0f, 0.0f }, { 0.0f, 0.0f, -9.8f }, { 0.0f, 0.0f, 9.8f },
};
@ -1535,7 +1541,7 @@ public:
{
b3Capsule capsule ={{0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, 0.25f};
b3Vec3 positions[8] = {
b3Pos positions[8] = {
{ 0.0f, -10.2f, 2.0f }, { 0.0f, 9.2f, 2.0f },
{ 0.0f, -9.9f, 4.0f }, { 0.0f, 8.9f, 4.0f },
{ -9.8f, 2.0f, 0.0f },
@ -1561,7 +1567,7 @@ public:
Sample::Render();
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( transform, 1.0f );
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
}
void Step() override