Optimizations (#161)

- cache body sim indices and split out fat aabb
- deferred restitution
- the manifold point normal velocity is only computed for restitution or
hit events, otherwise it is zero
This commit is contained in:
Erin Catto 2026-09-20 15:54:52 -07:00 • committed by GitHub
commit 9e5a4cde86
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
49 changed files with 3395 additions and 864 deletions

View file

@ -4,6 +4,7 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
// Compile-time options. Edit box3d/config.h, or define BOX3D_USER_CONFIG to
@ -93,11 +94,12 @@
/// Prototype for user allocation function.
/// @param size the allocation size in bytes
/// @param alignment the required alignment, guaranteed to be a power of 2
typedef void* b3AllocFcn( int32_t size, int32_t alignment );
typedef void* b3AllocFcn( size_t size, int32_t alignment );
/// Prototype for user free function.
/// @param mem the memory previously allocated through `b3AllocFcn`
typedef void b3FreeFcn( void* mem );
/// @param size the size of the allocation in bytes.
typedef void b3FreeFcn( void* mem, size_t size );
/// Prototype for the user assert callback. Return 0 to skip the debugger break.
typedef int b3AssertFcn( const char* condition, const char* fileName, int lineNumber );
@ -110,7 +112,7 @@ typedef void b3LogFcn( const char* message );
B3_API void b3SetAllocator( b3AllocFcn* allocFcn, b3FreeFcn* freeFcn );
/// Total bytes allocated by Box3D
B3_API int b3GetByteCount( void );
B3_API int64_t b3GetByteCount( void );
/// Override the default assert callback.
/// @param assertFcn a non-null assert callback

View file

@ -31,5 +31,3 @@
// Enable assertions when NDEBUG is defined
// #define B3_ENABLE_ASSERT
// #define B3_RESTITUTION_ITERATIONS

View file

@ -141,11 +141,6 @@ B3_API float b3GetStallThreshold( void );
#define B3_MAX_SHAPES ( 1 << B3_SHAPE_POWER )
#define B3_MAX_CHILD_SHAPES ( 1 << B3_CHILD_POWER )
/// Increase this if your application needs more accurate restitution. Doing so will
/// slow down the simulation. Must be 1 or more.
#ifndef B3_RESTITUTION_ITERATIONS
#define B3_RESTITUTION_ITERATIONS 1
#endif
/// This is the limit on how many mesh or heightfield triangles a single convex shape can collide with.
/// Increasing this will increase stack usage, so be careful. I recommend to simplify your collision data

View file

@ -537,69 +537,70 @@ typedef struct b3ShapeDef
/// @ingroup shape
B3_API b3ShapeDef b3DefaultShapeDef( void );
//! @cond
/// Profiling data. Times are in milliseconds.
/// @ingroup world
typedef struct b3Profile
{
float step;
float pairs;
float collide;
float solve;
float solverSetup;
float constraints;
float prepareConstraints;
float integrateVelocities;
float warmStart;
float solveImpulses;
float integratePositions;
float relaxImpulses;
float applyRestitution;
float storeImpulses;
float splitIslands;
float transforms;
float sensorHits;
float jointEvents;
float hitEvents;
float refit;
float bullets;
float sleepIslands;
float sensors;
float step; //
float pairs; //
float collide; //
float solve; //
float solverSetup; //
float constraints; //
float prepareConstraints; //
float integrateVelocities; //
float warmStart; //
float solveImpulses; //
float integratePositions; //
float relaxImpulses; //
float storeImpulses; //
float splitIslands; //
float transforms; //
float sensorHits; //
float jointEvents; //
float hitEvents; //
float refit; //
float bullets; //
float sleepIslands; //
float sensors; //
} b3Profile;
/// Counters that give details of the simulation size.
/// @ingroup world
typedef struct b3Counters
{
int bodyCount;
int shapeCount;
int contactCount;
int jointCount;
int islandCount;
int stackUsed;
int arenaCapacity;
int staticTreeHeight;
int treeHeight;
int satCallCount;
int satCacheHitCount;
int byteCount;
int taskCount;
int colorCounts[24];
int manifoldCounts[B3_CONTACT_MANIFOLD_COUNT_BUCKETS];
int64_t byteCount; //
int bodyCount; //
int shapeCount; //
int contactCount; //
int jointCount; //
int islandCount; //
int stackUsed; //
int arenaCapacity; //
int staticTreeHeight; //
int treeHeight; //
int satCallCount; //
int satCacheHitCount; //
int taskCount; //
int colorCounts[24]; //
int manifoldCounts[B3_CONTACT_MANIFOLD_COUNT_BUCKETS]; //
/// Number of contacts touched by the collide pass
/// graph contacts + awake-set non-touching
int awakeContactCount;
int awakeContactCount; //
/// Number of contacts recycled in the most recent step.
int recycledContactCount;
/// Maximum number of time of impact iterations
/// Maximum number of time of impact outer iterations
int distanceIterations;
/// Maximum TOI push backs.
int pushBackIterations;
/// Maximum TOI root solver iterations.
int rootIterations;
} b3Counters;
//! @endcond
/// Joint type enumeration. This is useful because all joint types use b3JointId and sometimes you
/// want to get the type of a joint.
@ -2651,12 +2652,14 @@ typedef struct b3ManifoldPoint
/// result from the final sub-step.
float normalImpulse;
/// The total normal impulse applied during sub-stepping. This is important
/// The total normal impulse applied across sub-stepping and restitution. This is important
/// to identify speculative contact points that had an interaction in the time step.
/// This includes the warm starting impulse, the sub-step delta impulse, and the restitution
/// impulse.
float totalNormalImpulse;
/// Relative normal velocity pre-solve. Used for hit events. If the normal impulse is
/// zero then there was no hit. Negative means shapes are approaching.
/// Relative normal velocity pre-solve. Negative when approaching. This is only
/// computed if hit events are enabled.
float normalVelocity;
/// Local point for matching