Manifold allocation cap (#227)

Degenerate scenarios can lead to a huge number of manifolds per contact.
This creates memory and performance problems. So I'm capping them to 8
using cheap heuristics for culling.
This commit is contained in:
Erin Catto 2026-11-17 13:51:59 -08:00 • committed by GitHub
commit 8f3b2e12eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 1161 additions and 138 deletions

View file

@ -8,9 +8,19 @@
#include <float.h>
#include <math.h>
#define WAVE_COUNT 40
#define WAVE_CELL_WIDTH 1.0f
#define WAVE_ROW_FREQUENCY 0.1f
#define WAVE_COLUMN_FREQUENCY 0.2f
#define WAVE_AMPLITUDE 0.5f
// Bumps about 20 cm across, close to the curvature of the bodies, on cells a few times smaller. The cell
// width is a power of two so the mesh builder steps to the same vertices the depth tracker computes.
#define ROUGH_WAVE_COUNT 640
#define ROUGH_CELL_WIDTH 0.03125f
#define ROUGH_ROW_FREQUENCY 4.7f
#define ROUGH_COLUMN_FREQUENCY 5.9f
#define ROUGH_AMPLITUDE 0.015f
#define DROP_SPACING 0.5f
#define DROP_HEIGHT 5.0f
#define BODY_RADIUS 0.05f
@ -53,6 +63,27 @@ static uint32_t HashSeed( uint32_t x )
return x != 0 ? x : 1;
}
typedef struct WaveGrid
{
int count;
float cellWidth;
float rowFrequency;
float columnFrequency;
} WaveGrid;
static WaveGrid GetWaveGrid( bool rough )
{
_Static_assert( ROUGH_WAVE_COUNT <= MESH_DROP_MAX_WAVE_COUNT && WAVE_COUNT <= MESH_DROP_MAX_WAVE_COUNT,
"wave count exceeds the depth tracker tables" );
if ( rough )
{
return (WaveGrid){ ROUGH_WAVE_COUNT, ROUGH_CELL_WIDTH, ROUGH_ROW_FREQUENCY, ROUGH_COLUMN_FREQUENCY };
}
return (WaveGrid){ WAVE_COUNT, WAVE_CELL_WIDTH, WAVE_ROW_FREQUENCY, WAVE_COLUMN_FREQUENCY };
}
static b3BoxHull MakeDropBox( void )
{
// The soft contact solver cannot always resolve a smaller box at 4 sub-steps, and its size gets
@ -64,13 +95,19 @@ MeshDropSettings GetDefaultMeshDropSettings( void )
{
MeshDropSettings settings = {
.shape = e_meshDropBox,
.amplitude = 0.5f,
.amplitude = WAVE_AMPLITUDE,
.rough = false,
.collide = false,
.soloIndex = -1,
};
return settings;
}
float GetMeshDropDefaultAmplitude( bool rough )
{
return rough ? ROUGH_AMPLITUDE : WAVE_AMPLITUDE;
}
uint32_t GetMeshDropSeed( uint32_t baseSeed, int generation )
{
return HashSeed( HashSeed( baseSeed ) + (uint32_t)generation );
@ -86,10 +123,11 @@ float GetMeshDropTunnelHeight( void )
return -2.0f;
}
b3MeshData* CreateMeshDropMesh( float amplitude )
b3MeshData* CreateMeshDropMesh( MeshDropSettings settings )
{
return b3CreateWaveMesh( MESH_DROP_WAVE_COUNT, MESH_DROP_WAVE_COUNT, WAVE_CELL_WIDTH, amplitude, WAVE_ROW_FREQUENCY,
WAVE_COLUMN_FREQUENCY );
WaveGrid grid = GetWaveGrid( settings.rough );
return b3CreateWaveMesh( grid.count, grid.count, grid.cellWidth, settings.amplitude, grid.rowFrequency,
grid.columnFrequency );
}
b3HullData* CreateMeshDropCylinder( void )
@ -97,7 +135,7 @@ b3HullData* CreateMeshDropCylinder( void )
return b3CreateCylinder( 0.4f, BODY_RADIUS, 0.0f, 6 );
}
b3BodyId CreateMeshDropGround( b3WorldId worldId, const b3MeshData* mesh )
b3BodyId CreateMeshDropGround( b3WorldId worldId, const b3MeshData* mesh, MeshDropSettings settings )
{
b3BodyDef bodyDef = b3DefaultBodyDef();
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
@ -106,7 +144,8 @@ b3BodyId CreateMeshDropGround( b3WorldId worldId, const b3MeshData* mesh )
shapeDef.filter.categoryBits = 1;
b3CreateMeshShape( groundId, &shapeDef, mesh, b3Vec3_one );
float extent = 0.5f * MESH_DROP_WAVE_COUNT * WAVE_CELL_WIDTH;
WaveGrid grid = GetWaveGrid( settings.rough );
float extent = 0.5f * grid.count * grid.cellWidth;
float halfHeight = 1.0f;
b3Transform transforms[4] = {
@ -202,19 +241,18 @@ int GetMeshDropBodyIndex( void* userData )
void ResetMeshDropDepth( MeshDropDepth* depth, MeshDropSettings settings, const b3HullData* cylinder )
{
// Same heights as b3CreateWaveMesh
float omegaZ = 2.0f * B3_PI * WAVE_ROW_FREQUENCY * WAVE_CELL_WIDTH;
float omegaX = 2.0f * B3_PI * WAVE_COLUMN_FREQUENCY * WAVE_CELL_WIDTH;
int index = 0;
for ( int ix = 0; ix <= MESH_DROP_WAVE_COUNT; ++ix )
WaveGrid grid = GetWaveGrid( settings.rough );
float omegaZ = 2.0f * B3_PI * grid.rowFrequency * grid.cellWidth;
float omegaX = 2.0f * B3_PI * grid.columnFrequency * grid.cellWidth;
for ( int i = 0; i <= grid.count; ++i )
{
float rowHeight = b3ComputeCosSin( omegaX * ix ).sine;
for ( int iz = 0; iz <= MESH_DROP_WAVE_COUNT; ++iz )
{
float columnHeight = b3ComputeCosSin( omegaZ * iz ).sine;
depth->heights[index++] = settings.amplitude * rowHeight * columnHeight;
}
depth->rowHeights[i] = b3ComputeCosSin( omegaX * i ).sine;
depth->columnHeights[i] = b3ComputeCosSin( omegaZ * i ).sine;
}
depth->waveCount = grid.count;
depth->cellWidth = grid.cellWidth;
depth->waveAmplitude = settings.amplitude;
depth->amplitude = fabsf( settings.amplitude );
depth->probeCount = 0;
depth->radius = 0.0f;
@ -272,20 +310,20 @@ void ResetMeshDropDepth( MeshDropDepth* depth, MeshDropSettings settings, const
// it reads a little deep on a crest and shallow in a valley.
static float GetWaveDistance( const MeshDropDepth* depth, b3Vec3 p )
{
float origin = -0.5f * MESH_DROP_WAVE_COUNT * WAVE_CELL_WIDTH;
float u = ( p.x - origin ) / WAVE_CELL_WIDTH;
float v = ( p.z - origin ) / WAVE_CELL_WIDTH;
int ix = b3ClampInt( (int)floorf( u ), 0, MESH_DROP_WAVE_COUNT - 1 );
int iz = b3ClampInt( (int)floorf( v ), 0, MESH_DROP_WAVE_COUNT - 1 );
float w = depth->cellWidth;
float origin = -0.5f * depth->waveCount * w;
float u = ( p.x - origin ) / w;
float v = ( p.z - origin ) / w;
int ix = b3ClampInt( (int)floorf( u ), 0, depth->waveCount - 1 );
int iz = b3ClampInt( (int)floorf( v ), 0, depth->waveCount - 1 );
int stride = MESH_DROP_WAVE_COUNT + 1;
float ha = depth->heights[ix * stride + iz];
float hb = depth->heights[ix * stride + iz + 1];
float hc = depth->heights[( ix + 1 ) * stride + iz + 1];
float hd = depth->heights[( ix + 1 ) * stride + iz];
float amplitude = depth->waveAmplitude;
float ha = amplitude * depth->rowHeights[ix] * depth->columnHeights[iz];
float hb = amplitude * depth->rowHeights[ix] * depth->columnHeights[iz + 1];
float hc = amplitude * depth->rowHeights[ix + 1] * depth->columnHeights[iz + 1];
float hd = amplitude * depth->rowHeights[ix + 1] * depth->columnHeights[iz];
// The cell diagonal runs from a to c, matching b3CreateWaveMesh
float w = WAVE_CELL_WIDTH;
b3Vec3 a = { origin + ix * w, ha, origin + iz * w };
b3Vec3 ac = { w, hc - ha, w };
b3Vec3 side = v - iz >= u - ix ? (b3Vec3){ 0.0f, hb - ha, w } : (b3Vec3){ w, hd - ha, 0.0f };

View file

@ -17,7 +17,8 @@ extern "C"
// Mesh Drop scene, shared by the Continuous / Mesh Drop sample and the headless mesh soak. A generation
// drops a grid of small bodies with random velocities on a wave mesh and steps until they sleep. A body
// that falls out of the world tunneled. The depth tracker measures how far each body sinks into the
// wave, which flags contact trouble far more often than tunnels do.
// wave, which flags contact trouble far more often than tunnels do. The rough wave has bumps smaller than
// the bodies, so a body touches many triangles with different normals.
typedef enum MeshDropShape
{
@ -28,12 +29,19 @@ typedef enum MeshDropShape
e_meshDropShapeCount
} MeshDropShape;
// Bodies per axis. A debug build drops a smaller patch at the same spacing so the samples app stays
// responsive. The soak tools run in release and keep the full grid.
#ifdef NDEBUG
#define MESH_DROP_GRID_COUNT 32
#else
#define MESH_DROP_GRID_COUNT 16
#endif
#define MESH_DROP_BODY_COUNT ( MESH_DROP_GRID_COUNT * MESH_DROP_GRID_COUNT )
#define MESH_DROP_DEPTH_LEVEL_COUNT 4
// Wave mesh cells along x and z
#define MESH_DROP_WAVE_COUNT 40
// Most wave mesh cells along x and z, reached by the rough wave
#define MESH_DROP_MAX_WAVE_COUNT 640
// Enough for the cylinder hull
#define MESH_DROP_MAX_PROBES 16
@ -48,6 +56,9 @@ typedef struct MeshDropSettings
MeshDropShape shape;
float amplitude;
// Fine bumps instead of the broad wave. The amplitude should be a few centimeters.
bool rough;
// Let the bodies collide with each other. Off isolates mesh collision.
bool collide;
@ -57,6 +68,8 @@ typedef struct MeshDropSettings
MeshDropSettings GetDefaultMeshDropSettings( void );
float GetMeshDropDefaultAmplitude( bool rough );
// Hashes the base before adding the generation, so nearby base seeds share no generations
uint32_t GetMeshDropSeed( uint32_t baseSeed, int generation );
@ -66,11 +79,11 @@ int GetMeshDropStepLimit( MeshDropShape shape );
// A body below this height tunneled
float GetMeshDropTunnelHeight( void );
b3MeshData* CreateMeshDropMesh( float amplitude );
b3MeshData* CreateMeshDropMesh( MeshDropSettings settings );
b3HullData* CreateMeshDropCylinder( void );
// Creates a static body with the wave mesh and four walls
b3BodyId CreateMeshDropGround( b3WorldId worldId, const b3MeshData* mesh );
b3BodyId CreateMeshDropGround( b3WorldId worldId, const b3MeshData* mesh, MeshDropSettings settings );
// Creates the bodies of one generation in grid order. Bodies left out by the solo index are null. The
// body user data encodes the grid index, see GetMeshDropBodyIndex.
@ -82,8 +95,13 @@ int GetMeshDropBodyIndex( void* userData );
typedef struct MeshDropDepth
{
// Wave heights at the mesh vertices
float heights[( MESH_DROP_WAVE_COUNT + 1 ) * ( MESH_DROP_WAVE_COUNT + 1 )];
// Wave height factors at the mesh vertices along x and z. A vertex height is their product times
// the wave amplitude.
float rowHeights[MESH_DROP_MAX_WAVE_COUNT + 1];
float columnHeights[MESH_DROP_MAX_WAVE_COUNT + 1];
int waveCount;
float cellWidth;
float waveAmplitude;
// Collision points in the body frame, rounded by the radius
b3Vec3 probes[MESH_DROP_MAX_PROBES];