This commit is contained in:
Erin Catto 2026-11-09 12:08:29 -08:00
commit d624766043
13 changed files with 31 additions and 205 deletions

View file

@ -682,7 +682,7 @@ B3_API void b3CollideTriangleAndCapsule( b3LocalManifold* manifold, int capacity
/// Collide a triangle and hull. Normal points from triangle to hull.
B3_API void b3CollideTriangleAndHull( b3LocalManifold* manifold, int capacity, b3Vec3 v1, b3Vec3 v2, b3Vec3 v3,
int triangleFlags, const b3HullData* hullB, b3SATCache* cache,
const b3HullData* hullB, b3SATCache* cache,
bool enableSpeculative );
/// Collide a triangle and sphere. Normal points from triangle to sphere.

View file

@ -2498,14 +2498,15 @@ typedef struct b3HeightFieldData
/// uint8_t, one per triangle.
int32_t flagsOffset;
/// Offset to the vertex ghost collision flag. Array of bits.
int32_t vertexFlagOffset;
/// Triangle winding.
uint8_t clockwise;
/// Explicit padding.
uint8_t padding[3];
// Offet to the vertex ghost collision flag. Array of bits.
int32_t vertexFlagOffset;
} b3HeightFieldData;
/**@}*/ // height_field
@ -2877,11 +2878,9 @@ typedef enum b3TriangleFeature
/// This gives debug information about how a contact point was generated.
typedef enum b3MeshContactFlags
{
b3_meshContactHullFace = 0x01,
b3_meshContactSnapped = 0x02,
b3_meshContactUnverified = 0x04,
b3_meshContactDeep = 0x08,
b3_meshContactBlocked = 0x10,
b3_meshContactHullFace = 0x01, ///< Normal is a hull face normal.
b3_meshContactSnapped = 0x02, ///< The normal is snapped to the triangle normal.
b3_meshContactDeep = 0x04, ///< The contact is considered deep (2 * linear slop).
} b3MeshContactFlags;
/// Separating axis test cache. Provides temporal acceleration of collision routines.

View file

@ -419,23 +419,6 @@ protected:
const b3MeshTriangle& triangle = b3GetMeshTriangles( m_mesh )[point.triangleIndex];
int v[3] = { triangle.index1, triangle.index2, triangle.index3 };
// The normal was replaced by the face normal of a nearby triangle
if ( point.meshFlags & b3_meshContactBlocked )
{
for ( int k = 0; k < 3; ++k )
{
for ( int other : m_vertexTriangles[v[k]] )
{
if ( MatchesFace( normal, other ) )
{
return true;
}
}
}
return false;
}
switch ( point.triangleFeature )
{
case b3_featureTriangleFace:
@ -3091,23 +3074,6 @@ public:
const b3MeshTriangle& triangle = b3GetMeshTriangles( m_mesh )[point.triangleIndex];
int v[3] = { triangle.index1, triangle.index2, triangle.index3 };
// The normal was replaced by the face normal of a nearby triangle
if ( point.meshFlags & b3_meshContactBlocked )
{
for ( int k = 0; k < 3; ++k )
{
for ( int other : m_vertexTriangles[v[k]] )
{
if ( MatchesFace( normal, other ) )
{
return true;
}
}
}
return false;
}
switch ( point.triangleFeature )
{
case b3_featureTriangleFace:
@ -3282,8 +3248,8 @@ public:
m_manifoldCount, rate );
DrawTextLine( "ghost feature: face %d, edge %d, vertex %d, unresolved %d", m_classCounts[e_faceGhost],
m_classCounts[e_edgeGhost], m_classCounts[e_vertexGhost], m_classCounts[e_otherGhost] );
DrawTextLine( "ghost origin: hull face %d, snapped %d, unverified %d, deep %d, blocked %d", m_flagCounts[0],
m_flagCounts[1], m_flagCounts[2], m_flagCounts[3], m_flagCounts[4] );
DrawTextLine( "ghost origin: hull face %d, snapped %d, deep %d", m_flagCounts[0], m_flagCounts[1],
m_flagCounts[2] );
DrawTextLine( "bodies: %d, recycled: %d", m_bodyCount, m_recycleCount );
DrawTextLine( "triangles: %d, stations: %d", m_mesh->triangleCount, (int)m_stations.size() );
}
@ -3430,7 +3396,7 @@ public:
bool m_drawFlow;
bool m_drawGhosts;
static constexpr int m_flagCount = 5;
static constexpr int m_flagCount = 3;
int m_classCounts[e_ghostClassCount];
int m_flagCounts[m_flagCount];
int m_ghostCount;

View file

@ -1248,7 +1248,6 @@ public:
ImGuiSliderFlags_Logarithmic );
rebuild = ( ImGui::IsItemDeactivatedAfterEdit() && m_remesh ) || rebuild;
const char* regularizeNames[] = { "None", "Light", "Full" };
rebuild =
( ImGui::Checkbox( "Repeat Simplify", &m_repeatSimplify ) && m_remesh ) || rebuild;

View file

@ -1979,9 +1979,6 @@ public:
m_transformB = b3WorldTransform_identity;
// m_transformB.p = { -2.16650009f, 0.912535489f, 0.00000000f };
// b3MeshEdgeFlags
m_flags = 0;
/*
* separation -0.0415397286 float
type 2 '\x2' unsigned char
@ -2051,10 +2048,9 @@ public:
};
b3CollideTriangleAndHull( &m_manifold, m_pointCapacity, localTriangle[0], localTriangle[1],
localTriangle[2], m_flags, m_hull, &m_satCache, true );
localTriangle[2], m_hull, &m_satCache, true );
}
int m_flags;
b3BoxHull m_boxHull;
b3HullData* m_cylinder;
b3HullData* m_hull;
@ -3017,7 +3013,7 @@ public:
bool speculative ) override
{
b3SATCache cache = {};
b3CollideTriangleAndHull( manifold, capacity, triangle[0], triangle[1], triangle[2], 0,
b3CollideTriangleAndHull( manifold, capacity, triangle[0], triangle[1], triangle[2],
&m_box.base, &cache, speculative );
}

View file

@ -597,9 +597,6 @@ b3HeightFieldData* b3CreateHeightField( const b3HeightFieldDef* data )
return hf;
}
_Static_assert( b3_concaveEdge3 == 4 * b3_concaveEdge1, "bit math" );
_Static_assert( b3_inverseConcaveEdge3 == 4 * b3_inverseConcaveEdge1, "bit math" );
// Decode the four corner vertices of a height field cell into local space.
// Output order matches the index naming used throughout this file:
// corners[0] = (column, row), corners[1] = (column + 1, row),
@ -641,8 +638,6 @@ b3Triangle b3GetHeightFieldTriangle( const b3HeightFieldData* heightField, int t
B3_ASSERT( triangleIndex < 2 * ( heightField->columnCount - 1 ) * ( heightField->rowCount - 1 ) );
b3Triangle triangle;
triangle.flags = b3GetHeightFieldFlags( heightField )[triangleIndex];
int columnCount = heightField->columnCount;
int quadIndex = triangleIndex >> 1;
int row = quadIndex / ( columnCount - 1 );
@ -685,15 +680,6 @@ b3Triangle b3GetHeightFieldTriangle( const b3HeightFieldData* heightField, int t
{
B3_SWAP( triangle.vertices[1], triangle.vertices[2] );
B3_SWAP( triangle.i2, triangle.i3 );
// Reversing winding swaps edge1 and edge3; edge2 (the diagonal) is preserved.
int flags = triangle.flags;
int edge1Bits = flags & ( b3_concaveEdge1 | b3_inverseConcaveEdge1 );
int edge3Bits = flags & ( b3_concaveEdge3 | b3_inverseConcaveEdge3 );
flags &= ~( b3_concaveEdge1 | b3_concaveEdge3 | b3_inverseConcaveEdge1 | b3_inverseConcaveEdge3 );
flags |= edge1Bits << 2;
flags |= edge3Bits >> 2;
triangle.flags = flags;
}
return triangle;

View file

@ -54,8 +54,8 @@ void b3CollideHullsAtWidth( b3LocalManifold* manifold, int capacity, const b3Hul
void b3BuildTriangleAndCapsuleFaceContact( b3LocalManifold* manifold, const b3Vec3* triangle, b3Plane plane,
const b3Capsule* capsule );
void b3BuildTriangleAndHullFaceContact( b3LocalManifold* manifold, int capacity, const b3Vec3* triangle, int triangleFlags,
const b3HullData* hull, b3SATCache* cache, bool enableSpeculative );
void b3BuildTriangleAndHullFaceContact( b3LocalManifold* manifold, int capacity, const b3Vec3* triangle, const b3HullData* hull,
b3SATCache* cache, bool enableSpeculative );
b3AxisQuery b3ComputeSeparatingAxisW4( const b3HullData* hullA, const b3HullData* hullB, b3Transform xfB, bool earlyReturn );
b3AxisQuery b3ComputeSeparatingAxisW8( const b3HullData* hullA, const b3HullData* hullB, b3Transform xfB, bool earlyReturn );

View file

@ -28,7 +28,6 @@ typedef struct b3Triangle
{
b3Vec3 vertices[3];
int i1, i2, i3;
int flags;
} b3Triangle;
typedef struct b3TrianglePoint

View file

@ -1126,7 +1126,7 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape
}
b3CollideTriangleAndHull( manifold, pointCapacity, vertices[0], vertices[1], vertices[2],
triangle.flags, hullB, &cache->satCache, enableSpeculative );
hullB, &cache->satCache, enableSpeculative );
if ( context != NULL )
{
context->satCallCount += 1;
@ -1175,6 +1175,8 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape
if ( deep )
{
meshFlags |= b3_meshContactDeep;
// Handle deep overlap.
if ( shapeB->type == b3_sphereShape )
{
@ -1204,7 +1206,7 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape
if ( result == b3_neighborSnap )
{
meshFlags = b3_meshContactSnapped;
meshFlags |= b3_meshContactSnapped;
}
if ( result == b3_neighborSkip || result == b3_neighborSnap || result == b3_neighborDrop )
@ -1221,7 +1223,7 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape
}
else if ( result == b3_neighborSnap && shapeB->type == b3_hullShape )
{
b3BuildTriangleAndHullFaceContact( manifold, pointCapacity, vertices, triangle.flags, hullB,
b3BuildTriangleAndHullFaceContact( manifold, pointCapacity, vertices, hullB,
&cache->satCache, enableSpeculative );
}
}

View file

@ -147,12 +147,10 @@ static inline b3Triangle b3GetMeshTriangle( const b3Mesh* mesh, int triangleInde
B3_ASSERT( 0 <= triangleIndex && triangleIndex < mesh->data->triangleCount );
const b3MeshTriangle* triangles = b3GetMeshTriangles( mesh->data );
const uint8_t* flags = b3GetMeshFlags( mesh->data );
const b3Vec3* vertices = b3GetMeshVertices( mesh->data );
b3Triangle result;
b3MeshTriangle triangle = triangles[triangleIndex];
uint8_t triangleFlags = flags[triangleIndex];
b3Vec3 scale = mesh->scale;
@ -167,12 +165,6 @@ static inline b3Triangle b3GetMeshTriangle( const b3Mesh* mesh, int triangleInde
result.i2 = triangle.index3;
result.i3 = triangle.index2;
// 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
{
@ -181,7 +173,6 @@ static inline b3Triangle b3GetMeshTriangle( const b3Mesh* mesh, int triangleInde
result.i2 = triangle.index2;
result.i3 = triangle.index3;
result.flags = triangleFlags;
}
return result;
@ -209,7 +200,6 @@ static inline bool b3IsConvexVertex( const b3Shape* shape, int triangleIndex, in
{
if ( shape->type == b3_meshShape )
{
// todo should this handle reflection?
int flags = b3GetMeshVertexFlag( &shape->mesh, triangleIndex, vertexIndex );
return ( flags & ( b3_flatVertex | b3_concaveVertex ) ) == 0;
}

View file

@ -22,7 +22,6 @@ typedef struct b3TriangleData
b3Vec3 v1, v2, v3;
b3Vec3 e1, e2, e3;
b3Plane plane;
int flags;
} b3TriangleData;
// Indexed by the 3-bit vertex mask
@ -599,15 +598,6 @@ static b3SeparatingAxis b3QueryTriangleAndHullEdges( const b3TriangleData* trian
b3Vec3 trianglePoints[] = { triangle->v1, triangle->v2, triangle->v3 };
b3Vec3 triangleEdges[] = { triangle->e1, triangle->e2, triangle->e3 };
// int edgeFlags[] = { b3_concaveEdge1, b3_concaveEdge1, b3_concaveEdge3 };
#if B3_FORCE_GHOST_COLLISIONS
int triangleFlags = 0xFF;
#else
int triangleFlags = triangle->flags;
#endif
(void)triangleFlags;
b3Vec3 triNormal = triangle->plane.normal;
const b3HullHalfEdge* hullEdges = b3GetHullEdges( hull );
@ -655,7 +645,6 @@ static b3SeparatingAxis b3QueryTriangleAndHullEdges( const b3TriangleData* trian
axis = b3Normalize( axis );
float separation = b3Dot( axis, b3Sub( trianglePoints[j], hullPoint ) );
// if ( separation > result.separation && ( edgeFlags[j] & triangleFlags ) == 0 )
if ( separation > result.separation )
{
// Note: We don't exit early if we find a separating axis here since we want to
@ -957,8 +946,8 @@ static void b3CollideTriangleAndHullEdges( b3LocalManifold* manifold, int capaci
}
// Triangle face contact wrapper.
void b3BuildTriangleAndHullFaceContact( b3LocalManifold* manifold, int capacity, const b3Vec3* triangle, int triangleFlags,
const b3HullData* hull, b3SATCache* cache, bool enableSpeculative )
void b3BuildTriangleAndHullFaceContact( b3LocalManifold* manifold, int capacity, const b3Vec3* triangle, const b3HullData* hull,
b3SATCache* cache, bool enableSpeculative )
{
manifold->pointCount = 0;
@ -970,7 +959,6 @@ void b3BuildTriangleAndHullFaceContact( b3LocalManifold* manifold, int capacity,
.e2 = b3Sub( triangle[2], triangle[1] ),
.e3 = b3Sub( triangle[0], triangle[2] ),
.plane = b3MakePlaneFromPoints( triangle[0], triangle[1], triangle[2] ),
.flags = triangleFlags,
};
b3SeparatingAxis query = b3QueryTriangleFace( &triangleData, hull );
@ -987,7 +975,7 @@ b3AtomicInt b3_triangleConvexCalls;
b3AtomicInt b3_triangleCacheHits;
// Triangle is in the local space of the hull for efficiency.
void b3CollideTriangleAndHull( b3LocalManifold* manifold, int capacity, b3Vec3 v1, b3Vec3 v2, b3Vec3 v3, int triangleFlags,
void b3CollideTriangleAndHull( b3LocalManifold* manifold, int capacity, b3Vec3 v1, b3Vec3 v2, b3Vec3 v3,
const b3HullData* hullB, b3SATCache* cache, bool enableSpeculative )
{
manifold->pointCount = 0;
@ -1032,7 +1020,6 @@ void b3CollideTriangleAndHull( b3LocalManifold* manifold, int capacity, b3Vec3 v
.e2 = triangleEdges[1],
.e3 = triangleEdges[2],
.plane = trianglePlane,
.flags = triangleFlags,
};
const b3HullHalfEdge* edges = b3GetHullEdges( hullB );
@ -1111,6 +1098,9 @@ void b3CollideTriangleAndHull( b3LocalManifold* manifold, int capacity, b3Vec3 v
return;
}
// Note: it is possible for a hull face to lean into an edge direction while mainting
// a valid cache. But this has never been observed.
// Deep overlap may lead to an invalid cache
// todo confirm
bool isDeep = separation < -2.0f * linearSlop;

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 );
}

View file

@ -346,106 +346,6 @@ 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;
}
// Brute force search for the triangle across edge a-b, walked b to a with consistent winding
static int FindNeighbor( const b3Mesh* mesh, int triangleIndex, int a, int b, int* edgeIndex )
{
@ -777,7 +677,7 @@ static int MeshManyTriangles( void )
return 0;
}
// The grid is symmetric in x, so the mirror only changes the winding and the flags
// The grid is symmetric in x, so the mirror only changes the winding
static int MeshWideHullSlide( void )
{
ENSURE( WideHullSlide( b3Vec3_one ) == 0 );
@ -1334,7 +1234,6 @@ int MeshTest( void )
RUN_SUBTEST( MeshCreators );
RUN_SUBTEST( MeshWideHullSlide );
RUN_SUBTEST( MeshManyTriangles );
RUN_SUBTEST( MeshMirroredFlags );
RUN_SUBTEST( MeshEdgeAngles );
RUN_SUBTEST( MeshVertexRing );
RUN_SUBTEST( MeshSphereOnRidge );