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,9 +1,8 @@
#include "camera.h"
#include "gfx/picking.h"
#include "sokol_app.h"
#include "box3d/math_functions.h"
#include "sokol_app.h"
#include <math.h>
@ -52,14 +51,15 @@ static void RebuildBasisAndView( Camera& c )
const b3Vec3 upUnnorm = b3Sub( worldUp, b3MulSV( b3Dot( worldUp, forward ), forward ) );
const b3Vec3 up = b3Normalize( upUnnorm );
const b3Vec3 right = b3Normalize( b3Cross( up, forward ) );
const b3Vec3 position = { c.m_pivot.x + forward.x * c.m_radius, c.m_pivot.y + forward.y * c.m_radius,
c.m_pivot.z + forward.z * c.m_radius };
c.m_position = position;
// Eye in world space is the draw origin. Building the view in that relative frame puts the eye
// at the origin, so the view matrix carries no translation and stays exact far from the origin.
c.m_worldEye = b3OffsetPos( c.m_pivot, b3MulSV( c.m_radius, forward ) );
c.m_position = b3Vec3_zero;
c.m_right = right;
c.m_up = up;
c.m_forward = forward;
MakeViewAndInverse( &c.m_view, &c.m_viewInv, right, up, forward, position );
MakeViewAndInverse( &c.m_view, &c.m_viewInv, right, up, forward, c.m_position );
}
Camera::Camera()
@ -75,6 +75,7 @@ Camera::Camera()
, m_height( 0 )
, m_thirdPerson( false )
, m_position{ 0.0f, 0.0f, 0.0f }
, m_worldEye{ 0.0f, 0.0f, 0.0f }
, m_right{ 1.0f, 0.0f, 0.0f }
, m_up{ 0.0f, 1.0f, 0.0f }
, m_forward{ 0.0f, 0.0f, 1.0f }
@ -103,7 +104,7 @@ void Camera::Frame( b3AABB aabb, float aspect, float padding )
b3Vec3 center = b3AABB_Center( aabb );
b3Vec3 ext = b3AABB_Extents( aabb ); // half-extents
float r = sqrtf( ext.x * ext.x + ext.y * ext.y + ext.z * ext.z );
m_pivot = center;
m_pivot = b3ToPos( center );
if ( r < 1.0e-6f )
{
// Point-like; preserve current radius, just retarget. The cached
@ -140,10 +141,8 @@ void Camera::SetOrbit( float yawRadians, float pitchRadians, float radius )
RebuildBasisAndView( *this );
}
void Camera::SetView( float yawDegrees, float pitchDegrees, float radius, b3Vec3 pivot )
void Camera::SetView( float yawDegrees, float pitchDegrees, float radius, b3Pos pivot )
{
// Box3D feeds degrees; render3d stores radians. Pivot first so SetOrbit's
// basis rebuild uses the new pivot.
SetPivot( pivot );
SetOrbit( yawDegrees * DEG_TO_RAD, pitchDegrees * DEG_TO_RAD, radius );
}
@ -160,14 +159,47 @@ b3Vec3 Camera::Position() const
PickRay Camera::BuildPickRay( float x, float y ) const
{
// Defined zero ray so callers never read uninitialized values when the
// camera has no size yet or the unprojection is degenerate.
PickRay ray;
if ( !::BuildPickRay( x, y, &ray.origin, &ray.translation ) )
ray.origin = b3Pos_zero;
ray.translation = b3Vec3_zero;
if ( m_width <= 0 || m_height <= 0 )
{
// Camera state not latched yet, or degenerate matrices: defined zero
// ray so callers never read uninitialized values.
ray.origin = m_position;
ray.translation = b3Vec3_zero;
return ray;
}
// Pixel -> NDC. Origin top-left, Y down. NDC has Y up, X right.
const float ndcX = ( 2.0f * x / (float)m_width ) - 1.0f;
const float ndcY = 1.0f - ( 2.0f * y / (float)m_height );
// Reverse-Z: near maps to clip-Z = +w, far to 0. Unproject both clip points
// through inv(P*V) = viewInv * projInv, no runtime matrix inversion. The
// matrices are eye-relative, so the recovered points are too.
const Mat4 invVP = MulMM4( m_viewInv, m_projInv );
const Vec4 clipN = MakeVec4( ndcX, ndcY, 1.0f, 1.0f );
const Vec4 clipF = MakeVec4( ndcX, ndcY, 0.0f, 1.0f );
const Vec4 wN = MulMV4( invVP, clipN );
const Vec4 wF = MulMV4( invVP, clipF );
if ( wN.w == 0.0f || wF.w == 0.0f )
{
return ray;
}
const float invWN = 1.0f / wN.w;
const float invWF = 1.0f / wF.w;
b3Vec3 nearWorld = { wN.x * invWN, wN.y * invWN, wN.z * invWN };
b3Vec3 farWorld = { wF.x * invWF, wF.y * invWF, wF.z * invWF };
// Lift the near point to absolute world with the double eye. Translation is
// a delta so it stays eye-relative and needs no offset.
ray.origin = b3OffsetPos( m_worldEye, nearWorld );
ray.translation = b3Sub( farWorld, nearWorld );
return ray;
}
@ -321,13 +353,12 @@ void Camera::Update( float dt, int width, int height )
{
// Snapshot eye BEFORE rotating so yaw/pitch pivot around the eye
// (FPS) instead of around m_pivot (orbit). After rotation we
// back-derive m_pivot from this preserved eye so Position()
// returns the same point regardless of look angle.
const b3Vec3 eyeBefore = Position();
// back-derive m_pivot from this preserved eye so the eye stays put
// regardless of look angle.
const b3Pos eyeBefore = b3OffsetPos( m_pivot, b3MulSV( m_radius, ForwardFromAngles( m_yaw, m_pitch ) ) );
// FPS look: drag right -> yaw decreases (turn head right, scene
// shifts left); drag down -> pitch increases (look down). Box3D's
// signs - opposite of render3d's orbit pitch sign on purpose.
// shifts left); drag down -> pitch increases (look down).
if ( m_orbitDX != 0.0f || m_orbitDY != 0.0f )
{
m_yaw -= m_orbitDX * FLY_LOOK_SENS;
@ -351,7 +382,7 @@ void Camera::Update( float dt, int width, int height )
if ( m_aDown )
wasdR -= 1.0f;
b3Vec3 eye = eyeBefore;
b3Pos eye = eyeBefore;
if ( wasdF != 0.0f || wasdR != 0.0f )
{
// Right = normalize(worldUp x forward), matching Box3D's