sphere ghost manifold sample
This commit is contained in:
Erin Catto 2026-11-08 10:54:50 -08:00
commit e51c0d7a1e
5 changed files with 219 additions and 25 deletions

View file

@ -683,6 +683,10 @@ B3_API void b3CollideTriangleAndSphere( b3LocalManifold* manifold, int capacity,
B3_API int b3CollideMeshAndHull( b3Manifold* manifolds, int capacity, const b3Mesh* mesh, const b3HullData* hull,
b3Transform transformBtoA, bool enableSpeculative, bool handleGhosts );
// todo comment
B3_API int b3CollideMeshAndSphere( b3Manifold* manifolds, int capacity, const b3Mesh* mesh, const b3Sphere* sphere,
b3Transform transformBtoA, bool enableSpeculative, bool handleGhosts );
/// This is used for computing 2D hulls in service of contact manifold simplification.
/// Placed here for testing. Internal.
typedef struct b3Point2D

View file

@ -2588,6 +2588,7 @@ public:
m_cellSize = 0.25f;
m_beltSpeed = 2.5f;
m_reverseFlow = false;
m_targetCount = 80;
m_spawnInterval = 15;
m_bodyType = e_mixedBody;
@ -2705,6 +2706,11 @@ public:
}
}
float GetBeltVelocity() const
{
return m_reverseFlow ? -m_beltSpeed : m_beltSpeed;
}
b3SurfaceMaterial MakeMaterial( int index ) const
{
b3SurfaceMaterial material = b3DefaultSurfaceMaterial();
@ -2718,7 +2724,8 @@ public:
// Horizontal is enough, the solver projects tangent velocity onto the contact plane
float yaw = -B3_PI + 2.0f * B3_PI * (float)( index - 1 ) / (float)m_yawBinCount;
material.friction = 0.8f;
material.tangentVelocity = { m_beltSpeed * cosf( yaw ), 0.0f, m_beltSpeed * sinf( yaw ) };
float velocity = GetBeltVelocity();
material.tangentVelocity = { velocity * cosf( yaw ), 0.0f, velocity * sinf( yaw ) };
material.customColor = b3_colorSlateGray;
return material;
}
@ -3294,7 +3301,7 @@ public:
{
b3Vec3 p = m_stations[k] + 0.05f * m_ups[k];
b3Vec3 t = m_tangents[k];
DrawArrow( b3ToPos( p ), b3ToPos( p + 0.25f * m_beltSpeed * t ), MakeColor( b3_colorBlueViolet ) );
DrawArrow( b3ToPos( p ), b3ToPos( p + 0.25f * GetBeltVelocity() * t ), MakeColor( b3_colorBlueViolet ) );
}
}
@ -3322,6 +3329,11 @@ public:
UpdateBeltSpeed();
}
if ( ImGui::Checkbox( "Reverse Flow", &m_reverseFlow ) )
{
UpdateBeltSpeed();
}
ImGui::SliderInt( "Body Count", &m_targetCount, 1, m_maxBodies );
ImGui::SliderInt( "Spawn Interval", &m_spawnInterval, 1, 60 );
@ -3407,6 +3419,7 @@ public:
float m_cellSize;
float m_beltSpeed;
bool m_reverseFlow;
int m_targetCount;
int m_spawnInterval;
BodyType m_bodyType;

View file

@ -3015,3 +3015,155 @@ public:
static int sampleHullGhostManifold =
RegisterSample( "Manifold", "Hull Ghost Manifold", HullGhostManifold::Create );
// A flat floor for x > 0 meeting a 10 degree ramp at the concave edge x = 0
static void BuildGhostRamp( std::vector<GhostMesh>& meshes )
{
GhostMesh mesh;
mesh.vertices = { { 0.0f, 0.0f, -2.0f },
{ 0.0f, 0.0f, 2.0f },
{ -2.0f, 2.0f * tanf( 10.0f * B3_PI / 180.0f ), 0.0f },
{ 2.0f, 0.0f, 0.0f } };
AddGhostTriangle( mesh, 0, 1, 2 );
AddGhostTriangle( mesh, 1, 0, 3 );
meshes.push_back( mesh );
}
// A hub whose six spokes alternate 8 degree ridges and 32 degree gullies, so every fan triangle has a
// concave edge and the mesh flags the hub concave. Every spoke still runs downhill, so the hub is the
// closest point for a body resting on it.
static void BuildGhostSaddle( std::vector<GhostMesh>& meshes )
{
float radius = 1.5f;
float toRadians = B3_PI / 180.0f;
float hubHeight = radius * tanf( 32.0f * toRadians );
GhostMesh mesh;
mesh.vertices.push_back( { 0.0f, hubHeight, 0.0f } );
for ( int i = 0; i < 6; ++i )
{
float angle = i * B3_PI / 3.0f;
float drop = ( i & 1 ) ? 32.0f : 8.0f;
mesh.vertices.push_back(
{ radius * cosf( angle ), hubHeight - radius * tanf( drop * toRadians ), radius * sinf( angle ) } );
}
for ( int i = 0; i < 6; ++i )
{
AddGhostTriangle( mesh, 0, 1 + i, 1 + ( i + 1 ) % 6 );
}
meshes.push_back( mesh );
}
// Sphere radius in the x extent
static const GhostScene s_sphereGhostScenes[] = {
{
"Flat edge",
"sphere 1 mm deep over the left triangle, 5 cm from the shared edge, the right triangle sees the edge "
"and tilts its normal 5.7 degrees",
"flat edge, the normal leans over the left triangle and a sphere off the face cannot snap, so the "
"right triangle drops it",
BuildGhostFlat,
{ 0.5f, 0.5f, 0.5f },
{ 0.0f, 0.499f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
true,
},
{
"Flat vertex",
"sphere 1 mm deep, 5 cm from the fan center inside one wedge, the other triangles see the center vertex "
"or a spoke and tilt their normals 5.7 degrees",
"flat vertex and edges, the normal is not the face normal, so they drop it",
BuildGhostFan,
{ 0.5f, 0.5f, 0.5f },
{ 0.0f, 0.499f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
true,
},
{
"Convex edge",
"sphere 1 mm deep on the right slope 5 cm below the crest, the left triangle sees the crest edge and "
"gives a normal 5.7 degrees past the right slope",
"convex edge, the normal is outside the edge cone so the left triangle skips and the right one owns "
"the contact",
BuildGhostCrest,
{ 0.5f, 0.5f, 0.5f },
{ 0.1774f, 1.005f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
true,
},
{
"Concave edge",
"sphere 1 mm deep on the floor 10 cm from a 10 degree ramp, the ramp triangle sees the crease edge 9 mm "
"away and tilts its normal 11.3 degrees away from the ramp",
"concave edge, the normal is neither face normal so it snaps, and a sphere off the face cannot snap, so "
"the ramp triangle drops it",
BuildGhostRamp,
{ 0.5f, 0.5f, 0.5f },
{ 0.1f, 0.499f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
true,
},
{
"Saddle vertex",
"none, sphere 1 mm deep on the hub. The spokes alternate 8 degree ridges and 32 degree gullies, so "
"every fan triangle has a concave edge, yet the hub is the closest point",
"the edge tests of every triangle reject the vertical normal across its gully. The normal is in the hub "
"fan cone, so every triangle keeps it",
BuildGhostSaddle,
{ 0.5f, 0.5f, 0.5f },
{ 0.0f, 1.4363f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
true,
},
};
class SphereGhostManifold : public GhostManifold
{
public:
explicit SphereGhostManifold( SampleContext* context )
: GhostManifold( context )
{
m_sphere = { b3Vec3_zero, 0.5f };
SetScenes( s_sphereGhostScenes, IM_ARRAYSIZE( s_sphereGhostScenes ) );
}
void CreateShape( const GhostScene& scene ) override
{
m_sphere = { b3Vec3_zero, scene.extents.x };
}
int CollideMesh( b3Manifold* manifolds, int capacity, const b3Mesh* mesh, b3Transform transformBtoA,
bool speculative, bool handleGhosts ) override
{
return b3CollideMeshAndSphere( manifolds, capacity, mesh, &m_sphere, transformBtoA, speculative,
handleGhosts );
}
void CollideTriangle( b3LocalManifold* manifold, int capacity, const b3Vec3* triangle, bool ) override
{
b3CollideTriangleAndSphere( manifold, capacity, triangle, &m_sphere );
}
void DrawShape( b3WorldTransform transform ) override
{
DrawWireSphere( transform, &m_sphere, 32, MakeColor( b3_colorGreen ) );
}
b3AABB ComputeShapeBounds( b3Transform transform ) override
{
b3Vec3 center = b3TransformPoint( transform, m_sphere.center );
b3Vec3 extent = { m_sphere.radius, m_sphere.radius, m_sphere.radius };
return { b3Sub( center, extent ), b3Add( center, extent ) };
}
static Sample* Create( SampleContext* context )
{
return new SphereGhostManifold( context );
}
b3Sphere m_sphere;
};
static int sampleSphereGhostManifold =
RegisterSample( "Manifold", "Sphere Ghost Manifold", SphereGhostManifold::Create );

View file

@ -1329,6 +1329,7 @@ int b3GetMeshVertexFlag( const b3Mesh* mesh, int triangleIndex, int vertexIndex
return vertexFlags[b3GetMeshVertexIndex( mesh, triangle, vertexIndex )];
}
// Is the candidate manifold normal in vertex Voronoi region?
bool b3IsInMeshVertexRing( const b3Mesh* mesh, int triangleIndex, int vertexIndex, b3Vec3 normal, float tolerance )
{
const b3MeshData* data = mesh->data;
@ -1340,23 +1341,25 @@ bool b3IsInMeshVertexRing( const b3Mesh* mesh, int triangleIndex, int vertexInde
return true;
}
b3MeshTriangle triangle = b3GetMeshTriangles( data )[triangleIndex];
const b3MeshTriangle* triangles = b3GetMeshTriangles( data );
b3MeshTriangle triangle = triangles[triangleIndex];
int vertex = b3GetMeshVertexIndex( mesh, triangle, vertexIndex );
// todo comment
// Account for the mesh scale, including reflection. The cliff scale is separate
// to maintain the 90 degree angle that all cliffs use.
b3Vec3 scale = mesh->scale;
b3Vec3 edgeScale, boundaryScale;
b3Vec3 edgeScale, cliffScale;
b3Vec3 absScale = b3Abs( scale );
bool uniform = absScale.y == absScale.x && absScale.z == absScale.x;
if ( uniform )
{
edgeScale = ( b3Vec3 ){ scale.x < 0.0f ? -1.0f : 1.0f, scale.y < 0.0f ? -1.0f : 1.0f, scale.z < 0.0f ? -1.0f : 1.0f };
boundaryScale = edgeScale;
cliffScale = edgeScale;
}
else
{
edgeScale = scale;
boundaryScale = ( b3Vec3 ){ 1.0f / scale.x, 1.0f / scale.y, 1.0f / scale.z };
cliffScale = ( b3Vec3 ){ 1.0f / scale.x, 1.0f / scale.y, 1.0f / scale.z };
}
const int* starts = (const int*)( (intptr_t)data + data->vertexRingOffset );
@ -1367,7 +1370,7 @@ bool b3IsInMeshVertexRing( const b3Mesh* mesh, int triangleIndex, int vertexInde
const b3Vec3* vertices = b3GetMeshVertices( data );
b3Vec3 faceNormal =
b3MakeNormalFromPoints( vertices[triangle.index1], vertices[triangle.index2], vertices[triangle.index3] );
faceNormal = b3Normalize( b3Mul( boundaryScale, faceNormal ) );
faceNormal = b3Normalize( b3Mul( cliffScale, faceNormal ) );
float cosAngle = b3Dot( normal, faceNormal );
return cosAngle > 0.0f && cosAngle * cosAngle >= 1.0f - tolerance * tolerance;
}
@ -1382,21 +1385,21 @@ bool b3IsInMeshVertexRing( const b3Mesh* mesh, int triangleIndex, int vertexInde
b3MakeRingFrame( axis, &tangent1, &tangent2 );
b3Vec3 edgeNormal = b3Mul( edgeScale, normal );
b3Vec3 boundaryNormal = b3Mul( boundaryScale, normal );
b3Vec3 cliffNormal = b3Mul( cliffScale, normal );
b3Vec3 localEdgeNormal = { b3Dot( edgeNormal, tangent1 ), b3Dot( edgeNormal, tangent2 ), b3Dot( edgeNormal, axis ) };
b3Vec3 localBoundaryNormal = { b3Dot( boundaryNormal, tangent1 ), b3Dot( boundaryNormal, tangent2 ), b3Dot( boundaryNormal, axis ) };
b3Vec3 localCliffNormal = { b3Dot( cliffNormal, tangent1 ), b3Dot( cliffNormal, tangent2 ), b3Dot( cliffNormal, axis ) };
for ( int i = 0; i < entryCount; ++i )
{
bool isBoundary = i >= edgeCount;
bool isCliff = i >= edgeCount;
b3Vec3 direction = b3DecodeRingEntry( entries[i] );
float lean = b3Dot( isBoundary ? localBoundaryNormal : localEdgeNormal, direction );
float lean = b3Dot( isCliff ? localCliffNormal : localEdgeNormal, direction );
float limit = tolerance;
if ( uniform == false )
{
b3Vec3 meshDirection = b3MulAdd( b3MulAdd( b3MulSV( direction.x, tangent1 ), direction.y, tangent2 ), direction.z, axis );
limit *= b3Length( b3Mul( isBoundary ? boundaryScale : edgeScale, meshDirection ) );
limit *= b3Length( b3Mul( isCliff ? cliffScale : edgeScale, meshDirection ) );
}
if ( lean > limit )

View file

@ -1667,8 +1667,9 @@ bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact
return true;
}
int b3CollideMeshAndHull( b3Manifold* manifolds, int capacity, const b3Mesh* mesh, const b3HullData* hull,
b3Transform transformBtoA, bool enableSpeculative, bool handleGhosts )
// todo comment
static int b3CollideMeshAndShape( b3Manifold* manifolds, int capacity, const b3Mesh* mesh, const b3Shape* shapeB,
b3AABB localBounds, b3Transform transformBtoA, bool enableSpeculative, bool handleGhosts )
{
if ( capacity <= 0 )
{
@ -1680,19 +1681,14 @@ int b3CollideMeshAndHull( b3Manifold* manifolds, int capacity, const b3Mesh* mes
.mesh = *mesh,
};
b3Shape hullShape = {
.type = b3_hullShape,
.hull = hull,
};
b3Arena arena = b3CreateArena( 64 * 1024 );
float margin = B3_SPECULATIVE_DISTANCE;
b3Vec3 extension = { margin, margin, margin };
b3AABB hullBounds = b3AABB_Transform( transformBtoA, hull->aabb );
b3AABB shapeBounds = b3AABB_Transform( transformBtoA, localBounds );
b3AABB queryBounds = {
.lowerBound = b3Sub( hullBounds.lowerBound, extension ),
.upperBound = b3Add( hullBounds.upperBound, extension ),
.lowerBound = b3Sub( shapeBounds.lowerBound, extension ),
.upperBound = b3Add( shapeBounds.upperBound, extension ),
};
b3TriangleQueryContext queryContext = {
@ -1714,8 +1710,8 @@ int b3CollideMeshAndHull( b3Manifold* manifolds, int capacity, const b3Mesh* mes
b3Transform transformAtoB = b3InvertTransform( transformBtoA );
b3Cluster* clusters;
int clusterCount = b3CollideMeshTriangles( &clusters, &meshShape, &hullShape, transformAtoB, triangleCaches,
triangleCount, enableSpeculative, handleGhosts, false, NULL, &arena );
int clusterCount = b3CollideMeshTriangles( &clusters, &meshShape, shapeB, transformAtoB, triangleCaches, triangleCount,
enableSpeculative, handleGhosts, false, NULL, &arena );
int count = b3MinInt( clusterCount, capacity );
b3Matrix3 matrixB = b3MakeMatrixFromQuat( transformBtoA.q );
@ -1746,3 +1742,29 @@ int b3CollideMeshAndHull( b3Manifold* manifolds, int capacity, const b3Mesh* mes
b3DestroyArena( &arena );
return count;
}
int b3CollideMeshAndHull( b3Manifold* manifolds, int capacity, const b3Mesh* mesh, const b3HullData* hull,
b3Transform transformBtoA, bool enableSpeculative, bool handleGhosts )
{
b3Shape hullShape = {
.type = b3_hullShape,
.hull = hull,
};
return b3CollideMeshAndShape( manifolds, capacity, mesh, &hullShape, hull->aabb, transformBtoA, enableSpeculative,
handleGhosts );
}
int b3CollideMeshAndSphere( b3Manifold* manifolds, int capacity, const b3Mesh* mesh, const b3Sphere* sphere,
b3Transform transformBtoA, bool enableSpeculative, bool handleGhosts )
{
b3Shape sphereShape = {
.type = b3_sphereShape,
.sphere = *sphere,
};
b3Vec3 extent = { sphere->radius, sphere->radius, sphere->radius };
b3AABB bounds = { b3Sub( sphere->center, extent ), b3Add( sphere->center, extent ) };
return b3CollideMeshAndShape( manifolds, capacity, mesh, &sphereShape, bounds, transformBtoA, enableSpeculative,
handleGhosts );
}