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

@ -1,10 +1,10 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT
#include "gfx/draw.h"
#include "human.h"
#include "mesh_loader.h"
#include "sample.h"
#include "gfx/draw.h"
#include "utils.h"
#include "box3d/box3d.h"
@ -19,7 +19,7 @@ public:
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 45.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 45.0f, b3Pos_zero );
}
{
@ -30,7 +30,7 @@ public:
b3Transform hullTransform;
hullTransform.p = { 1.0f, -0.125f * a, 0.0f };
hullTransform.q = b3MakeQuatFromAxisAngle( b3Normalize({ 1.0f, 0.0f, 1.0f }), 0.0f * B3_PI );
hullTransform.q = b3MakeQuatFromAxisAngle( b3Normalize( { 1.0f, 0.0f, 1.0f } ), 0.0f * B3_PI );
b3CompoundHullDef hullDef = {
.hull = &box.base,
@ -89,7 +89,7 @@ public:
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( transform, 1.0f );
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
int height = b3DynamicTree_GetHeight( &m_compound->tree );
DrawTextLine( "compound tree height = %d", height );
@ -113,7 +113,7 @@ public:
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 45.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 45.0f, b3Pos_zero );
}
float h = 10.0f;
@ -149,7 +149,7 @@ public:
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( transform, 1.0f );
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
int height = b3DynamicTree_GetHeight( &m_compound->tree );
DrawTextLine( "compound tree height = %d", height );
@ -174,7 +174,7 @@ public:
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 45.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 45.0f, b3Pos_zero );
}
float h = 10.0f;
@ -223,7 +223,7 @@ public:
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( transform, 1.0f );
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
int height = b3DynamicTree_GetHeight( &m_compound->tree );
DrawTextLine( "compound tree height = %d", height );
@ -248,7 +248,7 @@ public:
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 45.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 45.0f, b3Pos_zero );
}
{
@ -302,7 +302,7 @@ public:
hulls = nullptr;
}
//b3World_SetContactRecycleDistance( m_worldId, 0.0f );
// b3World_SetContactRecycleDistance( m_worldId, 0.0f );
{
b3BodyDef bodyDef = b3DefaultBodyDef();
@ -338,7 +338,7 @@ public:
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( transform, 1.0f );
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
DrawTextLine( "compound hull count = %d, mesh count = %d", m_compound->hullCount, m_compound->meshCount );
DrawTextLine( "compound byte count = %d", m_compound->byteCount );
@ -366,7 +366,7 @@ public:
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 45.0f, b3Vec3_zero );
m_camera->SetView( 45.0f, 30.0f, 45.0f, b3Pos_zero );
}
{
@ -455,7 +455,7 @@ public:
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( transform, 1.0f );
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
DrawTextLine( "compound instance count = %d, byte count = %d", m_compound->meshCount, m_compound->byteCount );
@ -492,7 +492,7 @@ public:
constexpr float a = 4.0f;
m_worldWidth = 2.0f * gridCount * a;
b3Vec3 position = { 0.0f, 10.0f, 0.0f };
b3Pos position = { 0.0f, 10.0f, 0.0f };
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 10.0f, 5.0f, position );
@ -645,7 +645,7 @@ public:
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.position = { -1.0f, -0.5f, 2.0f };
bodyDef.rotation = b3MakeQuatFromAxisAngle( {0.0f, 1.0f, 0.0f}, -1.15f * B3_PI );
bodyDef.rotation = b3MakeQuatFromAxisAngle( { 0.0f, 1.0f, 0.0f }, -1.15f * B3_PI );
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
@ -686,7 +686,7 @@ public:
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( transform, 4.0f );
DrawAxes( b3MakeWorldTransform( transform ), 4.0f );
DrawTextLine( "surface type = %d", m_userMaterialId );
DrawTextLine( "compound capsules/hulls/meshes/sphere = %d / %d / %d / %d", m_compound->capsuleCount,
@ -710,11 +710,11 @@ public:
CastClosestContext context = {};
(void)b3World_CastRay( m_worldId, m_rayOrigin, translation, filter, CastClosestCallback, &context );
DrawLine( m_rayOrigin, m_rayOrigin + translation, MakeColor( b3_colorAliceBlue ) );
DrawLine( m_rayOrigin, b3OffsetPos( m_rayOrigin, translation ), MakeColor( b3_colorAliceBlue ) );
if ( context.hit )
{
b3Vec3 p1 = context.point;
b3Vec3 p2 = b3MulAdd( p1, 0.5f, context.normal );
b3Pos p1 = context.point;
b3Pos p2 = p1 + 0.5f * context.normal;
DrawLine( p1, p2, MakeColor( b3_colorYellow ) );
DrawPoint( p1, 8.0f, MakeColor( b3_colorLightCoral ) );
DrawTextLine( "ray hit triangle/child/material = %d / %d / %d", context.triangleIndex, context.childIndex,
@ -728,20 +728,20 @@ public:
{
CastClosestContext context = {};
b3Vec3 origin = b3Sub( m_rayOrigin, { 1.0f, 0.0f, 1.0f } );
b3ShapeProxy proxy = { &origin, 1, 0.25f };
b3World_CastShape( m_worldId, &proxy, translation, filter, CastClosestCallback, &context );
b3Pos origin = m_rayOrigin - b3Vec3{ 1.0f, 0.0f, 1.0f };
b3ShapeProxy proxy = { &b3Vec3_zero, 1, 0.25f };
b3World_CastShape( m_worldId, origin, &proxy, translation, filter, CastClosestCallback, &context );
DrawLine( origin, origin + translation, MakeColor( b3_colorAliceBlue ) );
DrawLine( origin, b3OffsetPos( origin, translation ), MakeColor( b3_colorAliceBlue ) );
if ( context.hit )
{
b3Vec3 position = b3MulAdd( origin, context.fraction, translation );
b3Vec3 p1 = context.point;
b3Vec3 p2 = b3MulAdd( p1, 0.5f, context.normal );
b3Pos position = b3OffsetPos( origin, context.fraction * translation );
b3Pos p1 = context.point;
b3Pos p2 = p1 + 0.5f * context.normal;
DrawLine( p1, p2, MakeColor( b3_colorYellow ) );
DrawPoint( p1, 8.0f, MakeColor( b3_colorLightCoral ) );
b3Sphere sphere = { position, 0.25f };
DrawSolidSphere( b3Transform_identity, sphere, MakeColor( b3_colorOrchid ) );
b3Sphere sphere = { b3Vec3_zero, 0.25f };
DrawSolidSphere( { position, b3Quat_identity }, sphere, MakeColor( b3_colorOrchid ) );
DrawTextLine( "shape hit triangle/child/material = %d / %d / %d", context.triangleIndex, context.childIndex,
context.materialId );
}
@ -753,13 +753,13 @@ public:
{
bool overlap = false;
b3Vec3 origin = { m_rayOrigin.x - 1.0f, 2.0f, m_rayOrigin.z - 1.0f };
b3ShapeProxy proxy = { &origin, 1, 0.3f };
b3World_OverlapShape( m_worldId, &proxy, filter, OverlapResultFcn, &overlap );
b3Pos origin = { m_rayOrigin.x - 1.0f, 2.0f, m_rayOrigin.z - 1.0f };
b3ShapeProxy proxy = { &b3Vec3_zero, 1, 0.3f };
b3World_OverlapShape( m_worldId, origin, &proxy, filter, OverlapResultFcn, &overlap );
b3HexColor color = overlap ? b3_colorDarkMagenta : b3_colorDarkSeaGreen;
b3Sphere sphere = { origin, 0.3f };
DrawSolidSphere( b3Transform_identity, sphere, MakeColor( color ) );
b3Sphere sphere = { b3Vec3_zero, 0.3f };
DrawSolidSphere( { origin, b3Quat_identity }, sphere, MakeColor( color ) );
}
if ( m_rayOrigin.x > 0.45f * m_worldWidth )
@ -792,7 +792,7 @@ public:
b3Compound* m_compound;
CharacterMover m_mover;
float m_worldWidth;
b3Vec3 m_rayOrigin;
b3Pos m_rayOrigin;
};
static int sampleVillage = RegisterSample( "Compound", "Village", Village::Create );