fixed GJK fallback bug

more point culling
chamfer test
This commit is contained in:
Erin Catto 2026-11-02 22:46:34 -08:00
commit 991f3f9758
15 changed files with 624 additions and 45 deletions

View file

@ -83,6 +83,10 @@
</Expand>
</Type>
<Type Name="b3LocalManifoldPoint">
<DisplayString>s: {separation}</DisplayString>
</Type>
<Type Name="b3SeparationFunction">
<DisplayString>type: {type}</DisplayString>
<Expand>

View file

@ -99,6 +99,53 @@
21 3 22
*/
static b3Vec3 b3GetGridPoint( const float* heights, int columnCount, b3Vec3 scale, int row, int column )
{
return b3Mul( scale, (b3Vec3){ (float)column, heights[row * columnCount + column], (float)row } );
}
static b3Vec3 b3GetGridTriangleNormal( const float* heights, int columnCount, b3Vec3 scale, int row, int column, bool odd )
{
b3Vec3 point11 = b3GetGridPoint( heights, columnCount, scale, row, column );
b3Vec3 point12 = b3GetGridPoint( heights, columnCount, scale, row, column + 1 );
b3Vec3 point21 = b3GetGridPoint( heights, columnCount, scale, row + 1, column );
b3Vec3 point22 = b3GetGridPoint( heights, columnCount, scale, row + 1, column + 1 );
b3Vec3 v1 = odd ? point22 : point11;
b3Vec3 v2 = odd ? point12 : point21;
b3Vec3 v3 = odd ? point21 : point12;
return b3Normalize( b3Cross( b3Sub( v2, v1 ), b3Sub( v3, v1 ) ) );
}
// Mark vertices as flat or concave.
static void b3MarkGridEdge( uint8_t* vertexFlags, int index1, int index2, b3Vec3 point1, b3Vec3 point2, b3Vec3 normal1,
b3Vec3 normal2, bool clockwise )
{
b3Vec3 outward = b3Cross( b3Normalize( b3Sub( point2, point1 ) ), normal1 );
float angle = b3Atan2( b3Dot( normal2, outward ), b3Dot( normal2, normal1 ) );
// Quantize to match the mesh rule.
int8_t quantized = b3QuantizeEdgeAngle( clockwise ? -angle : angle );
if ( quantized != 0 )
{
vertexFlags[index1] = (uint8_t)( vertexFlags[index1] & ~b3_flatVertex );
vertexFlags[index2] = (uint8_t)( vertexFlags[index2] & ~b3_flatVertex );
}
if ( quantized < 0 )
{
vertexFlags[index1] |= b3_concaveVertex;
vertexFlags[index2] |= b3_concaveVertex;
}
}
static void b3MarkGridBoundary( uint8_t* vertexFlags, int index1, int index2 )
{
vertexFlags[index1] = (uint8_t)( vertexFlags[index1] & ~b3_flatVertex );
vertexFlags[index2] = (uint8_t)( vertexFlags[index2] & ~b3_flatVertex );
}
b3HeightFieldData* b3CreateHeightField( const b3HeightFieldDef* data )
{
int columnCount = data->countX;
@ -119,6 +166,8 @@ b3HeightFieldData* b3CreateHeightField( const b3HeightFieldDef* data )
byteCount += b3AlignUp8( cellCount * sizeof( uint8_t ) );
int flagsOffset = (int)byteCount;
byteCount += b3AlignUp8( triangleCount * sizeof( uint8_t ) );
int vertexFlagOffset = (int)byteCount;
byteCount += b3AlignUp8( (size_t)( ( heightCount + 7 ) / 8 ) );
// Zero the whole blob so alignment padding is defined. The construction-time hash
// sweeps raw bytes and would otherwise pick up uninitialized padding.
@ -133,6 +182,7 @@ b3HeightFieldData* b3CreateHeightField( const b3HeightFieldDef* data )
hf->heightsOffset = heightsOffset;
hf->materialOffset = materialOffset;
hf->flagsOffset = flagsOffset;
hf->vertexFlagOffset = vertexFlagOffset;
hf->clockwise = data->clockwiseWinding;
uint16_t* compressedHeights = (uint16_t*)( (intptr_t)hf + heightsOffset );
@ -466,6 +516,79 @@ b3HeightFieldData* b3CreateHeightField( const b3HeightFieldDef* data )
B3_ASSERT( triangleIndex == triangleCount );
// b3MeshVertexFlags
uint8_t* vertexFlags = (uint8_t*)b3Alloc( heightCount * sizeof( uint8_t ) );
memset( vertexFlags, b3_flatVertex, heightCount * sizeof( uint8_t ) );
bool clockwise = hf->clockwise != 0;
int cellColumnCount = columnCount - 1;
int cellRowCount = rowCount - 1;
for ( int row = 0; row < rowCount; ++row )
{
for ( int column = 0; column < columnCount; ++column )
{
int index = row * columnCount + column;
b3Vec3 point = b3GetGridPoint( heights, columnCount, scale, row, column );
if ( column < cellColumnCount )
{
bool below = row < cellRowCount && materialIndices[row * cellColumnCount + column] != B3_HEIGHT_FIELD_HOLE;
bool above =
row > 0 && materialIndices[( row - 1 ) * cellColumnCount + column] != B3_HEIGHT_FIELD_HOLE;
if ( below && above )
{
b3Vec3 next = b3GetGridPoint( heights, columnCount, scale, row, column + 1 );
b3Vec3 normal1 = b3GetGridTriangleNormal( heights, columnCount, scale, row, column, false );
b3Vec3 normal2 = b3GetGridTriangleNormal( heights, columnCount, scale, row - 1, column, true );
b3MarkGridEdge( vertexFlags, index + 1, index, next, point, normal1, normal2, clockwise );
}
else if ( below || above )
{
b3MarkGridBoundary( vertexFlags, index, index + 1 );
}
}
if ( row < cellRowCount )
{
bool right =
column < cellColumnCount && materialIndices[row * cellColumnCount + column] != B3_HEIGHT_FIELD_HOLE;
bool left = column > 0 && materialIndices[row * cellColumnCount + column - 1] != B3_HEIGHT_FIELD_HOLE;
if ( right && left )
{
b3Vec3 next = b3GetGridPoint( heights, columnCount, scale, row + 1, column );
b3Vec3 normal1 = b3GetGridTriangleNormal( heights, columnCount, scale, row, column, false );
b3Vec3 normal2 = b3GetGridTriangleNormal( heights, columnCount, scale, row, column - 1, true );
b3MarkGridEdge( vertexFlags, index, index + columnCount, point, next, normal1, normal2, clockwise );
}
else if ( right || left )
{
b3MarkGridBoundary( vertexFlags, index, index + columnCount );
}
}
if ( row < cellRowCount && column < cellColumnCount &&
materialIndices[row * cellColumnCount + column] != B3_HEIGHT_FIELD_HOLE )
{
b3Vec3 point21 = b3GetGridPoint( heights, columnCount, scale, row + 1, column );
b3Vec3 point12 = b3GetGridPoint( heights, columnCount, scale, row, column + 1 );
b3Vec3 normal1 = b3GetGridTriangleNormal( heights, columnCount, scale, row, column, false );
b3Vec3 normal2 = b3GetGridTriangleNormal( heights, columnCount, scale, row, column, true );
b3MarkGridEdge( vertexFlags, index + columnCount, index + 1, point21, point12, normal1, normal2, clockwise );
}
}
}
uint8_t* vertexBits = (uint8_t*)( (intptr_t)hf + vertexFlagOffset );
for ( int i = 0; i < heightCount; ++i )
{
if ( vertexFlags[i] == 0 )
{
vertexBits[i >> 3] |= (uint8_t)( 1 << ( i & 7 ) );
}
}
b3Free( vertexFlags, heightCount * sizeof( uint8_t ) );
b3Free( decompressedHeights, heightCount * sizeof( float ) );
hf->hash = 0;
@ -633,6 +756,36 @@ bool b3GetHeightFieldNeighborNormal( const b3HeightFieldData* heightField, int t
return true;
}
bool b3IsHeightFieldVertexConvex( const b3HeightFieldData* heightField, int triangleIndex, int vertexIndex )
{
B3_ASSERT( 0 <= triangleIndex && triangleIndex < b3GetHeightFieldTriangleCount( heightField ) );
B3_ASSERT( 0 <= vertexIndex && vertexIndex < 3 );
if ( heightField->vertexFlagOffset == 0 )
{
return false;
}
if ( heightField->clockwise )
{
vertexIndex = ( 3 - vertexIndex ) % 3;
}
int columnCount = heightField->columnCount;
int cellIndex = triangleIndex >> 1;
int row = cellIndex / ( columnCount - 1 );
int column = cellIndex - row * ( columnCount - 1 );
int offsets[2][3] = {
{ 0, columnCount, 1 },
{ columnCount + 1, 1, columnCount },
};
int index = row * columnCount + column + offsets[triangleIndex & 1][vertexIndex];
const uint8_t* vertexBits = (const uint8_t*)( (intptr_t)heightField + heightField->vertexFlagOffset );
return ( vertexBits[index >> 3] & ( 1 << ( index & 7 ) ) ) != 0;
}
b3AABB b3ComputeHeightFieldAABB( const b3HeightFieldData* shape, b3Transform transform )
{
return b3AABB_Transform( transform, shape->aabb );

View file

@ -1056,7 +1056,7 @@ typedef struct
#define B3_EDGE_ANGLE_STEPS 127.0f
static int8_t b3QuantizeEdgeAngle( float radians )
int8_t b3QuantizeEdgeAngle( float radians )
{
float scaled = radians * ( B3_EDGE_ANGLE_STEPS / B3_PI );
int quantized = scaled >= 0.0f ? (int)( scaled + 0.5f ) : -(int)( 0.5f - scaled );

View file

@ -426,6 +426,7 @@ typedef enum b3NeighborResult
b3_neighborSnap,
} b3NeighborResult;
// 1.0027 degrees
#define B3_NEIGHBOR_TOLERANCE 0.0175f
static bool b3GetNeighborDirection( const b3Shape* shape, int triangleIndex, const b3Vec3* vertices,
@ -463,8 +464,7 @@ static bool b3AdmitsVertexNormal( const b3Shape* shape, int triangleIndex, const
return false;
}
int flags = b3GetVertexFlag( shape, triangleIndex, vertexIndex );
return ( flags & ( b3_flatVertex | b3_concaveVertex ) ) == 0;
return b3IsConvexVertex( shape, triangleIndex, vertexIndex );
}
// Voronoi test of an edge or vertex normal against the neighbor normals.
@ -564,8 +564,8 @@ static b3NeighborResult b3TestHullFaceNeighbors( const b3Shape* shape, int trian
b3Vec3 normal = manifold->normal;
b3Vec3 triangleNormal = b3MakeNormalFromPoints( vertices[0], vertices[1], vertices[2] );
float tolerance = B3_NEIGHBOR_TOLERANCE;
if ( b3Dot( normal, triangleNormal ) > 0.0f &&
b3Length( b3Cross( normal, triangleNormal ) ) <= tolerance )
float cosAngle = b3Dot( normal, triangleNormal );
if ( cosAngle > 0.0f && cosAngle * cosAngle >= 1.0f - tolerance * tolerance )
{
return b3_neighborAdmit;
}
@ -838,7 +838,7 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape
float restOffset = B3_MESH_REST_OFFSET;
const b3HullData* hullB = shapeB->type == b3_hullShape ? shapeB->hull : NULL;
float hullSpeculativeDistance = enableSpeculative ? B3_SPECULATIVE_DISTANCE : 0.0f;
float hullSpeculativeDistance = enableSpeculative ? B3_SPECULATIVE_DISTANCE : restOffset;
b3Vec3 hullMargin = { hullSpeculativeDistance, hullSpeculativeDistance,
hullSpeculativeDistance };
@ -899,7 +899,7 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape
b3Vec3 v2 = meshVertices[meshFlipped ? meshTriangle.index3 : meshTriangle.index2];
b3Vec3 v3 = meshVertices[meshFlipped ? meshTriangle.index2 : meshTriangle.index3];
// todo comment
// This bool gets predicted, so worth it.
if ( unitScale == false )
{
v1 = b3Mul( meshScale, v1 );
@ -1078,12 +1078,17 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape
if ( validateNeighbors && manifold->pointCount > 0 &&
manifold->feature != b3_featureTriangleFace )
{
b3NeighborResult result =
manifold->feature == b3_featureHullFace
? b3TestHullFaceNeighbors( shapeA, triangleIndex, vertices, relativeMatrix,
manifold, &nearFeature )
: b3TestNeighbors( shapeA, triangleIndex, vertices, relativeMatrix,
manifold->feature, manifold->normal );
b3NeighborResult result;
if ( manifold->feature == b3_featureHullFace )
{
result = b3TestHullFaceNeighbors( shapeA, triangleIndex, vertices,
relativeMatrix, manifold, &nearFeature );
}
else
{
result = b3TestNeighbors( shapeA, triangleIndex, vertices, relativeMatrix,
manifold->feature, manifold->normal );
}
// A vertex test only sees the two edges of this triangle at the vertex, so an admitted
// vertex normal can still lean across another edge at the vertex, as out of a sliver tip.
@ -1201,7 +1206,7 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape
continue;
}
// todo comment
// todo explain this better
const b3Vec3* targetVertices = survivorVertices + 3 * j;
float planeDistance = b3Dot( blockTargets[j].normal, b3Sub( source->point, targetVertices[0] ) );
if ( b3AbsFloat( planeDistance ) > blockTargets[j].distanceTolerance )
@ -1264,6 +1269,53 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape
}
}
if ( hullB != NULL && validateNeighbors )
{
// Cull contacts below hull face when it rests on a triangle.
float tolerance = B3_NEIGHBOR_TOLERANCE;
float cosSquaredTolerance = 1.0f - tolerance * tolerance;
const b3LocalManifold* resting = NULL;
for ( int i = 0; i < manifoldCount && resting == NULL; ++i )
{
const b3LocalManifold* manifold = manifoldBuffer + i;
float cosAngle = b3Dot( manifold->normal, manifold->triangleNormal );
if ( manifold->pointCount > 0 && manifold->feature == b3_featureHullFace && cosAngle > 0.0f &&
cosAngle * cosAngle >= cosSquaredTolerance )
{
resting = manifold;
}
}
if ( resting != NULL )
{
b3Vec3 normal = resting->triangleNormal;
const b3Vec3* hullPoints = b3GetHullPoints( hullB );
float hullBottom = FLT_MAX;
for ( int i = 0; i < hullB->vertexCount; ++i )
{
hullBottom = b3MinFloat( hullBottom, b3Dot( normal, hullPoints[i] ) );
}
for ( int i = 0; i < survivorCount; ++i )
{
if ( survivorManifolds[i] == B3_NULL_INDEX )
{
continue;
}
b3LocalManifold* manifold = manifoldBuffer + survivorManifolds[i];
const b3Vec3* vertices = survivorVertices + 3 * i;
float cosAngle = b3Dot( manifold->normal, normal );
float height = b3MaxFloat( b3Dot( normal, vertices[0] ),
b3MaxFloat( b3Dot( normal, vertices[1] ), b3Dot( normal, vertices[2] ) ) );
if ( ( cosAngle <= 0.0f || cosAngle * cosAngle < cosSquaredTolerance ) && height < hullBottom )
{
manifold->pointCount = 0;
}
}
}
}
for ( int manifoldIndex = 0; manifoldIndex < manifoldCount; ++manifoldIndex )
{
b3LocalManifold* manifold = manifoldBuffer + manifoldIndex;

View file

@ -130,6 +130,7 @@ b3Triangle b3GetHeightFieldTriangle( const b3HeightFieldData* heightField, int t
int b3GetHeightFieldMaterial( const b3HeightFieldData* heightField, int triangleIndex );
bool b3GetHeightFieldNeighborNormal( const b3HeightFieldData* heightField, int triangleIndex, int edgeIndex, b3Vec3* normal );
bool b3IsHeightFieldVertexConvex( const b3HeightFieldData* heightField, int triangleIndex, int vertexIndex );
static inline int b3GetHeightFieldTriangleCount( const b3HeightFieldData* heightField )
{
@ -200,16 +201,18 @@ static inline bool b3GetNeighborNormal( const b3Shape* shape, int triangleIndex,
}
int b3GetMeshVertexFlag( const b3Mesh* mesh, int triangleIndex, int vertexIndex );
int8_t b3QuantizeEdgeAngle( float radians );
static inline int b3GetVertexFlag( const b3Shape* shape, int triangleIndex, int vertexIndex )
static inline bool b3IsConvexVertex( const b3Shape* shape, int triangleIndex, int vertexIndex )
{
if ( shape->type == b3_meshShape )
{
return b3GetMeshVertexFlag( &shape->mesh, triangleIndex, vertexIndex );
int flags = b3GetMeshVertexFlag( &shape->mesh, triangleIndex, vertexIndex );
return ( flags & ( b3_flatVertex | b3_concaveVertex ) ) == 0;
}
B3_ASSERT( shape->type == b3_heightShape );
return b3_flatVertex;
return b3IsHeightFieldVertexConvex( shape->heightField, triangleIndex, vertexIndex );
}
bool b3IsValidMesh( const b3MeshData* meshData );

View file

@ -747,7 +747,7 @@ static float b3CollideHullFace( b3LocalManifold* manifold, int pointCapacity, co
b3ClipVertex* clipPoint = input + i;
minSeparation = b3MinFloat( minSeparation, clipPoint->separation );
if ( enableSpeculative == false && clipPoint->separation > 0.0f )
if ( enableSpeculative == false && clipPoint->separation > B3_MESH_REST_OFFSET )
{
continue;
}
@ -763,7 +763,7 @@ static float b3CollideHullFace( b3LocalManifold* manifold, int pointCapacity, co
finalPointCount += 1;
}
float speculativeDistance = enableSpeculative ? B3_SPECULATIVE_DISTANCE : 0.0f;
float speculativeDistance = enableSpeculative ? B3_SPECULATIVE_DISTANCE : B3_MESH_REST_OFFSET;
if ( minSeparation > speculativeDistance )
{
// This can occur with a stale SAT cache
@ -863,7 +863,7 @@ static float b3CollideTriangleFace( b3LocalManifold* manifold, int pointCapacity
b3ClipVertex* clipPoint = input + i;
minSeparation = b3MinFloat( minSeparation, clipPoint->separation );
if ( enableSpeculative == false && clipPoint->separation > 0.0f )
if ( enableSpeculative == false && clipPoint->separation > B3_MESH_REST_OFFSET )
{
continue;
}
@ -880,7 +880,7 @@ static float b3CollideTriangleFace( b3LocalManifold* manifold, int pointCapacity
finalPointCount += 1;
}
float speculativeDistance = enableSpeculative ? B3_SPECULATIVE_DISTANCE : 0.0f;
float speculativeDistance = enableSpeculative ? B3_SPECULATIVE_DISTANCE : B3_MESH_REST_OFFSET;
if ( minSeparation >= speculativeDistance )
{
// This can happens if the objects move a part while re-using a cached axis
@ -1036,7 +1036,7 @@ void b3CollideTriangleAndHull( b3LocalManifold* manifold, int capacity, b3Vec3 v
const b3Plane* hullPlanes = b3GetHullPlanes( hullB );
const b3Vec3* hullPoints = b3GetHullPoints( hullB );
float speculativeDistance = enableSpeculative ? B3_SPECULATIVE_DISTANCE : 0.0f;
float speculativeDistance = enableSpeculative ? B3_SPECULATIVE_DISTANCE : B3_MESH_REST_OFFSET;
cache->hit = 1;
// Attempt to use the cache to speed up collision
@ -1340,7 +1340,7 @@ void b3CollideTriangleAndHull( b3LocalManifold* manifold, int capacity, b3Vec3 v
b3SimplexCache simplexCache = { 0 };
b3DistanceOutput output = b3ShapeDistance( &input, &simplexCache, NULL, 0 );
if ( output.distance > 0.0f )
if ( 0.0f < output.distance && output.distance <= speculativeDistance )
{
B3_ASSERT( 0 < simplexCache.count && simplexCache.count <= 3 );