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

@ -67,6 +67,33 @@ typedef struct b3Transform
b3Quat q;
} b3Transform;
#if defined( BOX3D_DOUBLE_PRECISION )
/// A world position. Double precision in large world mode so coordinates stay accurate far
/// from the origin.
typedef struct b3Pos
{
double x, y, z;
} b3Pos;
/// A world transform with double precision translation and float quaternion rotation. Rotation
/// is frame local and never needs the extra range, the same split as Jolt's DMat44.
typedef struct b3WorldTransform
{
b3Pos p;
b3Quat q;
} b3WorldTransform;
#else
/// In single precision mode these types are the same.
typedef b3Vec3 b3Pos;
/// In single precision mode these types are the same.
typedef b3Transform b3WorldTransform;
#endif
/// A 3x3 matrix.
typedef struct b3Matrix3
{
@ -106,6 +133,10 @@ static const b3Matrix3 b3Mat3_identity = {
{ 0.0f, 0.0f, 1.0f },
};
// Valid in both modes: 0.0f promotes to double, the identity rotation stays float
static const b3Pos b3Pos_zero = { 0.0f, 0.0f, 0.0f };
static const b3WorldTransform b3WorldTransform_identity = { { 0.0f, 0.0f, 0.0f }, { { 0.0f, 0.0f, 0.0f }, 1.0f } };
/// @return the minimum of two integers.
B3_INLINE int b3MinInt( int a, int b )
{
@ -591,6 +622,136 @@ B3_INLINE b3Vec3 b3InvTransformPoint( b3Transform t, b3Vec3 v )
return b3InvRotateVector( t.q, b3Sub( v, t.p ) );
}
// World position boundary. These cross between the double precision world space at the public
// boundary and the float interior. One set of bodies serves both modes: the typedefs collapse
// the types in float mode and the explicit float casts become no-ops.
/// Convert a vector to a world position.
B3_INLINE b3Pos b3ToPos( b3Vec3 v )
{
return B3_LITERAL( b3Pos ){ v.x, v.y, v.z };
}
/// Lossy conversion of a world position to a float vector.
B3_INLINE b3Vec3 b3ToVec3( b3Pos p )
{
return B3_LITERAL( b3Vec3 ){ (float)p.x, (float)p.y, (float)p.z };
}
/// Narrow a world coordinate to float, rounding toward negative infinity. Use with
/// b3RoundUpFloat to build a conservative float box that always contains the double bounds,
/// where plain rounding far from the origin could clip. nextafterf is an exact IEEE operation,
/// so this is cross-platform deterministic. With large world mode off this is a plain conversion.
B3_INLINE float b3RoundDownFloat( double x )
{
#if defined( BOX3D_DOUBLE_PRECISION )
float f = (float)x;
return (double)f > x ? nextafterf( f, -FLT_MAX ) : f;
#else
return (float)x;
#endif
}
/// Narrow a world coordinate to float, rounding toward positive infinity.
B3_INLINE float b3RoundUpFloat( double x )
{
#if defined( BOX3D_DOUBLE_PRECISION )
float f = (float)x;
return (double)f < x ? nextafterf( f, FLT_MAX ) : f;
#else
return (float)x;
#endif
}
/// a - b, demoted to float. The primary precision boundary operation.
B3_INLINE b3Vec3 b3SubPos( b3Pos a, b3Pos b )
{
return B3_LITERAL( b3Vec3 ){ (float)( a.x - b.x ), (float)( a.y - b.y ), (float)( a.z - b.z ) };
}
/// p + d
B3_INLINE b3Pos b3OffsetPos( b3Pos p, b3Vec3 d )
{
return B3_LITERAL( b3Pos ){ p.x + d.x, p.y + d.y, p.z + d.z };
}
/// World position interpolation for sweeps and sampling.
B3_INLINE b3Pos b3LerpPosition( b3Pos a, b3Pos b, float t )
{
return B3_LITERAL( b3Pos ){
( 1.0f - t ) * a.x + t * b.x,
( 1.0f - t ) * a.y + t * b.y,
( 1.0f - t ) * a.z + t * b.z,
};
}
/// Transform a local point to a world position. Rotation in float, translation in double.
B3_INLINE b3Pos b3TransformWorldPoint( b3WorldTransform t, b3Vec3 p )
{
b3Vec3 r = b3RotateVector( t.q, p );
return B3_LITERAL( b3Pos ){ t.p.x + r.x, t.p.y + r.y, t.p.z + r.z };
}
/// Transform a world position to a local point. One double subtraction, then float.
B3_INLINE b3Vec3 b3InvTransformWorldPoint( b3WorldTransform t, b3Pos p )
{
b3Vec3 d = { (float)( p.x - t.p.x ), (float)( p.y - t.p.y ), (float)( p.z - t.p.z ) };
return b3InvRotateVector( t.q, d );
}
/// Relative transform of frame B in frame A. The narrow phase boundary.
B3_INLINE b3Transform b3InvMulWorldTransforms( b3WorldTransform A, b3WorldTransform B )
{
b3Transform C;
C.q = b3InvMulQuat( A.q, B.q );
b3Vec3 d = { (float)( B.p.x - A.p.x ), (float)( B.p.y - A.p.y ), (float)( B.p.z - A.p.z ) };
C.p = b3InvRotateVector( A.q, d );
return C;
}
/// Compose a world transform with a local transform.
B3_INLINE b3WorldTransform b3MulWorldTransforms( b3WorldTransform A, b3Transform B )
{
b3WorldTransform C;
C.q = b3MulQuat( A.q, B.q );
b3Vec3 r = b3RotateVector( A.q, B.p );
C.p = B3_LITERAL( b3Pos ){ A.p.x + r.x, A.p.y + r.y, A.p.z + r.z };
return C;
}
/// Shift a world transform into the frame of a base position.
B3_INLINE b3Transform b3ToRelativeTransform( b3WorldTransform t, b3Pos base )
{
b3Transform r;
r.q = t.q;
r.p = B3_LITERAL( b3Vec3 ){ (float)( t.p.x - base.x ), (float)( t.p.y - base.y ), (float)( t.p.z - base.z ) };
return r;
}
/// Promote a float transform to a world transform. Lossless.
B3_INLINE b3WorldTransform b3MakeWorldTransform( b3Transform t )
{
b3WorldTransform w;
w.p = b3ToPos( t.p );
w.q = t.q;
return w;
}
/// Translate a local AABB by a world origin, rounding outward so the float box always contains
/// the double box. Far from the origin a plain conversion could clip a shape out of its own box.
/// In float mode the origin is float and the rounding is a no-op.
B3_INLINE b3AABB b3OffsetAABB( b3AABB localBox, b3Pos origin )
{
b3AABB out;
out.lowerBound.x = b3RoundDownFloat( origin.x + localBox.lowerBound.x );
out.lowerBound.y = b3RoundDownFloat( origin.y + localBox.lowerBound.y );
out.lowerBound.z = b3RoundDownFloat( origin.z + localBox.lowerBound.z );
out.upperBound.x = b3RoundUpFloat( origin.x + localBox.upperBound.x );
out.upperBound.y = b3RoundUpFloat( origin.y + localBox.upperBound.y );
out.upperBound.z = b3RoundUpFloat( origin.z + localBox.upperBound.z );
return out;
}
/// Compute the determinant of a 3-by-3 matrix.
B3_INLINE float b3Det( b3Matrix3 m )
{
@ -890,12 +1051,21 @@ B3_API bool b3IsValidMatrix3( b3Matrix3 a );
/// Is this a valid bounding box? Not Nan or infinity. Upper bound greater than or equal to lower bound.
B3_API bool b3IsValidAABB( b3AABB a );
/// Is this AABB valid and not huge or very far from the origin?
/// Is this AABB reasonably close to the origin? See B3_HUGE.
B3_API bool b3IsBoundedAABB( b3AABB a );
/// Is this AABB valid and reasonable?
B3_API bool b3IsSaneAABB( b3AABB a );
/// Is this a valid plane? Normal is a unit vector. Not Nan or infinity.
B3_API bool b3IsValidPlane( b3Plane a );
/// Is this a valid world position? Not NaN or infinity.
B3_API bool b3IsValidPosition( b3Pos p );
/// Is this a valid world transform? Not NaN or infinity. Rotation is normalized.
B3_API bool b3IsValidWorldTransform( b3WorldTransform t );
/**@}*/ // math
/**
@ -971,6 +1141,28 @@ B3_FORCE_INLINE b3Vec3 operator-( b3Vec3 a, b3Vec3 b )
return { a.x - b.x, a.y - b.y, a.z - b.z };
}
#if defined( BOX3D_DOUBLE_PRECISION )
/// Offset a world position by a vector.
B3_FORCE_INLINE b3Pos operator+( b3Pos a, b3Vec3 b )
{
return { a.x + b.x, a.y + b.y, a.z + b.z };
}
/// Offset a world position by a vector.
B3_FORCE_INLINE b3Pos operator-( b3Pos a, b3Vec3 b )
{
return { a.x - b.x, a.y - b.y, a.z - b.z };
}
/// Delta between two world positions, demoted to float.
B3_FORCE_INLINE b3Vec3 operator-( b3Pos a, b3Pos b )
{
return { (float)( a.x - b.x ), (float)( a.y - b.y ), (float)( a.z - b.z ) };
}
#endif
#endif
/**@}*/ // math_cpp