This commit is contained in:
Erin Catto 2026-10-31 00:07:57 -07:00
commit 1da9fe9bd6
15 changed files with 1849 additions and 593 deletions

View file

@ -19,10 +19,10 @@
// 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 278
#define RAGDOLL_HASH 0x8851ED6D
#define WAVE_PILE_SLEEP_STEP 274
#define WAVE_PILE_HASH 0xAEDE433C
#define QUERY_SPAWN_SLEEP_STEP 242
#define QUERY_SPAWN_HASH 0x489585E6
#define QUERY_SPAWN_HIT_COUNT 59
@ -30,10 +30,10 @@
#define MESH_DROP_SLEEP_STEP 218
#define MESH_DROP_HASH 0xD378CF04
#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 346
#define RAGDOLL_HASH 0x27E7E059
#define WAVE_PILE_SLEEP_STEP 297
#define WAVE_PILE_HASH 0xC357A993
#define QUERY_SPAWN_SLEEP_STEP 242
#define QUERY_SPAWN_HASH 0xF3AD9BA6
#define QUERY_SPAWN_HIT_COUNT 59

View file

@ -9,6 +9,8 @@
#include "box3d/math_functions.h"
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
// Two quads meeting at a concave crease along the shared edge 1-4. The crease exercises
@ -782,6 +784,340 @@ static int MeshWideHullSlide( void )
return 0;
}
// A sphere balanced on a convex ridge is held up by edge contacts alone. Their normals lie between
// the two face normals, so neighbor validation must keep them.
static int MeshSphereOnRidge( void )
{
b3Vec3 vertices[] = {
{ -2.0f, -0.7f, -1.0f }, { 0.0f, 0.0f, -1.0f }, { 2.0f, -0.7f, -1.0f },
{ -2.0f, -0.7f, 1.0f }, { 0.0f, 0.0f, 1.0f }, { 2.0f, -0.7f, 1.0f },
};
int32_t indices[] = {
0, 3, 1, 3, 4, 1, 1, 4, 2, 4, 5, 2,
};
b3MeshDef def = { 0 };
def.vertices = vertices;
def.vertexCount = ARRAY_COUNT( vertices );
def.indices = indices;
def.triangleCount = ARRAY_COUNT( indices ) / 3;
def.identifyEdges = true;
b3MeshData* mesh = b3CreateMesh( &def, NULL, 0 );
ENSURE( mesh != NULL );
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3BodyDef bodyDef = b3DefaultBodyDef();
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateMeshShape( groundId, &shapeDef, mesh, b3Vec3_one );
float radius = 0.25f;
bodyDef.type = b3_dynamicBody;
bodyDef.position = (b3Pos){ 0.0f, radius, 0.0f };
b3BodyId sphereId = b3CreateBody( worldId, &bodyDef );
b3Sphere sphere = { b3Vec3_zero, radius };
b3CreateSphereShape( sphereId, &shapeDef, &sphere );
for ( int step = 0; step < 30; ++step )
{
b3World_Step( worldId, 1.0f / 60.0f, 4 );
b3ContactData contacts[4];
int contactCount = b3Body_GetContactData( sphereId, contacts, ARRAY_COUNT( contacts ) );
ENSURE( contactCount == 1 );
ENSURE( contacts[0].manifoldCount >= 1 );
for ( int j = 0; j < contacts[0].manifoldCount; ++j )
{
ENSURE( b3AbsFloat( contacts[0].manifolds[j].normal.y ) > 0.99f );
}
}
b3Pos position = b3Body_GetPosition( sphereId );
ENSURE( position.y > 0.9f * radius );
b3DestroyWorld( worldId );
b3DestroyMesh( mesh );
return 0;
}
// The normal points from the mesh to the body. Any normal in the span of the face normals under
// the body is real, anything else is a ghost.
static bool IsProfileGhost( const b3Vec2* profile, int count, b3Vec3 normal, float xMin, float xMax )
{
float tolerance = 5.0f * B3_PI / 180.0f;
if ( b3AbsFloat( normal.z ) > sinf( tolerance ) )
{
return true;
}
float lower = B3_PI;
float upper = -B3_PI;
for ( int k = 0; k + 1 < count; ++k )
{
if ( profile[k + 1].x < xMin || xMax < profile[k].x )
{
continue;
}
b3Vec2 a = profile[k];
b3Vec2 b = profile[k + 1];
float angle = atan2f( b.x - a.x, a.y - b.y );
lower = b3MinFloat( lower, angle );
upper = b3MaxFloat( upper, angle );
}
float angle = atan2f( normal.y, normal.x );
return angle < lower - tolerance || upper + tolerance < angle;
}
// A capsule lying across the slide direction crosses a 5.7 degree crest at speed, like the crest
// scene in the ghost collision sample. The edges are flat, convex and concave, and a capsule edge
// contact on the wrong side of a fold kicks the capsule.
static int CapsuleOverCrest( bool mirror )
{
b3Vec2 profile[] = { { -20.0f, 0.0f }, { -10.0f, 0.0f }, { 0.0f, 1.0f }, { 10.0f, 0.0f }, { 20.0f, 0.0f } };
int profileCount = ARRAY_COUNT( profile );
// Quarter meter cells with alternating diagonals, like cooked level geometry
float cellSize = 0.25f;
b3Vec2 points[256];
int pointCount = 0;
points[pointCount++] = profile[0];
for ( int k = 0; k + 1 < profileCount; ++k )
{
b3Vec2 a = profile[k];
b3Vec2 b = profile[k + 1];
float length = sqrtf( ( b.x - a.x ) * ( b.x - a.x ) + ( b.y - a.y ) * ( b.y - a.y ) );
int n = (int)ceilf( length / cellSize - 0.01f );
for ( int s = 1; s <= n; ++s )
{
ENSURE( pointCount < ARRAY_COUNT( points ) );
float t = (float)s / (float)n;
points[pointCount++] = (b3Vec2){ a.x + t * ( b.x - a.x ), a.y + t * ( b.y - a.y ) };
}
}
// Mirror the data and flip it back with the mesh scale, so the world geometry is the same
float halfWidth = 3.0f;
int zCount = 24;
int stride = zCount + 1;
float sign = mirror ? -1.0f : 1.0f;
int vertexCount = pointCount * stride;
b3Vec3* vertices = malloc( vertexCount * sizeof( b3Vec3 ) );
for ( int i = 0; i < pointCount; ++i )
{
for ( int j = 0; j <= zCount; ++j )
{
float z = -halfWidth + 2.0f * halfWidth * (float)j / (float)zCount;
vertices[i * stride + j] = (b3Vec3){ sign * points[i].x, points[i].y, z };
}
}
int triangleCount = 2 * ( pointCount - 1 ) * zCount;
int32_t* indices = malloc( 3 * triangleCount * sizeof( int32_t ) );
int indexCount = 0;
for ( int i = 0; i + 1 < pointCount; ++i )
{
for ( int j = 0; j < zCount; ++j )
{
int a = i * stride + j;
int b = a + 1;
int c = a + stride + 1;
int d = a + stride;
int corners[6] = { a, b, c, c, d, a };
if ( ( i + j ) & 1 )
{
int alternate[6] = { a, b, d, b, c, d };
memcpy( corners, alternate, sizeof( corners ) );
}
for ( int k = 0; k < 6; k += 3 )
{
indices[indexCount++] = corners[k];
indices[indexCount++] = mirror ? corners[k + 2] : corners[k + 1];
indices[indexCount++] = mirror ? corners[k + 1] : corners[k + 2];
}
}
}
b3MeshDef def = { 0 };
def.vertices = vertices;
def.vertexCount = vertexCount;
def.indices = indices;
def.triangleCount = triangleCount;
def.identifyEdges = true;
b3MeshData* mesh = b3CreateMesh( &def, NULL, 0 );
free( vertices );
free( indices );
ENSURE( mesh != NULL );
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3BodyDef bodyDef = b3DefaultBodyDef();
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3Vec3 scale = mirror ? (b3Vec3){ -1.0f, 1.0f, 1.0f } : b3Vec3_one;
b3CreateMeshShape( groundId, &shapeDef, mesh, scale );
float radius = 0.2f;
bodyDef.type = b3_dynamicBody;
bodyDef.position = (b3Pos){ -16.0f, radius + 0.01f, 0.0f };
bodyDef.linearVelocity = (b3Vec3){ 6.0f, 0.0f, 0.0f };
bodyDef.enableSleep = false;
b3BodyId capsuleId = b3CreateBody( worldId, &bodyDef );
b3Capsule capsule = { { 0.0f, 0.0f, -0.4f }, { 0.0f, 0.0f, 0.4f }, radius };
shapeDef.baseMaterial.friction = 0.0f;
b3ShapeId capsuleShapeId = b3CreateCapsuleShape( capsuleId, &shapeDef, &capsule );
int ghostCount = 0;
for ( int step = 0; step < 600 && b3Body_GetPosition( capsuleId ).x < 15.0f; ++step )
{
b3World_Step( worldId, 1.0f / 60.0f, 4 );
b3AABB bounds = b3Shape_GetAABB( capsuleShapeId );
float xMin = (float)bounds.lowerBound.x - 0.1f;
float xMax = (float)bounds.upperBound.x + 0.1f;
b3ContactData contacts[4];
int contactCount = b3Body_GetContactData( capsuleId, contacts, ARRAY_COUNT( contacts ) );
for ( int i = 0; i < contactCount; ++i )
{
bool capsuleIsA = B3_ID_EQUALS( contacts[i].shapeIdA, capsuleShapeId );
for ( int j = 0; j < contacts[i].manifoldCount; ++j )
{
const b3Manifold* manifold = contacts[i].manifolds + j;
// Speculative points that never pushed are harmless
float impulse = 0.0f;
for ( int k = 0; k < manifold->pointCount; ++k )
{
impulse += manifold->points[k].totalNormalImpulse;
}
b3Vec3 normal = capsuleIsA ? b3Neg( manifold->normal ) : manifold->normal;
if ( impulse > 0.0f && IsProfileGhost( profile, profileCount, normal, xMin, xMax ) )
{
ghostCount += 1;
}
}
}
}
ENSURE( ghostCount == 0 );
ENSURE( b3Body_GetPosition( capsuleId ).x >= 15.0f );
b3DestroyWorld( worldId );
b3DestroyMesh( mesh );
return 0;
}
static int MeshCapsuleOverCrest( void )
{
ENSURE( CapsuleOverCrest( false ) == 0 );
ENSURE( CapsuleOverCrest( true ) == 0 );
return 0;
}
// Fires a sphere or the end of a capsule at the apex along the given direction, without gravity,
// and returns the deepest separation from the apex.
static float ApexImpact( b3MeshData* mesh, b3Vec3 direction, bool useCapsule )
{
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.gravity = b3Vec3_zero;
b3WorldId worldId = b3CreateWorld( &worldDef );
b3BodyDef bodyDef = b3DefaultBodyDef();
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateMeshShape( groundId, &shapeDef, mesh, b3Vec3_one );
float radius = 0.25f;
bodyDef.type = b3_dynamicBody;
bodyDef.position = b3ToPos( b3MulSV( radius + 0.1f, direction ) );
bodyDef.linearVelocity = b3MulSV( -2.0f, direction );
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
// The capsule points away from the apex, so its lower end is the closest point
b3Vec3 tip = b3MulSV( 0.5f, direction );
if ( useCapsule )
{
b3Capsule capsule = { b3Vec3_zero, tip, radius };
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
}
else
{
b3Sphere sphere = { b3Vec3_zero, radius };
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
}
float minSeparation = FLT_MAX;
for ( int step = 0; step < 60; ++step )
{
b3World_Step( worldId, 1.0f / 60.0f, 4 );
b3Pos p1 = b3Body_GetWorldPoint( bodyId, b3Vec3_zero );
b3Pos p2 = b3Body_GetWorldPoint( bodyId, useCapsule ? tip : b3Vec3_zero );
b3Vec3 a = b3ToVec3( p1 );
b3Vec3 segment = b3Sub( b3ToVec3( p2 ), a );
float lengthSquared = b3LengthSquared( segment );
float t = lengthSquared > 0.0f ? b3ClampFloat( -b3Dot( a, segment ) / lengthSquared, 0.0f, 1.0f ) : 0.0f;
float distance = b3Length( b3MulAdd( a, t, segment ) );
minSeparation = b3MinFloat( minSeparation, distance - radius );
}
b3DestroyWorld( worldId );
return minSeparation;
}
// A vertex manifold that fails a neighbor test is dropped. That relies on some triangle around a
// convex vertex admitting every normal in the vertex normal cone. On this irregular valence 6 apex
// each direction is inside the cone and exactly one of the six triangles admits it.
static int MeshApexContact( void )
{
b3Vec3 vertices[] = {
{ 0.0f, 0.0f, 0.0f }, { 1.08f, -1.9f, -0.45f }, { -1.1f, -0.89f, -0.35f }, { -1.73f, -1.91f, -0.06f },
{ -0.8f, -1.45f, 0.27f }, { 0.35f, -1.63f, 0.35f }, { 0.79f, -1.96f, 0.07f },
};
int32_t indices[] = {
0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 1,
};
b3MeshDef def = { 0 };
def.vertices = vertices;
def.vertexCount = ARRAY_COUNT( vertices );
def.indices = indices;
def.triangleCount = ARRAY_COUNT( indices ) / 3;
def.identifyEdges = true;
b3MeshData* mesh = b3CreateMesh( &def, NULL, 0 );
ENSURE( mesh != NULL );
b3Vec3 directions[] = {
{ -0.5703f, 0.7369f, 0.3629f },
{ 0.8019f, 0.4956f, 0.3337f },
};
// Dropping every vertex manifold here lets the body sink a full step before the deep overlap
// fallback catches it
for ( int i = 0; i < ARRAY_COUNT( directions ); ++i )
{
b3Vec3 direction = b3Normalize( directions[i] );
ENSURE( ApexImpact( mesh, direction, false ) > -B3_LINEAR_SLOP );
ENSURE( ApexImpact( mesh, direction, true ) > -B3_LINEAR_SLOP );
}
b3DestroyMesh( mesh );
return 0;
}
int MeshTest( void )
{
RUN_SUBTEST( MeshDenseStride );
@ -795,6 +1131,9 @@ int MeshTest( void )
RUN_SUBTEST( MeshManyTriangles );
RUN_SUBTEST( MeshMirroredFlags );
RUN_SUBTEST( MeshEdgeAngles );
RUN_SUBTEST( MeshSphereOnRidge );
RUN_SUBTEST( MeshCapsuleOverCrest );
RUN_SUBTEST( MeshApexContact );
return 0;
}