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

@ -652,9 +652,8 @@ static void DestroyDebugShape( void* userShape, void* context )
FreeDebugShape( index );
}
// World-space cell size for the procedural ground grid (meters per minor
// cell, major lines fire every 10 cells inside the shader).
#define BOX3D_GROUND_GRID_CELL_SIZE 1.0f
// BOX3D_GROUND_GRID_CELL_SIZE lives in the header so the host can wrap the draw
// origin to the same grid period.
// Alpha applied to non-static shapes when box3dAdapterSetTransparentDynamic is on.
#define BOX3D_TRANSPARENT_DYNAMIC_ALPHA 0.5f
@ -696,7 +695,7 @@ static void AppendResolvedShape( const DebugShape* us, b3Transform baseTransform
}
}
static bool DrawShape( void* userShape, b3Transform shapeTransform, b3HexColor color, void* context )
static bool DrawShape( void* userShape, b3WorldTransform shapeTransform, b3HexColor color, void* context )
{
(void)context;
if ( userShape == NULL )
@ -704,6 +703,9 @@ static bool DrawShape( void* userShape, b3Transform shapeTransform, b3HexColor c
return true; // unsupported shape type, skip and continue
}
// Shift the world transform into the camera relative frame the primitives render in
b3Transform shapeRelative = b3ToRelativeTransform( shapeTransform, GetDrawOrigin() );
DebugShape* us = (DebugShape*)userShape;
RefreshMaterialFromOverride( us );
@ -752,13 +754,13 @@ static bool DrawShape( void* userShape, b3Transform shapeTransform, b3HexColor c
{
for ( int ci = us->compound.firstChild; ci != -1; ci = s_adapter.pool[ci].nextChild )
{
b3Transform base = b3MulTransforms( shapeTransform, s_adapter.pool[ci].childTransform );
b3Transform base = b3MulTransforms( shapeRelative, s_adapter.pool[ci].childTransform );
AppendResolvedShape( &s_adapter.pool[ci], base, c, metallic, roughness, shadowCast, hk );
}
}
else
{
AppendResolvedShape( us, shapeTransform, c, metallic, roughness, shadowCast, hk );
AppendResolvedShape( us, shapeRelative, c, metallic, roughness, shadowCast, hk );
}
return true;
@ -767,18 +769,20 @@ static bool DrawShape( void* userShape, b3Transform shapeTransform, b3HexColor c
#define BOX3D_LINE_THICKNESS_PX 2.5f
#define BOX3D_TRANSFORM_LENGTH (0.25f * b3GetLengthUnitsPerMeter())
static void DrawSegmentFcn( b3Vec3 p1, b3Vec3 p2, b3HexColor color, void* context )
static void DrawSegmentFcn( b3Pos p1, b3Pos p2, b3HexColor color, void* context )
{
(void)context;
OverlayAppendLine( p1, p2, HexColorToVec4( color ), BOX3D_LINE_THICKNESS_PX, OVERLAY_THICKNESS_PIXELS,
OVERLAY_OCCLUSION_HIDE );
b3Pos o = GetDrawOrigin();
OverlayAppendLine( b3SubPos( p1, o ), b3SubPos( p2, o ), HexColorToVec4( color ), BOX3D_LINE_THICKNESS_PX,
OVERLAY_THICKNESS_PIXELS, OVERLAY_OCCLUSION_HIDE );
}
static void DrawTransformFcn( b3Transform transform, void* context )
static void DrawTransformFcn( b3WorldTransform transform, void* context )
{
(void)context;
b3Vec3 origin = transform.p;
b3Matrix3 basis = b3MakeMatrixFromQuat( transform.q );
b3Transform local = b3ToRelativeTransform( transform, GetDrawOrigin() );
b3Vec3 origin = local.p;
b3Matrix3 basis = b3MakeMatrixFromQuat( local.q );
float L = BOX3D_TRANSFORM_LENGTH;
Vec4 red = MakeVec4( 1.0f, 0.0f, 0.0f, 1.0f );
Vec4 green = MakeVec4( 0.0f, 1.0f, 0.0f, 1.0f );
@ -791,18 +795,21 @@ static void DrawTransformFcn( b3Transform transform, void* context )
OVERLAY_OCCLUSION_HIDE );
}
static void DrawPointFcn( b3Vec3 p, float size, b3HexColor color, void* context )
static void DrawPointFcn( b3Pos p, float size, b3HexColor color, void* context )
{
(void)context;
OverlayAppendPoint( p, HexColorToVec4( color ), size, OVERLAY_THICKNESS_PIXELS, OVERLAY_OCCLUSION_HIDE );
OverlayAppendPoint( b3SubPos( p, GetDrawOrigin() ), HexColorToVec4( color ), size, OVERLAY_THICKNESS_PIXELS,
OVERLAY_OCCLUSION_HIDE );
}
static void DrawBoundsFcn( b3AABB aabb, b3HexColor color, void* context )
{
(void)context;
b3Vec3 lower = aabb.lowerBound;
b3Vec3 upper = aabb.upperBound;
// The fat AABB is float world space, difference against the origin in double before the corners demote
b3Pos origin = GetDrawOrigin();
b3Vec3 lower = b3SubPos( b3ToPos( aabb.lowerBound ), origin );
b3Vec3 upper = b3SubPos( b3ToPos( aabb.upperBound ), origin );
Vec4 c = HexColorToVec4( color );
b3Vec3 c000 = { lower.x, lower.y, lower.z };
@ -839,11 +846,14 @@ static void DrawBoundsFcn( b3AABB aabb, b3HexColor color, void* context )
// Oriented box: 8 corners are transform * (+/-ex, +/-ey, +/-ez). 12 edges
// connect adjacent corners along each axis.
static void DrawBoxFcn( b3Vec3 extents, b3Transform transform, b3HexColor color, void* context )
static void DrawBoxFcn( b3Vec3 extents, b3WorldTransform transform, b3HexColor color, void* context )
{
(void)context;
Vec4 c = HexColorToVec4( color );
// Shift into the camera relative frame, then transform the local corners
b3Transform local = b3ToRelativeTransform( transform, GetDrawOrigin() );
// Local-space corners (8 = 2^3 sign combinations along each axis).
float signs[2] = { -1.0f, 1.0f };
b3Vec3 corners[8];
@ -856,7 +866,7 @@ static void DrawBoxFcn( b3Vec3 extents, b3Transform transform, b3HexColor color,
float lx = signs[xi] * extents.x;
float ly = signs[yi] * extents.y;
float lz = signs[zi] * extents.z;
corners[( xi << 2 ) | ( yi << 1 ) | zi] = b3TransformPoint( transform, (b3Vec3){ lx, ly, lz } );
corners[( xi << 2 ) | ( yi << 1 ) | zi] = b3TransformPoint( local, (b3Vec3){ lx, ly, lz } );
}
}
}
@ -879,14 +889,14 @@ static void DrawBoxFcn( b3Vec3 extents, b3Transform transform, b3HexColor color,
}
}
static void DrawStringFcn( b3Vec3 p, const char* s, b3HexColor color, void* context )
static void DrawStringFcn( b3Pos p, const char* s, b3HexColor color, void* context )
{
(void)context;
if ( s == NULL )
{
return;
}
DrawString( p, HexColorToVec4( color ), s );
DrawString( b3SubPos( p, GetDrawOrigin() ), HexColorToVec4( color ), s );
}
void AttachToWorldDef( b3WorldDef* def )