diff --git a/benchmark/ghost_baseline.csv b/benchmark/ghost_baseline.csv index 99cb9d7..3d7f972 100644 --- a/benchmark/ghost_baseline.csv +++ b/benchmark/ghost_baseline.csv @@ -49,16 +49,6 @@ bumps,0,1,capsule,0,1 bumps,0,1,upright,0,1 bumps,0,1,sphere,0,1 bumps,0,1,cylinder,0,1 -tjunction,0,0,box,0,2 -tjunction,0,0,capsule,0,2 -tjunction,0,0,upright,0,2 -tjunction,0,0,sphere,0,2 -tjunction,0,0,cylinder,0,2 -tjunction,0,1,box,0,2 -tjunction,0,1,capsule,0,2 -tjunction,0,1,upright,0,2 -tjunction,0,1,sphere,0,2 -tjunction,0,1,cylinder,0,2 peel,0,0,box,0,2 peel,0,0,capsule,0,2 peel,0,0,upright,0,2 @@ -103,13 +93,13 @@ chamfer,0,0,box,0,2 chamfer,0,0,cylinder,0,2 chamfer,0,1,box,0,2 chamfer,0,1,cylinder,0,2 -rail,0,0,box,36,5 +rail,0,0,box,0,2 rail,0,0,capsule,0,2 rail,0,0,upright,0,5 rail,0,0,sphere,0,4 -rail,0,0,cylinder,8,3 -rail,0,1,box,36,5 +rail,0,0,cylinder,0,3 +rail,0,1,box,0,2 rail,0,1,capsule,0,2 rail,0,1,upright,0,5 rail,0,1,sphere,0,4 -rail,0,1,cylinder,8,3 +rail,0,1,cylinder,0,3 diff --git a/benchmark/ghost_baseline_hf.csv b/benchmark/ghost_baseline_hf.csv index 15d89b1..932a909 100644 --- a/benchmark/ghost_baseline_hf.csv +++ b/benchmark/ghost_baseline_hf.csv @@ -49,16 +49,6 @@ bumps,0,1,capsule,0,1 bumps,0,1,upright,0,1 bumps,0,1,sphere,0,1 bumps,0,1,cylinder,0,1 -tjunction,0,0,box,0,2 -tjunction,0,0,capsule,0,2 -tjunction,0,0,upright,0,2 -tjunction,0,0,sphere,0,2 -tjunction,0,0,cylinder,3,2 -tjunction,0,1,box,1,0 -tjunction,0,1,capsule,0,2 -tjunction,0,1,upright,0,2 -tjunction,0,1,sphere,0,2 -tjunction,0,1,cylinder,0,2 peel,0,0,box,0,2 peel,0,0,capsule,0,2 peel,0,0,upright,0,2 diff --git a/benchmark/ghost_scan.c b/benchmark/ghost_scan.c index e917019..56cd9dd 100644 --- a/benchmark/ghost_scan.c +++ b/benchmark/ghost_scan.c @@ -18,7 +18,7 @@ #include // Command line and result file keys -static const char* s_sceneKeys[e_ghostSceneCount] = { "flat", "ramp", "cliff", "crest", "bumps", "tjunction", "peel", "chamfer", "rail" }; +static const char* s_sceneKeys[e_ghostSceneCount] = { "flat", "ramp", "cliff", "crest", "bumps", "peel", "chamfer", "rail" }; static const char* s_bodyKeys[e_ghostBodyCount] = { "box", "capsule", "upright", "sphere", "cylinder" }; #define MAX_CONFIGS 256 @@ -372,7 +372,7 @@ int main( int argc, char** argv ) { printf( "Usage: ghost_scan [options]\n" "Runs the Ghost Collisions sample headless and counts ghost manifolds per scene and body.\n" - " -s, --scene= flat, ramp, cliff, crest, bumps, tjunction, peel, chamfer, rail (default all)\n" + " -s, --scene= flat, ramp, cliff, crest, bumps, peel, chamfer, rail (default all)\n" " chamfer runs only the box and cylinder, fixed rotation without speculative\n" " -b, --body= box, capsule, upright, sphere, cylinder (default all)\n" " -m, --mirror=<0|1|all> mirrored mesh (default all)\n" @@ -381,7 +381,7 @@ int main( int argc, char** argv ) " -w, --write= write results as csv, replacing only the rows of this run\n" " -c, --compare= compare with a written csv, fail if any count grew or has no row\n" " --speed=, --yaw=, --friction=, --cell=, --fixed-rotation, --no-speculative\n" - " --height-field height field ground, the t-junction becomes a fine and coarse tile seam\n" + " --height-field height field ground, one static body per height field\n" " -v, --verbose print every ghost\n" "Exit code is 1 when a compared count grew or has no row, 0 otherwise.\n" ); return 2; diff --git a/samples/sample.cpp b/samples/sample.cpp index 22c0b35..899f1c3 100644 --- a/samples/sample.cpp +++ b/samples/sample.cpp @@ -1376,8 +1376,52 @@ void Sample::DrawTextLine( const char* text, ... ) char buffer[512]; vsnprintf( buffer, sizeof( buffer ), text, args ); va_end( args ); - DrawScreenString( m_textX, m_textLine, MakeColor( b3_colorWhite ), buffer ); - m_textLine += m_textIncrement; + + // The text arena keeps 63 characters per string, so wrap long lines at spaces and indent the rest + constexpr int capacity = 63; + constexpr int indent = 2; + const char* start = buffer; + bool first = true; + while ( true ) + { + int width = first ? capacity : capacity - indent; + int length = (int)strlen( start ); + int cut = length; + if ( length > width ) + { + cut = width; + while ( cut > 0 && start[cut] != ' ' ) + { + cut -= 1; + } + + if ( cut == 0 ) + { + cut = width; + } + } + + char line[capacity + 1]; + int offset = first ? 0 : indent; + memset( line, ' ', offset ); + memcpy( line + offset, start, cut ); + line[offset + cut] = '\0'; + DrawScreenString( m_textX, m_textLine, MakeColor( b3_colorWhite ), line ); + m_textLine += m_textIncrement; + + start += cut; + while ( *start == ' ' ) + { + start += 1; + } + + if ( *start == '\0' ) + { + break; + } + + first = false; + } } SampleEntry g_sampleEntries[MAX_SAMPLES]; diff --git a/samples/sample_manifold.cpp b/samples/sample_manifold.cpp index 09f8218..f66be02 100644 --- a/samples/sample_manifold.cpp +++ b/samples/sample_manifold.cpp @@ -2129,6 +2129,9 @@ public: { GhostMesh source; std::vector edgeTypes; + + // By engine triangle index, which differs from the source order + std::vector triangleCenters; b3MeshData* data; }; @@ -2152,6 +2155,7 @@ public: m_handleGhosts = true; m_speculative = true; m_drawTriangleManifolds = false; + m_drawTriangleIndices = false; m_cullDistance = 0.5f; m_showGizmo = false; m_gizmoLocal = false; @@ -2220,6 +2224,21 @@ public: mesh.source = source; mesh.data = b3CreateMesh( &meshDef, nullptr, 0 ); ClassifyEdges( mesh ); + + b3AABB bounds = { source.vertices[0], source.vertices[0] }; + for ( b3Vec3 vertex : source.vertices ) + { + bounds.lowerBound = b3Min( bounds.lowerBound, vertex ); + bounds.upperBound = b3Max( bounds.upperBound, vertex ); + } + + b3Vec3 margin = { 0.1f, 0.1f, 0.1f }; + bounds.lowerBound = b3Sub( bounds.lowerBound, margin ); + bounds.upperBound = b3Add( bounds.upperBound, margin ); + + mesh.triangleCenters.assign( meshDef.triangleCount, b3Vec3_zero ); + b3Mesh query = { mesh.data, b3Vec3_one }; + b3QueryMesh( &query, bounds, StoreTriangleCenter, &mesh.triangleCenters ); m_meshes.push_back( mesh ); } @@ -2282,6 +2301,16 @@ public: } } + static bool StoreTriangleCenter( b3Vec3 a, b3Vec3 b, b3Vec3 c, int triangleIndex, void* context ) + { + std::vector* centers = (std::vector*)context; + if ( triangleIndex < (int)centers->size() ) + { + ( *centers )[triangleIndex] = b3MulSV( 1.0f / 3.0f, b3Add( a, b3Add( b, c ) ) ); + } + return true; + } + static bool CollectTriangle( b3Vec3 a, b3Vec3 b, b3Vec3 c, int triangleIndex, void* context ) { GhostManifold* self = (GhostManifold*)context; @@ -2395,6 +2424,27 @@ public: } } + if ( m_drawTriangleIndices ) + { + bool multipleMeshes = m_meshes.size() > 1; + for ( int i = 0; i < (int)m_meshes.size(); ++i ) + { + const std::vector& centers = m_meshes[i].triangleCenters; + for ( int t = 0; t < (int)centers.size(); ++t ) + { + b3Pos center = b3OffsetPos( b3Pos_zero, centers[t] ); + if ( multipleMeshes ) + { + DrawString3D( center, MakeColor( b3_colorLightGray ), "m%d t%d", i, t ); + } + else + { + DrawString3D( center, MakeColor( b3_colorLightGray ), "t%d", t ); + } + } + } + } + DrawShape( m_transform ); float length = 0.3f * b3GetLengthUnitsPerMeter(); @@ -2485,6 +2535,7 @@ public: ImGui::Checkbox( "Ghost Handling", &m_handleGhosts ); ImGui::Checkbox( "Speculative", &m_speculative ); ImGui::Checkbox( "Triangle Manifolds", &m_drawTriangleManifolds ); + ImGui::Checkbox( "Triangle Indices", &m_drawTriangleIndices ); ImGui::SliderFloat( "Cull Distance", &m_cullDistance, 0.0f, 200.0f, "%0.1f cm" ); if ( ImGui::Button( "Reset Pose" ) ) { @@ -2539,6 +2590,7 @@ public: float m_cullDistance; bool m_speculative; bool m_drawTriangleManifolds; + bool m_drawTriangleIndices; bool m_showGizmo; bool m_gizmoLocal; @@ -2615,6 +2667,70 @@ static void BuildGhostTJunction( std::vector& meshes ) meshes.push_back( mesh ); } +// A 90 degree convex ridge along z, two cells along the ridge +static void BuildGhostRidge( std::vector& meshes ) +{ + GhostMesh mesh; + b3Vec2 profile[3] = { { -1.5f, -1.5f }, { 0.0f, 0.0f }, { 1.5f, -1.5f } }; + b3Vec3 facings[2] = { { -1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 0.0f } }; + for ( int k = 0; k < 3; ++k ) + { + for ( int i = 0; i < 3; ++i ) + { + mesh.vertices.push_back( { profile[i].x, profile[i].y, -1.0f + k } ); + } + } + + for ( int k = 0; k < 2; ++k ) + { + for ( int i = 0; i < 2; ++i ) + { + int a = 3 * k + i; + int b = a + 1; + int c = a + 4; + int d = a + 3; + AddGhostTriangle( mesh, a, b, c, facings[i] ); + AddGhostTriangle( mesh, a, c, d, facings[i] ); + } + } + meshes.push_back( mesh ); +} + +// A welded strip with an open boundary along x = 0, like a PEEL slab lying on other ground +static void BuildGhostStrip( std::vector& meshes ) +{ + GhostMesh mesh; + for ( int row = 0; row < 5; ++row ) + { + for ( int column = 0; column < 3; ++column ) + { + mesh.vertices.push_back( { 0.5f * column, 0.0f, -1.0f + 0.5f * row } ); + } + } + + for ( int row = 0; row < 4; ++row ) + { + for ( int column = 0; column < 2; ++column ) + { + int a = 3 * row + column; + int b = a + 1; + int c = a + 4; + int d = a + 3; + if ( ( row + column ) & 1 ) + { + AddGhostTriangle( mesh, a, d, b ); + AddGhostTriangle( mesh, d, c, b ); + } + else + { + AddGhostTriangle( mesh, a, d, c ); + AddGhostTriangle( mesh, c, b, a ); + } + } + } + meshes.push_back( mesh ); +} + // s&box units are inches. Beam top at y = 0 for x > 0, chamfer dropping 1 inch over 1.5 inch for x // < 0, 8 inch cells along z with the chamfer quads split like the s&box sample. A side depth in inches // adds a vertical face below the chamfer, so the chamfer bottom edge is convex instead of a boundary. @@ -2764,6 +2880,30 @@ static const GhostScene s_hullGhostScenes[] = { { 0.0f, 0.0f, -20.0f }, true, }, + { + "Concave edge, deep", + "box on the left slope tilted 18 degrees and 2 cm deep, the right triangle picks the box side " + "face 11 mm deep", + "concave edge, snap to the triangle face, which has no points here, so the left triangle owns " + "the contact", + BuildGhostValley, + { 0.5f, 0.5f, 0.5f }, + { -0.314f, 0.61f, 0.0f }, + { 0.0f, 0.0f, -18.0f }, + true, + }, + { + "Ridge, deep", + "box corner jammed on a 90 degree ridge, the apex is 3 cm inside the box, 28 mm into the left " + "slope and 34 mm into the right", + "deep, the right triangle picks an edge pair, a convex skip, and its snapped face contact is " + "empty. The left triangles keep their own normal, which pushes the box off the apex", + BuildGhostRidge, + { 0.5f, 0.5f, 0.5f }, + { -0.564f, 0.579f, -0.016f }, + { -32.0f, -20.0f, 8.0f }, + true, + }, { "T-junction", "a vertex in the middle of the left triangle edge, no edge matches so all are boundary " @@ -2776,6 +2916,18 @@ static const GhostScene s_hullGhostScenes[] = { { 0.0f, 45.0f, 0.0f }, true, }, + { + "Open boundary", + "box hanging over the open edge of a welded strip, yawed 5 degrees and 1 cm deep, its leading " + "face 5 mm past the row edge at the boundary vertex", + "unknown, the boundary edge has no neighbor, the next row triangle at the vertex blocks the " + "normal and has its own contact so it is dropped", + BuildGhostStrip, + { 0.5f, 0.5f, 0.5f }, + { -0.25f, 0.49f, 0.019f }, + { 0.0f, 5.0f, 0.0f }, + true, + }, { "Chamfer", "none without handling, SAT picks the box bottom for the chamfer triangles, 0.07 inch deep", diff --git a/shared/ghost_collisions.c b/shared/ghost_collisions.c index 5c28b43..24f64e3 100644 --- a/shared/ghost_collisions.c +++ b/shared/ghost_collisions.c @@ -38,7 +38,7 @@ static const PeelPreset s_peelPresets[GHOST_PEEL_PRESET_COUNT] = { }; const char* g_ghostSceneNames[e_ghostSceneCount] = { - "Flat", "Concave", "Cliff", "Crest", "Bumps", "T-Junction", "Internal", "Chamfer", "Rail", + "Flat", "Concave", "Cliff", "Crest", "Bumps", "Internal", "Chamfer", "Rail", }; const char* g_ghostBodyNames[e_ghostBodyCount] = { "Box", "H Capsule", "V Capsule", "Sphere", "Cylinder" }; @@ -484,8 +484,6 @@ void CreateGhostGround( b3WorldId worldId, GhostCollisionsData* data ) } MeshBuilder builder = { 0 }; - b3Vec2 left[2] = { { -h, 0.0f }, { 0.0f, 0.0f } }; - b3Vec2 right[2] = { { 0.0f, 0.0f }, { h, 0.0f } }; float cellSize = settings->cellSize; bool mirror = settings->mirror; float groundHalfWidth = GHOST_HALF_WIDTH; @@ -493,13 +491,7 @@ void CreateGhostGround( b3WorldId worldId, GhostCollisionsData* data ) if ( settings->heightField ) { - if ( settings->scene == e_ghostTJunction ) - { - AddHeightField( worldId, data, -h, 0.0f, GetCellCount( h, cellSize ), groundHalfWidth, - GetCellCount( 2.0f * groundHalfWidth, 0.5f * cellSize ), false ); - AddHeightField( worldId, data, 0.0f, h, GetCellCount( h, cellSize ), groundHalfWidth, cellCountZ, false ); - } - else if ( settings->scene == e_ghostInternal ) + if ( settings->scene == e_ghostInternal ) { // One grid line per profile row, as in the mesh PeelPreset preset = s_peelPresets[settings->peelPreset]; @@ -529,13 +521,6 @@ void CreateGhostGround( b3WorldId worldId, GhostCollisionsData* data ) AddRail( &builder, data->profile, data->profileCount, cellSize, mirror ); AddMesh( data, &builder, false ); } - else if ( settings->scene == e_ghostTJunction ) - { - // Fine and coarse strips share only every other vertex along x = 0 - AddStrip( &builder, left, 2, cellSize, 0.5f * cellSize, mirror ); - AddStrip( &builder, right, 2, cellSize, cellSize, mirror ); - AddMesh( data, &builder, false ); - } else if ( settings->scene == e_ghostInternal ) { // The mesh has no friction and the ground plane under it does, as in PEEL diff --git a/shared/ghost_collisions.h b/shared/ghost_collisions.h index 6aaad1e..74b6612 100644 --- a/shared/ghost_collisions.h +++ b/shared/ghost_collisions.h @@ -26,7 +26,6 @@ typedef enum GhostScene e_ghostCliff, e_ghostCrest, e_ghostBumps, - e_ghostTJunction, e_ghostInternal, e_ghostChamfer, e_ghostRail, @@ -60,8 +59,7 @@ typedef struct GhostSettings int peelPreset; bool mirror; - // Build the ground from height fields instead of meshes. Each height field is its own shape, so the - // T-junction scene becomes a seam between a fine and a coarse tile. + // Build the ground from height fields instead of meshes. Each height field is its own shape. bool heightField; float cellSize; diff --git a/src/mesh_contact.c b/src/mesh_contact.c index 63d3afe..d827a61 100644 --- a/src/mesh_contact.c +++ b/src/mesh_contact.c @@ -829,7 +829,7 @@ static bool b3ShouldBlockNormal( const b3Vec3* vertices, b3BlockTarget target, b } } - return onEdge == false || ( infringed && allInfringed ); + return onEdge && infringed && allInfringed; } static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shapeA, const b3Shape* shapeB, @@ -1027,6 +1027,8 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape int* survivorManifolds = b3Bump( arena, survivorCount * sizeof( int ) ); memset( survivorManifolds, 0xFF, survivorCount * sizeof( int ) ); + bool* snappedEmpty = b3Bump( arena, survivorCount * sizeof( bool ) ); + memset( snappedEmpty, 0, survivorCount * sizeof( bool ) ); b3BlockSource* blockSources = b3Bump( arena, survivorCount * sizeof( b3BlockSource ) ); int blockSourceCount = 0; @@ -1142,17 +1144,6 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape meshFlags = b3_meshContactSnapped; } - b3LocalManifold deepManifold = { 0 }; - b3LocalManifoldPoint deepPoints[B3_MAX_CLIP_POINTS]; - bool keepDeep = deep && result == b3_neighborSnap && shapeB->type == b3_hullShape; - if ( keepDeep ) - { - B3_ASSERT( manifold->pointCount <= B3_MAX_CLIP_POINTS ); - deepManifold = *manifold; - memcpy( deepPoints, manifold->points, - manifold->pointCount * sizeof( b3LocalManifoldPoint ) ); - } - if ( result == b3_neighborSkip || result == b3_neighborSnap ) { // Skip leaves the normal to the neighbor. A sphere off the face cannot snap. @@ -1169,23 +1160,10 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape { b3BuildTriangleAndHullFaceContact( manifold, pointCapacity, vertices, triangle.flags, hullB, &cache->satCache, enableSpeculative ); - - if ( keepDeep && manifold->pointCount == 0 ) - { - // A deep hull off the triangle face keeps its own manifold. - *manifold = deepManifold; - memcpy( manifold->points, deepPoints, - manifold->pointCount * sizeof( b3LocalManifoldPoint ) ); - - // The kept manifold failed the neighbor test, so it is unverified like a boundary contact. - blockSource = true; - meshFlags = b3_meshContactDeep | b3_meshContactUnverified; - if ( manifold->feature == b3_featureHullFace ) - { - meshFlags |= b3_meshContactHullFace; - } - } } + + // todo comment + snappedEmpty[survivorIndex] = result == b3_neighborSnap && manifold->pointCount == 0; } int manifoldPointCount = manifold->pointCount; @@ -1390,7 +1368,9 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape } else { - b3Vec3 blockerNormal = blockTargets[source->blockerIndex].normal; + // todo comment + b3Vec3 blockerNormal = snappedEmpty[source->blockerIndex] ? manifold->triangleNormal + : blockTargets[source->blockerIndex].normal; manifold->normal = blockerNormal; manifold->triangleNormal = blockerNormal; manifold->feature = b3_featureTriangleFace; diff --git a/test/test_mesh.c b/test/test_mesh.c index b57c36c..5d2e365 100644 --- a/test/test_mesh.c +++ b/test/test_mesh.c @@ -1146,23 +1146,9 @@ static int CountCylinderGhosts( GhostScene scene, int peelPreset, bool mirror, i return ghostCount; } -// The fine and coarse strips of the T-junction share only every other vertex, so the edges along the -// junction have no neighbors. Boundary contacts there are blocked by the triangles across the junction. -static int MeshTJunctionSlide( void ) -{ - for ( int mirror = 0; mirror < 2; ++mirror ) - { - int runCount; - ENSURE( CountCylinderGhosts( e_ghostTJunction, 0, mirror == 1, &runCount ) == 0 ); - ENSURE( runCount >= 2 ); - } - - return 0; -} - // The PEEL cylinder slides along its axis on a polygon edge that runs along an internal edge of the // strip. Deep edge contacts on the far side fail the neighbor test and their face snap is empty, so -// they are kept, and the triangles under the cylinder block them. +// the triangles under the cylinder own the contact. static int MeshPeelCylinder( void ) { int runCount; @@ -1187,7 +1173,6 @@ int MeshTest( void ) RUN_SUBTEST( MeshSphereOnRidge ); RUN_SUBTEST( MeshCapsuleOverCrest ); RUN_SUBTEST( MeshApexContact ); - RUN_SUBTEST( MeshTJunctionSlide ); RUN_SUBTEST( MeshPeelCylinder ); return 0;