Reduce ghosts (#212)

Full ghost collision handling for sphere, capsule, and hull versus a
single mesh or heightfield. This requires mesh to be built with edge
identification.

Optimized mesh builder.

Removed B3_MAX_MESH_CONTACT_TRIANGLES. You will still hit performance
problems for dense meshes. Fixes #184
This commit is contained in:
Erin Catto 2026-11-10 21:53:10 -08:00 • committed by GitHub
commit 38f2ae1e19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
61 changed files with 17756 additions and 1492 deletions

View file

@ -19,27 +19,27 @@
// Double precision accumulates body positions in double, so the settle/sleep step and the
// state hash differ from the float build. Both modes are internally deterministic.
#if defined( BOX3D_DOUBLE_PRECISION )
#define RAGDOLL_SLEEP_STEP 263
#define RAGDOLL_HASH 0xA5423F98
#define WAVE_PILE_SLEEP_STEP 272
#define WAVE_PILE_HASH 0xC8E5F3D6
#define RAGDOLL_SLEEP_STEP 282
#define RAGDOLL_HASH 0x6E4B13AD
#define WAVE_PILE_SLEEP_STEP 315
#define WAVE_PILE_HASH 0x15007937
#define QUERY_SPAWN_SLEEP_STEP 242
#define QUERY_SPAWN_HASH 0x489585E6
#define QUERY_SPAWN_HIT_COUNT 59
#define QUERY_SPAWN_QUERY_HASH 0x5B4429DC
#define MESH_DROP_SLEEP_STEP 218
#define MESH_DROP_HASH 0xD378CF04
#define MESH_DROP_SLEEP_STEP 197
#define MESH_DROP_HASH 0x5DA091C3
#else
#define RAGDOLL_SLEEP_STEP 291
#define RAGDOLL_HASH 0xFF4C605B
#define WAVE_PILE_SLEEP_STEP 272
#define WAVE_PILE_HASH 0x266F83B0
#define RAGDOLL_SLEEP_STEP 277
#define RAGDOLL_HASH 0x5E042B6E
#define WAVE_PILE_SLEEP_STEP 455
#define WAVE_PILE_HASH 0x854F313D
#define QUERY_SPAWN_SLEEP_STEP 242
#define QUERY_SPAWN_HASH 0xF3AD9BA6
#define QUERY_SPAWN_HIT_COUNT 59
#define QUERY_SPAWN_QUERY_HASH 0xE3271F3D
#define MESH_DROP_SLEEP_STEP 226
#define MESH_DROP_HASH 0xD2FF069D
#define MESH_DROP_SLEEP_STEP 197
#define MESH_DROP_HASH 0x96D47D4F
#endif
// The goldens above pin exact values for the default four point manifold. A build that

View file

@ -11,6 +11,7 @@
#include "box3d/math_functions.h"
#include <float.h>
#include <math.h>
#include <stdio.h>
static int HeightFieldCreate( void )
@ -673,6 +674,146 @@ static int RayCastClockwiseWinding( void )
return 0;
}
#define BUMPY_COUNT_X 9
#define BUMPY_COUNT_Z 7
// A bumpy field with holes, so cells have every mix of interior, border and hole neighbors
static b3HeightFieldData* MakeBumpyField( bool clockwise )
{
float heights[BUMPY_COUNT_X * BUMPY_COUNT_Z];
for ( int i = 0; i < BUMPY_COUNT_Z; ++i )
{
for ( int j = 0; j < BUMPY_COUNT_X; ++j )
{
heights[i * BUMPY_COUNT_X + j] = sinf( 0.9f * (float)i ) * cosf( 0.7f * (float)j ) + 0.1f * (float)( ( i * j ) % 3 );
}
}
uint8_t materials[( BUMPY_COUNT_X - 1 ) * ( BUMPY_COUNT_Z - 1 )] = { 0 };
materials[9] = B3_HEIGHT_FIELD_HOLE;
materials[20] = B3_HEIGHT_FIELD_HOLE;
materials[47] = B3_HEIGHT_FIELD_HOLE;
b3HeightFieldDef def = { 0 };
def.heights = heights;
def.materialIndices = materials;
def.scale = (b3Vec3){ 1.5f, 0.8f, 0.5f };
def.countX = BUMPY_COUNT_X;
def.countZ = BUMPY_COUNT_Z;
def.globalMinimumHeight = -2.0f;
def.globalMaximumHeight = 2.0f;
def.clockwiseWinding = clockwise;
return b3CreateHeightField( &def );
}
static bool IsHole( const b3HeightFieldData* hf, int triangleIndex )
{
return b3GetHeightFieldMaterial( hf, triangleIndex ) == B3_HEIGHT_FIELD_HOLE;
}
static b3Vec3 TriangleNormal( b3Triangle triangle )
{
b3Vec3 e1 = b3Sub( triangle.vertices[1], triangle.vertices[0] );
b3Vec3 e2 = b3Sub( triangle.vertices[2], triangle.vertices[0] );
return b3Normalize( b3Cross( e1, e2 ) );
}
// Neighbors found by brute force over shared grid points must match the grid walk
static int HeightFieldNeighborNormals( void )
{
for ( int pass = 0; pass < 2; ++pass )
{
b3HeightFieldData* hf = MakeBumpyField( pass == 1 );
int triangleCount = b3GetHeightFieldTriangleCount( hf );
int interiorCount = 0;
int boundaryCount = 0;
for ( int i = 0; i < triangleCount; ++i )
{
if ( IsHole( hf, i ) )
{
continue;
}
b3Triangle triangle = b3GetHeightFieldTriangle( hf, i );
int indices[3] = { triangle.i1, triangle.i2, triangle.i3 };
for ( int k = 0; k < 3; ++k )
{
int a = indices[k];
int b = indices[( k + 1 ) % 3];
int neighbor = B3_NULL_INDEX;
for ( int j = 0; j < triangleCount && neighbor == B3_NULL_INDEX; ++j )
{
if ( j == i || IsHole( hf, j ) )
{
continue;
}
b3Triangle other = b3GetHeightFieldTriangle( hf, j );
int otherIndices[3] = { other.i1, other.i2, other.i3 };
for ( int m = 0; m < 3; ++m )
{
if ( otherIndices[m] == b && otherIndices[( m + 1 ) % 3] == a )
{
neighbor = j;
}
}
}
b3Vec3 normal;
bool found = b3GetHeightFieldNeighborNormal( hf, i, k, &normal );
ENSURE( found == ( neighbor != B3_NULL_INDEX ) );
if ( found == false )
{
boundaryCount += 1;
continue;
}
interiorCount += 1;
b3Vec3 expected = TriangleNormal( b3GetHeightFieldTriangle( hf, neighbor ) );
ENSURE( b3Dot( normal, expected ) > 0.99999f );
}
}
ENSURE( interiorCount > 0 );
ENSURE( boundaryCount > 0 );
b3DestroyHeightField( hf );
}
return 0;
}
// The shape level lookup dispatches to the mesh and height field versions
static int NeighborNormalDispatch( void )
{
b3HeightFieldData* hf = MakeBumpyField( false );
b3Shape heightShape = { 0 };
heightShape.type = b3_heightShape;
heightShape.heightField = hf;
b3Vec3 normal1, normal2;
ENSURE( b3GetNeighborNormal( &heightShape, 1, 1, &normal1 ) );
ENSURE( b3GetHeightFieldNeighborNormal( hf, 1, 1, &normal2 ) );
ENSURE( normal1.x == normal2.x && normal1.y == normal2.y && normal1.z == normal2.z );
b3DestroyHeightField( hf );
b3MeshData* data = b3CreateWaveMesh( 4, 4, 1.0f, 0.5f, 0.2f, 0.15f );
b3Shape meshShape = { 0 };
meshShape.type = b3_meshShape;
meshShape.mesh = (b3Mesh){ data, { 2.0f, 0.5f, -1.0f } };
bool found1 = b3GetNeighborNormal( &meshShape, 5, 1, &normal1 );
bool found2 = b3GetMeshNeighborNormal( &meshShape.mesh, 5, 1, &normal2 );
ENSURE( found1 && found2 );
ENSURE( normal1.x == normal2.x && normal1.y == normal2.y && normal1.z == normal2.z );
b3DestroyMesh( data );
return 0;
}
int HeightFieldTest( void )
{
RUN_SUBTEST( HeightFieldCreate );
@ -688,6 +829,8 @@ int HeightFieldTest( void )
RUN_SUBTEST( ShapeCastClockwiseWinding );
RUN_SUBTEST( RayCastBackside );
RUN_SUBTEST( RayCastClockwiseWinding );
RUN_SUBTEST( HeightFieldNeighborNormals );
RUN_SUBTEST( NeighborNormalDispatch );
return 0;
}

View file

@ -580,7 +580,7 @@ static int Hull2DSquareTest( void )
b3Point2D hull[16];
FillPoints2D( pts, input, 8 );
int count = b3Hull2D( pts, 8, hull );
int count = b3Hull2D( pts, 8, hull, 0 );
ENSURE( count == 4 );
// The reduction code assumes counter clockwise winding
@ -616,7 +616,7 @@ static int Hull2DCollinearTest( void )
b3Point2D hull[10];
FillPoints2D( pts, input, 5 );
ENSURE( b3Hull2D( pts, 5, hull ) == 2 );
ENSURE( b3Hull2D( pts, 5, hull, 0 ) == 2 );
return 0;
}
@ -635,7 +635,7 @@ static int Hull2DWeldTest( void )
b3Point2D hull[10];
FillPoints2D( pts, input, 5 );
int count = b3Hull2D( pts, 5, hull );
int count = b3Hull2D( pts, 5, hull, 0 );
ENSURE( count == 4 );
for ( int i = 0; i < count; ++i )
@ -659,7 +659,7 @@ static int SimplifyHull2DTargetTest( void )
pts[i].originalIndex = i;
}
int hullCount = b3Hull2D( pts, 20, hull );
int hullCount = b3Hull2D( pts, 20, hull, 0 );
ENSURE( hullCount == 20 );
int count = b3SimplifyHull2D( hull, hullCount, 6 );
@ -682,7 +682,7 @@ static int SimplifyHull2DNoopTest( void )
b3Point2D hull[8];
FillPoints2D( pts, input, 4 );
int hullCount = b3Hull2D( pts, 4, hull );
int hullCount = b3Hull2D( pts, 4, hull, 0 );
ENSURE( hullCount == 4 );
ENSURE( b3SimplifyHull2D( hull, hullCount, 8 ) == 4 );
@ -706,7 +706,7 @@ static int Hull2DOrderTest( void )
b3Point2D hull[16];
FillPoints2D( pts, input, 8 );
int count1 = b3Hull2D( pts, 8, hull );
int count1 = b3Hull2D( pts, 8, hull, 0 );
ENSURE( count1 == 7 );
bool onHull1[8] = { false };
@ -724,7 +724,7 @@ static int Hull2DOrderTest( void )
shuffled[i].originalIndex = i;
}
int count2 = b3Hull2D( shuffled, 8, shuffledHull );
int count2 = b3Hull2D( shuffled, 8, shuffledHull, 0 );
ENSURE( count2 == count1 );
bool onHull2[8] = { false };
@ -741,6 +741,143 @@ static int Hull2DOrderTest( void )
return 0;
}
// Float rounding can call the middle of three nearly collinear points a left turn in both chains.
// Listing it twice gave the mesh manifold a duplicate point.
static int Hull2DNearCollinearTest( void )
{
b3Vec2 input[3] = {
{ 0.42742428183555603f, -0.9914372563362122f },
{ 0.8582707643508911f, -0.7915351390838623f },
{ 1.5856003761291504f, -0.45407217741012573f },
};
b3Point2D pts[3];
b3Point2D hull[6];
FillPoints2D( pts, input, 3 );
int count = b3Hull2D( pts, 3, hull, 0 );
ENSURE( 2 <= count && count <= 3 );
for ( int i = 0; i < count; ++i )
{
for ( int j = i + 1; j < count; ++j )
{
ENSURE( hull[i].originalIndex != hull[j].originalIndex );
}
}
return 0;
}
// A 3 by 3 grid has three points on each side. Only the corners are strict hull vertices.
static int Hull2DKeepCollinearTest( void )
{
b3Vec2 input[9];
for ( int i = 0; i < 9; ++i )
{
input[i] = (b3Vec2){ (float)( i % 3 ), (float)( i / 3 ) };
}
b3Point2D pts[9];
b3Point2D hull[18];
FillPoints2D( pts, input, 9 );
ENSURE( b3Hull2D( pts, 9, hull, 0 ) == 4 );
FillPoints2D( pts, input, 9 );
int count = b3Hull2D( pts, 9, hull, b3_hull2DKeepCollinear );
ENSURE( count == 8 );
ENSURE( Hull2DArea( hull, count ) > 0.0f );
for ( int i = 0; i < count; ++i )
{
ENSURE( hull[i].originalIndex != 4 );
}
// All collinear keeps every point
b3Vec2 line[4] = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 2.0f, 0.0f }, { 3.0f, 0.0f } };
FillPoints2D( pts, line, 4 );
ENSURE( b3Hull2D( pts, 4, hull, b3_hull2DKeepCollinear ) == 4 );
return 0;
}
// Coincident points under the exact merge: the earlier point survives unless a later one is deeper by
// more than the linear slop. The default weld always keeps the earlier point.
static int Hull2DExactMergeTest( void )
{
b3Vec2 input[5] = {
{ -1.0f, -1.0f }, { 1.0f, -1.0f }, { 1.0f, 1.0f }, { -1.0f, 1.0f }, { -1.0f, -1.0f },
};
float depths[2] = { 2.0f * B3_LINEAR_SLOP, 0.5f * B3_LINEAR_SLOP };
int expected[2] = { 4, 0 };
for ( int k = 0; k < 2; ++k )
{
b3Point2D pts[5];
b3Point2D hull[10];
FillPoints2D( pts, input, 5 );
pts[4].separation = -depths[k];
int count = b3Hull2D( pts, 5, hull, b3_hull2DExactMerge );
ENSURE( count == 4 );
int survivor = -1;
for ( int i = 0; i < count; ++i )
{
if ( hull[i].originalIndex == 0 || hull[i].originalIndex == 4 )
{
survivor = hull[i].originalIndex;
}
}
ENSURE( survivor == expected[k] );
FillPoints2D( pts, input, 5 );
pts[4].separation = -depths[k];
count = b3Hull2D( pts, 5, hull, 0 );
ENSURE( count == 4 );
for ( int i = 0; i < count; ++i )
{
ENSURE( hull[i].originalIndex != 4 );
}
}
return 0;
}
// The index order flag returns the same points sorted by input index
static int Hull2DIndexOrderTest( void )
{
b3Vec2 input[8] = {
{ 1.0f, 2.0f }, { -2.5f, 0.4f }, { 2.0f, -1.0f }, { 0.2f, 0.1f },
{ -2.0f, -1.0f }, { 2.5f, 0.5f }, { 0.0f, -1.5f }, { -1.0f, 1.8f },
};
b3Point2D pts[8];
b3Point2D hull[16];
FillPoints2D( pts, input, 8 );
int count1 = b3Hull2D( pts, 8, hull, 0 );
bool onHull[8] = { false };
for ( int i = 0; i < count1; ++i )
{
onHull[hull[i].originalIndex] = true;
}
FillPoints2D( pts, input, 8 );
int count2 = b3Hull2D( pts, 8, hull, b3_hull2DIndexOrder );
ENSURE( count2 == count1 );
for ( int i = 0; i < count2; ++i )
{
ENSURE( onHull[hull[i].originalIndex] );
ENSURE( i == 0 || hull[i - 1].originalIndex < hull[i].originalIndex );
}
return 0;
}
int HullTest( void )
{
RUN_SUBTEST( CreateHullCubeTest );
@ -759,6 +896,10 @@ int HullTest( void )
RUN_SUBTEST( Hull2DCollinearTest );
RUN_SUBTEST( Hull2DWeldTest );
RUN_SUBTEST( Hull2DOrderTest );
RUN_SUBTEST( Hull2DNearCollinearTest );
RUN_SUBTEST( Hull2DKeepCollinearTest );
RUN_SUBTEST( Hull2DExactMergeTest );
RUN_SUBTEST( Hull2DIndexOrderTest );
RUN_SUBTEST( SimplifyHull2DTargetTest );
RUN_SUBTEST( SimplifyHull2DNoopTest );

View file

@ -430,7 +430,7 @@ static int TriangleEdgeTest( void )
b3LocalManifold manifold = { 0 };
manifold.points = points;
b3SATCache cache = { .type = b3_manualEdgePairAxis };
b3CollideTriangleAndHull( &manifold, 8, v1, v2, v3, 0, &hull.base, &cache, true );
b3CollideTriangleAndHull( &manifold, 8, v1, v2, v3, &hull.base, &cache, true );
b3Vec3 expectedNormal = b3Neg( axis );
b3Vec3 expectedPoint = b3MulAdd( hullPoint, 0.5f * gap, axis );
@ -463,7 +463,7 @@ static int TriangleEdgeTest( void )
b3LocalManifold manifold = { 0 };
manifold.points = points;
b3SATCache cache = { 0 };
b3CollideTriangleAndHull( &manifold, 8, v1, v2, v3, 0, &hull.base, &cache, true );
b3CollideTriangleAndHull( &manifold, 8, v1, v2, v3, &hull.base, &cache, true );
ENSURE( manifold.pointCount == 0 );
ENSURE( cache.type == b3_edgePairAxis );
@ -496,7 +496,7 @@ static int TriangleParallelEdgeTest( void )
b3LocalManifold manifold = { 0 };
manifold.points = points;
b3SATCache cache = { 0 };
b3CollideTriangleAndHull( &manifold, 8, v1, v2, v3, 0, &hull.base, &cache, true );
b3CollideTriangleAndHull( &manifold, 8, v1, v2, v3, &hull.base, &cache, true );
ENSURE_FACE_POINTS( manifold.pointCount );
ENSURE( cache.type == b3_faceAxisA );
@ -890,7 +890,7 @@ static int TriangleHullEdgeSweepTest( void )
b3LocalManifold manifold = { 0 };
manifold.points = points;
b3SATCache cache = { .type = b3_manualEdgePairAxis };
b3CollideTriangleAndHull( &manifold, 8, v1, v2, v3, 0, &hull.base, &cache, true );
b3CollideTriangleAndHull( &manifold, 8, v1, v2, v3, &hull.base, &cache, true );
if ( cache.type != b3_edgePairAxis || manifold.pointCount != 1 )
{
@ -1340,7 +1340,7 @@ static int SpeculativeMarginTest( void )
manifold.pointCount = 0;
b3BoxHull raised = b3MakeTransformedBoxHull( 0.5f, 0.5f, 0.5f, SlideY( 0.5f + gap ) );
b3SATCache cache = { 0 };
b3CollideTriangleAndHull( &manifold, B3_MAX_MANIFOLD_POINTS, triangle[0], triangle[1], triangle[2], 0, &raised.base,
b3CollideTriangleAndHull( &manifold, B3_MAX_MANIFOLD_POINTS, triangle[0], triangle[1], triangle[2], &raised.base,
&cache, true );
ENSURE( CheckSpeculativePoints( &manifold, gap ) == 0 );
}

File diff suppressed because it is too large Load diff