diff --git a/samples/sample_manifold.cpp b/samples/sample_manifold.cpp index f1b35d1..a3875f9 100644 --- a/samples/sample_manifold.cpp +++ b/samples/sample_manifold.cpp @@ -2110,7 +2110,7 @@ static void AddGhostTriangle( GhostMesh& mesh, int a, int b, int c, b3Vec3 facin class GhostManifold : public Sample { public: - static constexpr int m_maxScenes = 16; + static constexpr int m_maxScenes = 20; static constexpr int m_manifoldCapacity = 32; static constexpr int m_triangleCapacity = 64; static constexpr int m_trianglePointCapacity = 64; @@ -2875,6 +2875,26 @@ static void BuildGhostSaddleRidge( std::vector& meshes ) meshes.push_back( mesh ); } +// The ghost oracle wave, 3 m square with 25 cm cells +static void BuildGhostWave( std::vector& meshes ) +{ + b3MeshData* data = CreateOracleMesh( e_oracleWave ); + const b3Vec3* vertices = b3GetMeshVertices( data ); + const b3MeshTriangle* triangles = b3GetMeshTriangles( data ); + + GhostMesh mesh; + mesh.vertices.assign( vertices, vertices + data->vertexCount ); + for ( int i = 0; i < data->triangleCount; ++i ) + { + mesh.indices.push_back( triangles[i].index1 ); + mesh.indices.push_back( triangles[i].index2 ); + mesh.indices.push_back( triangles[i].index3 ); + } + + b3DestroyMesh( data ); + meshes.push_back( mesh ); +} + static const float s_inch = 0.0254f; static const GhostScene s_hullGhostScenes[] = { @@ -3030,6 +3050,58 @@ static const GhostScene s_hullGhostScenes[] = { { 0.0f, 0.0f, 0.0f }, true, }, + { + "Open boundary, snapped neighbors", + "box rolled 44 degrees over the open edge of the strip, its low edge crosses the boundary 1.6 mm " + "up. Its own triangle gives a face contact and the neighbor triangles pick a box side face", + "the neighbors snap to the strip normal and join the face cluster with points 19 to 21 cm up the " + "box side, so the cluster reduction keeps those over the touching point unless the deepest point " + "is put back", + BuildGhostStrip, + { 0.145f, 0.225f, 0.155f }, + { -0.071f, 0.26f, 0.023f }, + { 5.0f, -23.0f, 44.0f }, + true, + }, + { + "Open boundary, rolled", + "box rolled 40 degrees over the open edge of the strip, its low edge 4 mm from the boundary. The " + "edge axis wins SAT by 0.1 mm but the box bottom face is kept for its points, leaning 12 degrees " + "along the boundary edge", + "the leaning face snaps to the strip normal, whose clip reaches 29 cm up the box face. A boundary " + "edge has no neighbor, so the GJK contact at the edge replaces the snapped clip with one point " + "and the edge normal", + BuildGhostStrip, + { 0.2f, 0.1f, 0.2f }, + { 0.0f, 0.184f, 0.25f }, + { -15.0f, 0.0f, 40.0f }, + true, + }, + { + "Rail end", + "box rolled 30 degrees against the open end of the rail, its corner edge 4 mm from the wall end " + "edge, SAT picks a box face whose normal is not perpendicular to that edge", + "the face normal fails the edge test and snaps to the wall face, whose clip is empty beside the " + "rail. A boundary edge has no neighbor to take the contact, so the GJK normal at the edge " + "replaces it", + BuildGhostRail, + { 0.2f, 0.2f, 0.2f }, + { 0.527f, 0.3f, 0.0f }, + { 0.0f, 0.0f, -30.0f }, + true, + }, + { + "Wave, resting cull", + "box on a wave resting within 1 degree of one triangle 12 mm away, the closest triangles are 7 mm " + "away, tilted about 5 degrees and below the lowest box corner along the resting normal", + "the closest triangles snap to their own faces. The resting cull only drops triangles farther " + "than the resting contact, otherwise these drop and the only contact left is 5 mm too far", + BuildGhostWave, + { 0.35f, 0.17f, 0.15f }, + { 0.532f, 0.198f, -1.175f }, + { -3.0f, 77.0f, -6.0f }, + true, + }, }; class HullGhostManifold : public GhostManifold diff --git a/shared/ghost_collisions.c b/shared/ghost_collisions.c index 35e3b8a..226ccd3 100644 --- a/shared/ghost_collisions.c +++ b/shared/ghost_collisions.c @@ -14,6 +14,14 @@ #define GHOST_HALF_WIDTH 3.0f #define GHOST_START_X -16.0f +// The rail deflects the body sideways and the floor is frictionless, so a narrow floor ends the run off +// the side instead of at the far end +#define GHOST_RAIL_HALF_LENGTH ( 0.5f * GHOST_HALF_LENGTH ) +#define GHOST_RAIL_HALF_WIDTH ( 2.0f * GHOST_HALF_WIDTH ) + +// A run takes about 144 steps, clear of 150 where the 900 step scan would flip between 5 and 6 runs +#define GHOST_RAIL_START_X -5.3f + // A 34 degree chamfer like the s&box beams #define GHOST_CHAMFER_WIDTH 0.05f #define GHOST_CHAMFER_DROP ( GHOST_CHAMFER_WIDTH / 1.5f ) @@ -156,10 +164,10 @@ static void AddStrip( MeshBuilder* builder, const b3Vec2* profile, int count, fl // The rail cross section is a profile in zy extruded along x. Each quad side is its edge direction along // the section crossed with x, which is the outward normal for a section traversed with air on the left. -static void AddRail( MeshBuilder* builder, const b3Vec2* section, int count, float cellSize, bool mirror ) +static void AddRail( MeshBuilder* builder, const b3Vec2* section, int count, float halfLength, float cellSize, bool mirror ) { float sign = mirror ? -1.0f : 1.0f; - int xCount = b3MaxInt( 1, (int)ceilf( 2.0f * GHOST_HALF_LENGTH / cellSize - 0.01f ) ); + int xCount = b3MaxInt( 1, (int)ceilf( 2.0f * halfLength / cellSize - 0.01f ) ); int stride = xCount + 1; int pointCount = 1; @@ -193,7 +201,7 @@ static void AddRail( MeshBuilder* builder, const b3Vec2* section, int count, flo float y = a.y + t * ( b.y - a.y ); for ( int j = 0; j <= xCount; ++j ) { - float x = -GHOST_HALF_LENGTH + 2.0f * GHOST_HALF_LENGTH * (float)j / (float)xCount; + float x = -halfLength + 2.0f * halfLength * (float)j / (float)xCount; builder->vertices[index++] = (b3Vec3){ sign * x, y, z }; } } @@ -401,9 +409,11 @@ void CreateGhostGround( b3WorldId worldId, GhostCollisionsData* data ) data->groundId = b3CreateBody( worldId, &bodyDef ); const GhostSettings* settings = &data->settings; - float h = GHOST_HALF_LENGTH; + bool rail = settings->scene == e_ghostRail; + float h = rail ? GHOST_RAIL_HALF_LENGTH : GHOST_HALF_LENGTH; data->profileCount = 0; - data->halfWidth = GHOST_HALF_WIDTH; + data->halfLength = h; + data->halfWidth = rail ? GHOST_RAIL_HALF_WIDTH : GHOST_HALF_WIDTH; switch ( settings->scene ) { @@ -467,7 +477,7 @@ void CreateGhostGround( b3WorldId worldId, GhostCollisionsData* data ) case e_ghostRail: // Cross section in zy, from the floor up the inner wall, over the top and down the outer skirt - PushProfile( data, -GHOST_HALF_WIDTH, 0.0f ); + PushProfile( data, -data->halfWidth, 0.0f ); PushProfile( data, 0.0f, 0.0f ); PushProfile( data, 0.0f, GHOST_RAIL_HEIGHT ); PushProfile( data, GHOST_RAIL_THICKNESS, GHOST_RAIL_HEIGHT ); @@ -483,7 +493,7 @@ void CreateGhostGround( b3WorldId worldId, GhostCollisionsData* data ) MeshBuilder builder = { 0 }; float cellSize = settings->cellSize; bool mirror = settings->mirror; - float groundHalfWidth = GHOST_HALF_WIDTH; + float groundHalfWidth = data->halfWidth; int cellCountZ = GetCellCount( 2.0f * groundHalfWidth, cellSize ); if ( settings->heightField ) @@ -515,7 +525,7 @@ void CreateGhostGround( b3WorldId worldId, GhostCollisionsData* data ) } else if ( settings->scene == e_ghostRail ) { - AddRail( &builder, data->profile, data->profileCount, cellSize, mirror ); + AddRail( &builder, data->profile, data->profileCount, h, cellSize, mirror ); AddMesh( data, &builder, false ); } else if ( settings->scene == e_ghostInternal ) @@ -574,7 +584,7 @@ void LaunchGhostBody( b3WorldId worldId, GhostCollisionsData* data ) // contact is edge on edge and perpendicular to the rail, a face normal there leans along the rail // and catches the body. Found in the Conveyor Circuit sample. b3Quat pitch = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, 60.0f * B3_PI / 180.0f ); - bodyDef.position = (b3Pos){ GHOST_START_X, GHOST_RAIL_HEIGHT + 0.45f, -0.15f }; + bodyDef.position = (b3Pos){ GHOST_RAIL_START_X, GHOST_RAIL_HEIGHT + 0.45f, -0.15f }; bodyDef.rotation = b3MulQuat( pitch, yaw ); bodyDef.linearVelocity = (b3Vec3){ settings->speed, 0.0f, 0.0f }; } @@ -753,7 +763,7 @@ static bool IsGhost( const GhostCollisionsData* data, b3Vec3 normal, float xMin, int UpdateGhostCollisions( b3WorldId worldId, GhostCollisionsData* data, GhostReport* reports, int capacity ) { b3Pos center = b3Body_GetWorldCenter( data->bodyId ); - bool done = center.x > GHOST_HALF_LENGTH - 1.0f || center.y < -3.0f; + bool done = center.x > data->halfLength - 1.0f || center.y < -3.0f; if ( data->settings.scene == e_ghostInternal ) { // Run out onto the ground plane, or give up on a body knocked off the side diff --git a/shared/ghost_collisions.h b/shared/ghost_collisions.h index 603ab90..c307bc4 100644 --- a/shared/ghost_collisions.h +++ b/shared/ghost_collisions.h @@ -93,7 +93,8 @@ typedef struct GhostCollisionsData b3Vec2 profile[GHOST_MAX_PROFILE_POINTS]; int profileCount; - // The ground spans z in [-halfWidth, halfWidth] + // The ground spans x in [-halfLength, halfLength] and z in [-halfWidth, halfWidth] + float halfLength; float halfWidth; b3MeshData* meshes[2]; int meshCount; diff --git a/src/mesh_contact.c b/src/mesh_contact.c index 9861334..a555dcd 100644 --- a/src/mesh_contact.c +++ b/src/mesh_contact.c @@ -487,6 +487,73 @@ static int b3DropInteriorPoints( b3Point2D* points, int count, float margin ) #endif +// Fallback if culling leads to all points separated. Rare. +static void b3KeepDeepestPoint( b3Point2D* kept, int keptCount, const b3LocalManifoldPoint* points, int count, + b3Vec3 origin, b3Vec3 u, b3Vec3 v ) +{ + int deepest = 0; + for ( int i = 1; i < count; ++i ) + { + if ( points[i].separation < points[deepest].separation ) + { + deepest = i; + } + } + + float keptSeparation = FLT_MAX; + for ( int i = 0; i < keptCount; ++i ) + { + if ( kept[i].originalIndex == deepest ) + { + return; + } + + keptSeparation = b3MinFloat( keptSeparation, points[kept[i].originalIndex].separation ); + } + + float separation = points[deepest].separation; + if ( separation <= 0.0f || separation >= keptSeparation - B3_LINEAR_SLOP ) + { + return; + } + + int replaceIndex = 0; + float bestArea = -1.0f; + for ( int candidate = 0; candidate < keptCount; ++candidate ) + { + b3Vec2 ring[B3_MAX_MANIFOLD_POINTS]; + int ringCount = 0; + for ( int i = 0; i < keptCount; ++i ) + { + int index = i == candidate ? deepest : kept[i].originalIndex; + b3Vec3 d = b3Sub( points[index].point, origin ); + ring[ringCount] = (b3Vec2){ b3Dot( d, u ), b3Dot( d, v ) }; + ringCount += 1; + } + + float area = 0.0f; + for ( int i = 0; i < ringCount; ++i ) + { + for ( int j = i + 1; j < ringCount; ++j ) + { + for ( int k = j + 1; k < ringCount; ++k ) + { + float signedArea = b3Cross2( b3Sub2( ring[j], ring[i] ), b3Sub2( ring[k], ring[i] ) ); + area = b3MaxFloat( area, b3AbsFloat( signedArea ) ); + } + } + } + + if ( area > bestArea ) + { + bestArea = area; + replaceIndex = candidate; + } + } + + kept[replaceIndex].originalIndex = deepest; +} + static int b3ReduceCluster( b3LocalManifoldPoint* points, int count1, b3Vec3 normal, b3Arena arena ) { int targetCount = 1; @@ -528,6 +595,8 @@ static int b3ReduceCluster( b3LocalManifoldPoint* points, int count1, b3Vec3 nor #endif B3_ASSERT( count2 <= B3_MAX_MANIFOLD_POINTS ); + b3KeepDeepestPoint( hull, count2, points, count1, origin, u, v ); + b3LocalManifoldPoint finalPoints[B3_MAX_MANIFOLD_POINTS]; for ( int i = 0; i < count2; ++i ) { @@ -1008,6 +1077,80 @@ static b3NeighborResult b3TestHullFaceNeighbors( const b3Shape* shape, int trian return result == b3_neighborSkip || result == b3_neighborDrop ? b3_neighborSnap : result; } +// Special handling if a boundary collision drops all points. Rare. +static void b3CollideBoundaryEdge( b3LocalManifold* manifold, const b3Shape* shapeA, int triangleIndex, + const b3Vec3* vertices, b3Matrix3 meshToLocal, const b3HullData* hull, + float speculativeDistance ) +{ + float linearSlop = B3_LINEAR_SLOP; + + b3DistanceInput input = { 0 }; + input.proxyA = (b3ShapeProxy){ vertices, 3, 0.0f }; + input.proxyB = (b3ShapeProxy){ b3GetHullPoints( hull ), hull->vertexCount, 0.0f }; + input.transform = b3Transform_identity; + input.useRadii = false; + b3SimplexCache simplexCache = { 0 }; + b3DistanceOutput output = b3ShapeDistance( &input, &simplexCache, NULL, 0 ); + + if ( output.distance < B3_OVERLAP_SLOP || output.distance > speculativeDistance ) + { + return; + } + + float tolerance = 0.01f * linearSlop; + b3TriangleFeature feature = b3_featureNone; + bool onBoundary = false; + b3TriangleFeature vertexFeatures[] = { b3_featureVertex1, b3_featureVertex2, b3_featureVertex3 }; + b3TriangleFeature edgeFeatures[] = { b3_featureEdge1, b3_featureEdge2, b3_featureEdge3 }; + for ( int i = 0; i < 3; ++i ) + { + b3Vec3 a = vertices[i]; + b3Vec3 e = b3Sub( vertices[i < 2 ? i + 1 : 0], a ); + float t = b3ClampFloat( b3Dot( b3Sub( output.pointA, a ), e ) / b3LengthSquared( e ), 0.0f, 1.0f ); + b3Vec3 closest = b3MulAdd( a, t, e ); + if ( b3DistanceSquared( closest, output.pointA ) >= tolerance * tolerance ) + { + continue; + } + + b3Vec3 neighborNormal; + onBoundary = onBoundary || b3GetNeighborNormal( shapeA, triangleIndex, i, &neighborNormal ) == false; + + if ( b3DistanceSquared( a, output.pointA ) < tolerance * tolerance ) + { + feature = vertexFeatures[i]; + } + else if ( b3DistanceSquared( b3Add( a, e ), output.pointA ) < tolerance * tolerance ) + { + feature = vertexFeatures[i < 2 ? i + 1 : 0]; + } + else if ( feature == b3_featureNone ) + { + feature = edgeFeatures[i]; + } + } + + if ( onBoundary == false || feature == b3_featureNone ) + { + return; + } + + int ownerEdge; + if ( b3TestNeighbors( shapeA, triangleIndex, vertices, meshToLocal, feature, output.normal, &ownerEdge ) != + b3_neighborAdmit ) + { + return; + } + + b3LocalManifoldPoint* point = manifold->points + 0; + point->point = b3Lerp( output.pointA, output.pointB, 0.5f ); + point->separation = output.distance; + point->pair = b3MakeFeaturePair( b3_featureShapeA, (int)feature, b3_featureShapeA, (int)feature ); + manifold->normal = output.normal; + manifold->pointCount = 1; + manifold->feature = feature; +} + static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shapeA, const b3Shape* shapeB, b3Transform transformAtoB, b3TriangleCache* triangleCaches, int triangleCount, bool enableSpeculative, bool handleGhosts, bool isFast, b3TaskContext* context, @@ -1309,6 +1452,12 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape meshFlags |= b3_meshContactSnapped; } + float rawSeparation = FLT_MAX; + for ( int i = 0; i < manifold->pointCount; ++i ) + { + rawSeparation = b3MinFloat( rawSeparation, manifold->points[i].separation ); + } + if ( result == b3_neighborSkip || result == b3_neighborSnap || result == b3_neighborDrop ) { // Skip leaves the normal to the neighbor. A sphere off the face cannot snap. @@ -1325,6 +1474,11 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape { b3BuildTriangleAndHullFaceContact( manifold, pointCapacity, vertices, hullB, &cache->satCache, enableSpeculative ); + if ( rawSeparation > 0.0f ) + { + b3CollideBoundaryEdge( manifold, shapeA, triangleIndex, vertices, relativeMatrix, hullB, + hullSpeculativeDistance ); + } } } @@ -1389,6 +1543,15 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape hullBottom = b3MinFloat( hullBottom, b3Dot( normal, hullPoints[i] ) ); } + float restingSeparation = FLT_MAX; + for ( int i = 0; i < resting->pointCount; ++i ) + { + restingSeparation = b3MinFloat( restingSeparation, resting->points[i].separation ); + } + + // Padding + hullBottom -= b3MaxFloat( 0.0f, restingSeparation ) + linearSlop; + for ( int i = 0; i < survivorCount; ++i ) { if ( survivorManifolds[i] == B3_NULL_INDEX ) diff --git a/test/test_determinism.c b/test/test_determinism.c index 16a3975..9a3f5fd 100644 --- a/test/test_determinism.c +++ b/test/test_determinism.c @@ -21,25 +21,25 @@ #if defined( BOX3D_DOUBLE_PRECISION ) #define RAGDOLL_SLEEP_STEP 282 #define RAGDOLL_HASH 0x6E4B13AD -#define WAVE_PILE_SLEEP_STEP 349 -#define WAVE_PILE_HASH 0x9AD0EB48 +#define WAVE_PILE_SLEEP_STEP 315 +#define WAVE_PILE_HASH 0x15007937 #define QUERY_SPAWN_SLEEP_STEP 242 #define QUERY_SPAWN_HASH 0x489585E6 #define QUERY_SPAWN_HIT_COUNT 59 #define QUERY_SPAWN_QUERY_HASH 0x5B4429DC #define MESH_DROP_SLEEP_STEP 197 -#define MESH_DROP_HASH 0xBCCE1A08 +#define MESH_DROP_HASH 0x5DA091C3 #else #define RAGDOLL_SLEEP_STEP 277 #define RAGDOLL_HASH 0x5E042B6E -#define WAVE_PILE_SLEEP_STEP 353 -#define WAVE_PILE_HASH 0xC1BEDCBA +#define WAVE_PILE_SLEEP_STEP 455 +#define WAVE_PILE_HASH 0x854F313D #define QUERY_SPAWN_SLEEP_STEP 242 #define QUERY_SPAWN_HASH 0xF3AD9BA6 #define QUERY_SPAWN_HIT_COUNT 59 #define QUERY_SPAWN_QUERY_HASH 0xE3271F3D #define MESH_DROP_SLEEP_STEP 197 -#define MESH_DROP_HASH 0x4EC0324E +#define MESH_DROP_HASH 0x96D47D4F #endif // The goldens above pin exact values for the default four point manifold. A build that diff --git a/test/test_mesh.c b/test/test_mesh.c index b777503..6c436cb 100644 --- a/test/test_mesh.c +++ b/test/test_mesh.c @@ -1315,13 +1315,12 @@ static int MeshVertexRing( void ) // counts belong to the first ORACLE_SCAN_POSES poses, so a change to a scene or to the pose stream moves // them without the collider changing. Lower a budget when a case is fixed. // -// The two misses examined so far: the cluster reduction keeps spread rather than the deepest point, so a -// snapped neighbor joining the cluster pushed the deepest point out (flat pose 416), and a contact -// against a boundary edge from the side was dropped (crest pose 377). +// The remaining ghost, wave pose 291, is a hull face normal admitted by a vertex cone with the deepest +// point half a linear slop from the vertex, where the oracle only accepts the normal at the vertex. static int MeshGhostOracle( void ) { int ghostBudget[e_oracleSceneCount] = { 0, 0, 0, 0, 0, 0, 0, 1 }; - int missedBudget[e_oracleSceneCount] = { 1, 0, 1, 3, 2, 1, 1, 3 }; + int missedBudget[e_oracleSceneCount] = { 0, 0, 0, 0, 0, 0, 0, 0 }; for ( int scene = 0; scene < e_oracleSceneCount; ++scene ) {