From 1a7e09c49d6c73029f194a77b4d9dc1abd8f5bcb Mon Sep 17 00:00:00 2001 From: Erin Catto Date: Thu, 29 Oct 2026 23:05:48 -0700 Subject: [PATCH] fix for ghost collision on reflected meshes remove B3_MAX_MESH_CONTACT_TRIANGLES cap --- include/box3d/constants.h | 7 -- samples/sample_issues.cpp | 4 +- src/mesh_contact.c | 226 +++++++++++++++++-------------------- src/shape.h | 11 +- test/test_mesh.c | 230 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 342 insertions(+), 136 deletions(-) diff --git a/include/box3d/constants.h b/include/box3d/constants.h index 259dcb9..8e8c3e3 100644 --- a/include/box3d/constants.h +++ b/include/box3d/constants.h @@ -137,10 +137,3 @@ B3_API float b3GetLengthUnitsPerMeter( void ); #define B3_CHILD_POWER ( 64 - 2 * B3_SHAPE_POWER ) #define B3_MAX_SHAPES ( 1 << B3_SHAPE_POWER ) #define B3_MAX_CHILD_SHAPES ( 1 << B3_CHILD_POWER ) - -/// 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 -/// before increasing this. For example, using render mesh for collision often leads to poor performance. -#ifndef B3_MAX_MESH_CONTACT_TRIANGLES -#define B3_MAX_MESH_CONTACT_TRIANGLES 512 -#endif diff --git a/samples/sample_issues.cpp b/samples/sample_issues.cpp index 05fa85a..3f9d641 100644 --- a/samples/sample_issues.cpp +++ b/samples/sample_issues.cpp @@ -1573,9 +1573,7 @@ public: static int sampleHugeBox = RegisterSample( "Issues", "Huge Box", HugeBox::Create ); -// This demonstrates this cap on mesh complexity. B3_MAX_MESH_CONTACT_TRIANGLES -// It generates this warning: -// Box3D: WARNING: complex mesh detected, triangle buffer capacity of 256 reached +// A tilted cylinder whose bounds cover more than 512 height field triangles class HeightfieldIssue : public Sample { public: diff --git a/src/mesh_contact.c b/src/mesh_contact.c index 86f7ed0..dab5bbb 100644 --- a/src/mesh_contact.c +++ b/src/mesh_contact.c @@ -12,6 +12,7 @@ #include "box3d/types.h" #include +#include #define B3_MAX_POINTS_PER_TRIANGLE 32 @@ -30,8 +31,11 @@ static bool b3IsSorted( const int* array, int count ) } #endif +#define B3_INITIAL_TRIANGLE_CAPACITY 512 + typedef struct b3TriangleQueryContext { + b3Arena* arena; int* indices; int capacity; int count; @@ -40,38 +44,19 @@ typedef struct b3TriangleQueryContext static bool b3CollectTriangleIndicesCallback( b3Vec3 a, b3Vec3 b, b3Vec3 c, int triangleIndex, void* context ) { B3_UNUSED( a, b, c ); - b3TriangleQueryContext* triangleContext = (b3TriangleQueryContext*)context; + b3TriangleQueryContext* triangleContext = context; if ( triangleContext->count == triangleContext->capacity ) { - return false; + int capacity = 2 * triangleContext->capacity; + int* indices = b3Bump( triangleContext->arena, capacity * (int)sizeof( int ) ); + memcpy( indices, triangleContext->indices, triangleContext->count * sizeof( int ) ); + triangleContext->indices = indices; + triangleContext->capacity = capacity; } + triangleContext->indices[triangleContext->count] = triangleIndex; triangleContext->count += 1; - return triangleContext->count < triangleContext->capacity; -} - -static int b3QueryMeshTriangles( int* indices, int capacity, const b3Mesh* mesh, b3AABB bounds ) -{ - b3TriangleQueryContext context = { - .indices = indices, - .capacity = capacity, - .count = 0, - }; - - b3QueryMesh( mesh, bounds, b3CollectTriangleIndicesCallback, &context ); - return context.count; -} - -static int b3QueryHeightFieldTriangles( int* indices, int capacity, const b3HeightFieldData* heightField, b3AABB bounds ) -{ - b3TriangleQueryContext context = { - .indices = indices, - .capacity = capacity, - .count = 0, - }; - - b3QueryHeightField( heightField, bounds, b3CollectTriangleIndicesCallback, &context ); - return context.count; + return true; } static void b3RefreshCache( b3Contact* contact, const b3Shape* shapeA, b3WorldTransform xfA, const b3AABB* bounds, b3Arena arena ) @@ -102,33 +87,29 @@ static void b3RefreshCache( b3Contact* contact, const b3Shape* shapeA, b3WorldTr meshContact->queryBounds.upperBound = b3Add( bounds->upperBound, extension ); // Query triangles - int triangleCapacity = B3_MAX_MESH_CONTACT_TRIANGLES; - int* triangleIndices = b3Bump( &arena, B3_MAX_MESH_CONTACT_TRIANGLES * sizeof( int ) ); + b3TriangleQueryContext queryContext = { + .arena = &arena, + .indices = b3Bump( &arena, B3_INITIAL_TRIANGLE_CAPACITY * (int)sizeof( int ) ), + .capacity = B3_INITIAL_TRIANGLE_CAPACITY, + .count = 0, + }; // Bounds are in world space. Convert to the local mesh frame. The broadphase bounds are float, // so the demoted mesh transform is the matching float world frame (exact in float mode). b3Transform meshTransform = b3ToRelativeTransform( xfA, b3Pos_zero ); b3AABB localBounds = b3AABB_Transform( b3InvertTransform( meshTransform ), meshContact->queryBounds ); - int triangleCount; if ( shapeA->type == b3_meshShape ) { - triangleCount = b3QueryMeshTriangles( triangleIndices, triangleCapacity, &shapeA->mesh, localBounds ); + b3QueryMesh( &shapeA->mesh, localBounds, b3CollectTriangleIndicesCallback, &queryContext ); } else { B3_ASSERT( shapeA->type == b3_heightShape ); - triangleCount = b3QueryHeightFieldTriangles( triangleIndices, triangleCapacity, shapeA->heightField, localBounds ); + b3QueryHeightField( shapeA->heightField, localBounds, b3CollectTriangleIndicesCallback, &queryContext ); } - if ( triangleCount == triangleCapacity ) - { - static bool s_once = false; - if ( s_once == false ) - { - b3Log( "WARNING: complex mesh detected, triangle buffer capacity of %d reached", triangleCapacity ); - s_once = true; - } - } + int* triangleIndices = queryContext.indices; + int triangleCount = queryContext.count; // Triangle indices must be sorted to match caches. B3_VALIDATE( b3IsSorted( triangleIndices, triangleCount ) ); @@ -174,57 +155,71 @@ typedef struct b3TentativeTriangle int index; } b3TentativeTriangle; -#define B3_MAX_EDGE_COUNT 64 +#define B3_EMPTY_EDGE_KEY UINT64_MAX + +// Capacity for edge and vertex keys. +static int b3GetFoundCapacity( int keyCount ) +{ + int capacity = 16; + while ( capacity < 2 * keyCount ) + { + capacity *= 2; + } + return capacity; +} typedef struct b3FoundEdges { - uint64_t keys[B3_MAX_EDGE_COUNT]; - int count; + uint64_t* keys; + int mask; } b3FoundEdges; +static b3FoundEdges b3CreateFoundEdges( b3Arena* arena, int edgeCount ) +{ + int capacity = b3GetFoundCapacity( edgeCount ); + b3FoundEdges edges; + edges.keys = b3Bump( arena, capacity * (int)sizeof( uint64_t ) ); + memset( edges.keys, 0xFF, capacity * sizeof( uint64_t ) ); + edges.mask = capacity - 1; + return edges; +} + +static inline uint64_t b3MakeEdgeKey( int vertex1, int vertex2 ) +{ + uint64_t i1 = (uint64_t)b3MinInt( vertex1, vertex2 ); + uint64_t i2 = (uint64_t)b3MaxInt( vertex1, vertex2 ); + return i1 << 32 | i2; +} + +static inline int b3FindEdgeSlot( const b3FoundEdges* edges, uint64_t key ) +{ + int index = (int)( b3KeyHash( key ) & (uint32_t)edges->mask ); + while ( edges->keys[index] != key && edges->keys[index] != B3_EMPTY_EDGE_KEY ) + { + index = ( index + 1 ) & edges->mask; + } + + return index; +} + static inline bool b3AddEdge( b3FoundEdges* edges, int vertex1, int vertex2 ) { - uint64_t i1 = (uint64_t)b3MinInt( vertex1, vertex2 ); - uint64_t i2 = (uint64_t)b3MaxInt( vertex1, vertex2 ); - uint64_t key = i1 << 32 | i2; - - int count = edges->count; - for ( int i = 0; i < count; ++i ) + uint64_t key = b3MakeEdgeKey( vertex1, vertex2 ); + int index = b3FindEdgeSlot( edges, key ); + if ( edges->keys[index] == key ) { - if ( edges->keys[i] == key ) - { - return false; - } + return false; } - if ( count == B3_MAX_EDGE_COUNT ) - { - // This will lead to a potential ghost collision - return true; - } - - edges->keys[count] = key; - edges->count += 1; - + edges->keys[index] = key; return true; } -static inline bool b3FindEdge( b3FoundEdges* edges, int vertex1, int vertex2 ) +static inline bool b3FindEdge( const b3FoundEdges* edges, int vertex1, int vertex2 ) { - uint64_t i1 = (uint64_t)b3MinInt( vertex1, vertex2 ); - uint64_t i2 = (uint64_t)b3MaxInt( vertex1, vertex2 ); - uint64_t key = i1 << 32 | i2; - - int count = edges->count; - for ( int i = 0; i < count; ++i ) - { - if ( edges->keys[i] == key ) - { - return true; - } - } - - return false; + uint64_t key = b3MakeEdgeKey( vertex1, vertex2 ); + int index = b3FindEdgeSlot( edges, key ); + return edges->keys[index] == key; } #if 0 @@ -239,36 +234,36 @@ static inline bool b3TrianglesShareEdge( int a1, int a2, int a3, int b1, int b2, } #endif -#define B3_MAX_VERTEX_COUNT 64 - typedef struct b3FoundVertices { - int keys[B3_MAX_VERTEX_COUNT]; - int count; + int* keys; + int mask; } b3FoundVertices; +static b3FoundVertices b3CreateFoundVertices( b3Arena* arena, int vertexCount ) +{ + int capacity = b3GetFoundCapacity( vertexCount ); + b3FoundVertices vertices; + vertices.keys = b3Bump( arena, capacity * (int)sizeof( int ) ); + memset( vertices.keys, 0xFF, capacity * sizeof( int ) ); + vertices.mask = capacity - 1; + return vertices; +} + static inline bool b3AddVertex( b3FoundVertices* vertices, int vertex ) { - int key = vertex; - - int count = vertices->count; - for ( int i = 0; i < count; ++i ) + int index = (int)( b3KeyHash( (uint64_t)vertex ) & (uint32_t)vertices->mask ); + while ( vertices->keys[index] != B3_NULL_INDEX ) { - if ( vertices->keys[i] == key ) + if ( vertices->keys[index] == vertex ) { return false; } + + index = ( index + 1 ) & vertices->mask; } - if ( count == B3_MAX_VERTEX_COUNT ) - { - // This will lead to a potential ghost collision - return true; - } - - vertices->keys[count] = key; - vertices->count += 1; - + vertices->keys[index] = vertex; return true; } @@ -549,11 +544,6 @@ bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact b3TentativeTriangle* tentativeTriangles = b3Bump( &arena, triangleCount * sizeof( b3TentativeTriangle ) ); int tentativeTriangleCount = 0; - b3FoundEdges foundEdges; - b3FoundVertices foundVertices; - foundEdges.count = 0; - foundVertices.count = 0; - // This transform converts from mesh frame into the shapeB frame b3Transform transformAtoB = b3InvMulWorldTransforms( xfB, xfA ); b3Matrix3 relativeMatrix = b3MakeMatrixFromQuat( transformAtoB.q ); @@ -782,13 +772,6 @@ bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact if ( manifold->feature == b3_featureTriangleFace || B3_FORCE_GHOST_COLLISIONS ) { - (void)b3AddEdge( &foundEdges, triangle.i1, triangle.i2 ); - (void)b3AddEdge( &foundEdges, triangle.i2, triangle.i3 ); - (void)b3AddEdge( &foundEdges, triangle.i3, triangle.i1 ); - (void)b3AddVertex( &foundVertices, triangle.i1 ); - (void)b3AddVertex( &foundVertices, triangle.i2 ); - (void)b3AddVertex( &foundVertices, triangle.i3 ); - acceptedManifolds[acceptedManifoldCount++] = manifold; } else if ( manifold->feature == b3_featureHullFace ) @@ -796,13 +779,6 @@ bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact float cosNormalAngle = b3Dot( manifold->triangleNormal, manifold->normal ); if ( cosNormalAngle > 0.5f ) { - (void)b3AddEdge( &foundEdges, triangle.i1, triangle.i2 ); - (void)b3AddEdge( &foundEdges, triangle.i2, triangle.i3 ); - (void)b3AddEdge( &foundEdges, triangle.i3, triangle.i1 ); - (void)b3AddVertex( &foundVertices, triangle.i1 ); - (void)b3AddVertex( &foundVertices, triangle.i2 ); - (void)b3AddVertex( &foundVertices, triangle.i3 ); - acceptedManifolds[acceptedManifoldCount++] = manifold; } else @@ -816,12 +792,6 @@ bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact if ( minSeparation < -2.0f * linearSlop ) { // Deep overlap - (void)b3AddEdge( &foundEdges, triangle.i1, triangle.i2 ); - (void)b3AddEdge( &foundEdges, triangle.i2, triangle.i3 ); - (void)b3AddEdge( &foundEdges, triangle.i3, triangle.i1 ); - (void)b3AddVertex( &foundVertices, triangle.i1 ); - (void)b3AddVertex( &foundVertices, triangle.i2 ); - (void)b3AddVertex( &foundVertices, triangle.i3 ); acceptedManifolds[acceptedManifoldCount++] = manifold; } else @@ -847,6 +817,20 @@ bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact B3_ASSERT( tentativeManifoldCount <= triangleCount ); B3_ASSERT( tentativeTriangleCount <= triangleCount ); + b3FoundEdges foundEdges = b3CreateFoundEdges( &arena, 3 * manifoldCount ); + b3FoundVertices foundVertices = b3CreateFoundVertices( &arena, 3 * manifoldCount ); + + for ( int i = 0; i < acceptedManifoldCount; ++i ) + { + b3LocalManifold* m = acceptedManifolds[i]; + (void)b3AddEdge( &foundEdges, m->i1, m->i2 ); + (void)b3AddEdge( &foundEdges, m->i2, m->i3 ); + (void)b3AddEdge( &foundEdges, m->i3, m->i1 ); + (void)b3AddVertex( &foundVertices, m->i1 ); + (void)b3AddVertex( &foundVertices, m->i2 ); + (void)b3AddVertex( &foundVertices, m->i3 ); + } + if ( shapeB->type == b3_sphereShape ) { // Sort triangles so the closest triangles are processed first diff --git a/src/shape.h b/src/shape.h index a9164d4..f8c2acf 100644 --- a/src/shape.h +++ b/src/shape.h @@ -163,11 +163,12 @@ static inline b3Triangle b3GetMeshTriangle( const b3Mesh* mesh, int triangleInde result.i2 = triangle.index3; result.i3 = triangle.index2; - // mesh is inverted, so concave edges are now convex - result.flags = 0; - result.flags |= ( triangleFlags & b3_inverseConcaveEdge1 ) ? b3_concaveEdge1 : 0; - result.flags |= ( triangleFlags & b3_inverseConcaveEdge2 ) ? b3_concaveEdge2 : 0; - result.flags |= ( triangleFlags & b3_inverseConcaveEdge3 ) ? b3_concaveEdge3 : 0; + // Hand mesh inversion. + int edge1Bits = triangleFlags & ( b3_concaveEdge1 | b3_inverseConcaveEdge1 ); + int edge3Bits = triangleFlags & ( b3_concaveEdge3 | b3_inverseConcaveEdge3 ); + result.flags = triangleFlags & ~( b3_concaveEdge1 | b3_concaveEdge3 | b3_inverseConcaveEdge1 | b3_inverseConcaveEdge3 ); + result.flags |= edge1Bits << 2; + result.flags |= edge3Bits >> 2; } else { diff --git a/test/test_mesh.c b/test/test_mesh.c index 69330ed..52dd151 100644 --- a/test/test_mesh.c +++ b/test/test_mesh.c @@ -1,8 +1,10 @@ // SPDX-FileCopyrightText: 2026 Erin Catto // SPDX-License-Identifier: MIT +#include "shape.h" #include "test_macros.h" +#include "box3d/box3d.h" #include "box3d/collision.h" #include "box3d/math_functions.h" @@ -341,6 +343,231 @@ static int MeshCreators( void ) return 0; } +// Two bits per edge: concave and inverse concave. Both set means flat. +static int EdgeClass( int flags, int edgeIndex ) +{ + int concave = ( flags >> edgeIndex ) & 1; + int inverse = ( flags >> ( edgeIndex + 4 ) ) & 1; + return concave | inverse << 1; +} + +// A mirror with the winding swapped keeps every edge convex, concave or flat, so each edge +// class must follow its vertex pair to its new slot +static bool MirroredFlagsMatch( const b3MeshData* data, b3Vec3 mirrorScale ) +{ + b3Mesh plain = { .data = data, .scale = b3Vec3_one }; + b3Mesh mirrored = { .data = data, .scale = mirrorScale }; + + for ( int i = 0; i < data->triangleCount; ++i ) + { + b3Triangle triangle1 = b3GetMeshTriangle( &plain, i ); + b3Triangle triangle2 = b3GetMeshTriangle( &mirrored, i ); + int indices1[3] = { triangle1.i1, triangle1.i2, triangle1.i3 }; + int indices2[3] = { triangle2.i1, triangle2.i2, triangle2.i3 }; + + for ( int k = 0; k < 3; ++k ) + { + int a = indices2[k]; + int b = indices2[( k + 1 ) % 3]; + + int match = -1; + for ( int j = 0; j < 3; ++j ) + { + int c = indices1[j]; + int d = indices1[( j + 1 ) % 3]; + if ( ( a == c && b == d ) || ( a == d && b == c ) ) + { + match = j; + } + } + + if ( match == -1 || EdgeClass( triangle2.flags, k ) != EdgeClass( triangle1.flags, match ) ) + { + return false; + } + } + } + + return true; +} + +static bool HasConvexEdge( const b3MeshData* mesh ) +{ + const uint8_t* flags = b3GetMeshFlags( mesh ); + + for ( int i = 0; i < mesh->triangleCount; ++i ) + { + for ( int k = 0; k < 3; ++k ) + { + if ( EdgeClass( flags[i], k ) == 2 ) + { + return true; + } + } + } + + return false; +} + +// The valley has concave and flat edges, the box has convex and flat edges +static int MeshMirroredFlags( void ) +{ + b3Vec3 vertices[VALLEY_VERTEX_COUNT]; + int32_t indices[3 * VALLEY_TRIANGLE_COUNT]; + MakeValley( vertices, indices ); + + b3MeshDef def = MakeValleyDef( vertices, indices ); + b3MeshData* valley = b3CreateMesh( &def, NULL, 0 ); + ENSURE( valley != NULL ); + ENSURE( HasConcaveEdge( valley ) ); + + b3MeshData* box = b3CreateBoxMesh( b3Vec3_zero, (b3Vec3){ 1.0f, 2.0f, 3.0f }, true ); + ENSURE( box != NULL ); + ENSURE( HasConvexEdge( box ) ); + + b3Vec3 mirrorScales[] = { + { -1.0f, 1.0f, 1.0f }, + { 1.0f, -1.0f, 1.0f }, + { 2.0f, 0.5f, -3.0f }, + { -1.0f, -1.0f, -1.0f }, + }; + + for ( int i = 0; i < ARRAY_COUNT( mirrorScales ); ++i ) + { + ENSURE( MirroredFlagsMatch( valley, mirrorScales[i] ) ); + ENSURE( MirroredFlagsMatch( box, mirrorScales[i] ) ); + } + + b3DestroyMesh( box ); + b3DestroyMesh( valley ); + return 0; +} + +// A wide box sliding over a fine grid sits on hundreds of flat internal edges. With no friction +// any manifold off the face normal is a ghost that slows or lifts the box. +static int WideHullSlide( b3Vec3 meshScale ) +{ + b3WorldDef worldDef = b3DefaultWorldDef(); + b3WorldId worldId = b3CreateWorld( &worldDef ); + + b3MeshData* mesh = b3CreateGridMesh( 120, 40, 0.25f, 0, true ); + ENSURE( mesh != NULL ); + + b3BodyDef bodyDef = b3DefaultBodyDef(); + b3BodyId groundId = b3CreateBody( worldId, &bodyDef ); + b3ShapeDef shapeDef = b3DefaultShapeDef(); + b3CreateMeshShape( groundId, &shapeDef, mesh, meshScale ); + + float speed = 4.0f; + bodyDef.type = b3_dynamicBody; + bodyDef.position = (b3Pos){ -10.0f, 0.5f, 0.0f }; + bodyDef.linearVelocity = (b3Vec3){ speed, 0.0f, 0.0f }; + b3BodyId boxId = b3CreateBody( worldId, &bodyDef ); + + b3BoxHull box = b3MakeBoxHull( 1.25f, 0.5f, 1.25f ); + shapeDef.baseMaterial.friction = 0.0f; + b3CreateHullShape( boxId, &shapeDef, &box.base ); + + int ghostCount = 0; + for ( int step = 0; step < 120; ++step ) + { + b3World_Step( worldId, 1.0f / 60.0f, 4 ); + + b3ContactData contacts[4]; + int contactCount = b3Body_GetContactData( boxId, contacts, ARRAY_COUNT( contacts ) ); + for ( int i = 0; i < contactCount; ++i ) + { + for ( int j = 0; j < contacts[i].manifoldCount; ++j ) + { + if ( b3AbsFloat( contacts[i].manifolds[j].normal.y ) < 0.99f ) + { + ghostCount += 1; + } + } + } + } + + b3Vec3 velocity = b3Body_GetLinearVelocity( boxId ); + + ENSURE( ghostCount == 0 ); + ENSURE( velocity.x > 0.99f * speed ); + ENSURE( b3AbsFloat( velocity.y ) < 0.01f ); + + b3DestroyWorld( worldId ); + b3DestroyMesh( mesh ); + return 0; +} + +// The box overlaps more than a thousand grid triangles. All of them must reach the narrow +// phase, so the reduced manifold reaches all four sides of the footprint. +static int MeshManyTriangles( void ) +{ + b3WorldDef worldDef = b3DefaultWorldDef(); + b3WorldId worldId = b3CreateWorld( &worldDef ); + + b3MeshData* mesh = b3CreateGridMesh( 40, 40, 0.25f, 0, true ); + ENSURE( mesh != NULL ); + + b3BodyDef bodyDef = b3DefaultBodyDef(); + b3BodyId groundId = b3CreateBody( worldId, &bodyDef ); + b3ShapeDef shapeDef = b3DefaultShapeDef(); + b3CreateMeshShape( groundId, &shapeDef, mesh, b3Vec3_one ); + + bodyDef.type = b3_dynamicBody; + bodyDef.position = (b3Pos){ 0.0f, 0.5f, 0.0f }; + b3BodyId boxId = b3CreateBody( worldId, &bodyDef ); + + b3BoxHull box = b3MakeBoxHull( 3.0f, 0.5f, 3.0f ); + b3ShapeId boxShapeId = b3CreateHullShape( boxId, &shapeDef, &box.base ); + + for ( int step = 0; step < 30; ++step ) + { + b3World_Step( worldId, 1.0f / 60.0f, 4 ); + } + + b3Pos position = b3Body_GetPosition( boxId ); + b3Quat rotation = b3Body_GetRotation( boxId ); + + ENSURE( b3AbsFloat( (float)position.y - 0.5f ) < 0.02f ); + ENSURE( b3AbsFloat( rotation.v.x ) < 0.001f ); + ENSURE( b3AbsFloat( rotation.v.z ) < 0.001f ); + + b3Vec3 lower = { FLT_MAX, FLT_MAX, FLT_MAX }; + b3Vec3 upper = { -FLT_MAX, -FLT_MAX, -FLT_MAX }; + + b3ContactData contacts[4]; + int contactCount = b3Body_GetContactData( boxId, contacts, ARRAY_COUNT( contacts ) ); + for ( int i = 0; i < contactCount; ++i ) + { + bool boxIsA = B3_ID_EQUALS( contacts[i].shapeIdA, boxShapeId ); + for ( int j = 0; j < contacts[i].manifoldCount; ++j ) + { + const b3Manifold* manifold = contacts[i].manifolds + j; + for ( int k = 0; k < manifold->pointCount; ++k ) + { + b3Vec3 anchor = boxIsA ? manifold->points[k].anchorA : manifold->points[k].anchorB; + lower = b3Min( lower, anchor ); + upper = b3Max( upper, anchor ); + } + } + } + + ENSURE( lower.x < -2.5f && upper.x > 2.5f ); + ENSURE( lower.z < -2.5f && upper.z > 2.5f ); + + b3DestroyWorld( worldId ); + b3DestroyMesh( mesh ); + return 0; +} + +// The grid is symmetric in x, so the mirror only changes the winding and the flags +static int MeshWideHullSlide( void ) +{ + ENSURE( WideHullSlide( b3Vec3_one ) == 0 ); + ENSURE( WideHullSlide( (b3Vec3){ -1.0f, 1.0f, 1.0f } ) == 0 ); + return 0; +} + int MeshTest( void ) { RUN_SUBTEST( MeshDenseStride ); @@ -350,6 +577,9 @@ int MeshTest( void ) RUN_SUBTEST( MeshClockWiseIgnored ); RUN_SUBTEST( MeshClockWiseStrideWeld ); RUN_SUBTEST( MeshCreators ); + RUN_SUBTEST( MeshWideHullSlide ); + RUN_SUBTEST( MeshManyTriangles ); + RUN_SUBTEST( MeshMirroredFlags ); return 0; }