Height field fixes (#6)
Fix for height field flat edges Fix for height field shape cast Made B3_REST_OFFSET and official constant
This commit is contained in:
parent
aedb0e34aa
commit
758dc665dd
8 changed files with 471 additions and 341 deletions
|
|
@ -3,8 +3,9 @@
|
|||
|
||||
#include "test_macros.h"
|
||||
|
||||
// b3GetHeightFieldTriangle is internal
|
||||
// b3GetHeightFieldTriangle and b3IntersectRayTriangle are internal
|
||||
#include "shape.h"
|
||||
#include "simd.h"
|
||||
|
||||
#include "box3d/collision.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
|
@ -233,6 +234,299 @@ static int FileRoundtrip( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int ShapeCastVerticalStraddle( void )
|
||||
{
|
||||
// Regression: a vertical shape cast whose swept volume straddles a cell
|
||||
// boundary must test every cell it overlaps. The field is flat at y = 0 with
|
||||
// only cell (0,0) solid; the surrounding cells are holes. Each sphere is
|
||||
// dropped straight down with its center nudged just past a boundary of the
|
||||
// solid cell, so that cell sits on the trailing (-x / -z) side of the sweep.
|
||||
// A cull AABB pinned to the leading box corner skips the solid cell entirely
|
||||
// and reports a miss.
|
||||
float heights[9] = { 0 };
|
||||
uint8_t materials[4] = { 0, B3_HEIGHT_FIELD_HOLE, B3_HEIGHT_FIELD_HOLE, B3_HEIGHT_FIELD_HOLE };
|
||||
|
||||
b3HeightFieldDef def = { 0 };
|
||||
def.heights = heights;
|
||||
def.materialIndices = materials;
|
||||
def.scale = (b3Vec3){ 1.0f, 1.0f, 1.0f };
|
||||
def.countX = 3;
|
||||
def.countZ = 3;
|
||||
def.globalMinimumHeight = -1.0f;
|
||||
def.globalMaximumHeight = 1.0f;
|
||||
def.clockwiseWinding = false;
|
||||
|
||||
b3HeightField* hf = b3CreateHeightField( &def );
|
||||
|
||||
// Solid cell (0,0) spans x,z in [0,1]. Radius 0.3 with the center 0.05 past a
|
||||
// boundary still reaches back into the solid cell.
|
||||
const float radius = 0.3f;
|
||||
|
||||
// Straddle the x = 1 edge: solid cell is on the -x side. Contact lands on the
|
||||
// cell edge, sqrt(0.05^2 + cy^2) = radius -> cy = 0.2958040,
|
||||
// fraction = (10 - cy) / 20 = 0.4852098.
|
||||
{
|
||||
b3Vec3 center = { 1.05f, 10.0f, 0.5f };
|
||||
b3ShapeCastInput input = { 0 };
|
||||
input.proxy = (b3ShapeProxy){ ¢er, 1, radius };
|
||||
input.translation = (b3Vec3){ 0.0f, -20.0f, 0.0f };
|
||||
input.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput out = b3ShapeCastHeightField( hf, &input );
|
||||
ENSURE( out.hit == true );
|
||||
ENSURE_SMALL( out.fraction - 0.4852098f, 2e-3f );
|
||||
}
|
||||
|
||||
// Straddle the z = 1 edge: solid cell is on the -z side (same geometry).
|
||||
{
|
||||
b3Vec3 center = { 0.5f, 10.0f, 1.05f };
|
||||
b3ShapeCastInput input = { 0 };
|
||||
input.proxy = (b3ShapeProxy){ ¢er, 1, radius };
|
||||
input.translation = (b3Vec3){ 0.0f, -20.0f, 0.0f };
|
||||
input.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput out = b3ShapeCastHeightField( hf, &input );
|
||||
ENSURE( out.hit == true );
|
||||
ENSURE_SMALL( out.fraction - 0.4852098f, 2e-3f );
|
||||
}
|
||||
|
||||
// Straddle the (1,1) corner: solid cell is diagonally trailing. Contact lands
|
||||
// on the corner vertex, sqrt(2*0.05^2 + cy^2) = radius -> cy = 0.2915476,
|
||||
// fraction = (10 - cy) / 20 = 0.4854226.
|
||||
{
|
||||
b3Vec3 center = { 1.05f, 10.0f, 1.05f };
|
||||
b3ShapeCastInput input = { 0 };
|
||||
input.proxy = (b3ShapeProxy){ ¢er, 1, radius };
|
||||
input.translation = (b3Vec3){ 0.0f, -20.0f, 0.0f };
|
||||
input.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput out = b3ShapeCastHeightField( hf, &input );
|
||||
ENSURE( out.hit == true );
|
||||
ENSURE_SMALL( out.fraction - 0.4854226f, 2e-3f );
|
||||
}
|
||||
|
||||
b3DestroyHeightField( hf );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Brute-force shape cast: cast the proxy against every (non-hole) triangle and
|
||||
// keep the closest hit. This is the ground truth for b3ShapeCastHeightField.
|
||||
static b3CastOutput BruteForceShapeCast( const b3HeightField* hf, const b3ShapeCastInput* input )
|
||||
{
|
||||
b3CastOutput best = { 0 };
|
||||
float bestFraction = input->maxFraction;
|
||||
|
||||
int triangleCount = b3GetHeightFieldTriangleCount( hf );
|
||||
for ( int t = 0; t < triangleCount; ++t )
|
||||
{
|
||||
int cellIndex = t >> 1;
|
||||
if ( hf->materialIndices[cellIndex] == B3_HEIGHT_FIELD_HOLE )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
b3Triangle tri = b3GetHeightFieldTriangle( hf, t );
|
||||
|
||||
b3ShapeCastPairInput pair = { 0 };
|
||||
pair.proxyA = (b3ShapeProxy){ tri.vertices, 3, 0.0f };
|
||||
pair.proxyB = input->proxy;
|
||||
pair.transformA = b3Transform_identity;
|
||||
pair.transformB = b3Transform_identity;
|
||||
pair.translationB = input->translation;
|
||||
pair.maxFraction = bestFraction;
|
||||
pair.canEncroach = input->canEncroach;
|
||||
|
||||
b3CastOutput out = b3ShapeCast( &pair );
|
||||
if ( out.hit && out.fraction < bestFraction )
|
||||
{
|
||||
bestFraction = out.fraction;
|
||||
best = out;
|
||||
best.triangleIndex = t;
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
static int ShapeCastBruteForce( void )
|
||||
{
|
||||
// b3ShapeCastHeightField walks the grid and culls cells; the brute-force cast
|
||||
// against every triangle is the ground truth. The grid walk must never miss a
|
||||
// closer hit, regardless of cast direction, origin or radius.
|
||||
b3Vec3 scale = { 2.0f, 1.5f, 2.0f };
|
||||
b3HeightField* hf = b3CreateWave( 10, 10, scale, 0.1f, 0.03333f, false );
|
||||
|
||||
// Documented repro from sample/sample_mesh.cpp "Height Field": a sphere cast
|
||||
// that moves only in z (and y). Body at (-9,0,-9), world origin (5.5,4,2.913)
|
||||
// -> local (14.5,4,11.913). The grid walk used to terminate one row early
|
||||
// because it compared a clamped-sweep fraction against an input-space one.
|
||||
{
|
||||
b3Vec3 origin = { 14.5f, 4.0f, 11.913f };
|
||||
b3ShapeCastInput input = { 0 };
|
||||
input.proxy = (b3ShapeProxy){ &origin, 1, 0.2f };
|
||||
input.translation = (b3Vec3){ 0.0f, -8.0f, 6.397f };
|
||||
input.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput grid = b3ShapeCastHeightField( hf, &input );
|
||||
b3CastOutput brute = BruteForceShapeCast( hf, &input );
|
||||
ENSURE( brute.hit == true );
|
||||
ENSURE( grid.hit == brute.hit );
|
||||
ENSURE_SMALL( grid.fraction - brute.fraction, 2e-3f );
|
||||
}
|
||||
|
||||
// Sweep origins across the field with assorted directions and radii.
|
||||
const float radii[] = { 0.15f, 0.4f, 0.9f };
|
||||
const b3Vec3 deltas[] = {
|
||||
{ 0.0f, -8.0f, 0.0f }, // vertical
|
||||
{ 0.0f, -8.0f, 6.4f }, // z only (+ y)
|
||||
{ 5.1f, -8.0f, 0.0f }, // x only (+ y)
|
||||
{ 0.0f, -8.0f, -6.4f }, // -z
|
||||
{ -5.1f, -8.0f, 0.0f }, // -x
|
||||
{ 6.0f, -8.0f, 5.0f }, // diagonal
|
||||
{ -7.0f, -8.0f, 4.0f }, // diagonal, mixed sign
|
||||
{ 9.0f, -3.0f, -9.0f }, // shallow diagonal
|
||||
};
|
||||
|
||||
int failures = 0;
|
||||
for ( int xi = 0; xi < 5; ++xi )
|
||||
{
|
||||
for ( int zi = 0; zi < 5; ++zi )
|
||||
{
|
||||
// 0.05 nudge keeps the swept box straddling cell boundaries.
|
||||
b3Vec3 origin = { 1.0f + 4.0f * xi + 0.05f, 4.0f, 1.0f + 4.0f * zi + 0.05f };
|
||||
|
||||
for ( int di = 0; di < ARRAY_COUNT( deltas ); ++di )
|
||||
{
|
||||
for ( int ri = 0; ri < ARRAY_COUNT( radii ); ++ri )
|
||||
{
|
||||
b3ShapeCastInput input = { 0 };
|
||||
input.proxy = (b3ShapeProxy){ &origin, 1, radii[ri] };
|
||||
input.translation = deltas[di];
|
||||
input.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput grid = b3ShapeCastHeightField( hf, &input );
|
||||
b3CastOutput brute = BruteForceShapeCast( hf, &input );
|
||||
|
||||
float diff = grid.fraction - brute.fraction;
|
||||
diff = diff < 0.0f ? -diff : diff;
|
||||
|
||||
if ( grid.hit != brute.hit || ( brute.hit && diff > 2e-3f ) )
|
||||
{
|
||||
printf( " mismatch: origin=(%.2f,%.2f,%.2f) delta=(%.2f,%.2f,%.2f) r=%.2f"
|
||||
" grid(hit=%d,f=%.5f) brute(hit=%d,f=%.5f tri=%d)\n",
|
||||
origin.x, origin.y, origin.z, deltas[di].x, deltas[di].y, deltas[di].z, radii[ri],
|
||||
grid.hit, grid.fraction, brute.hit, brute.fraction, brute.triangleIndex );
|
||||
failures += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ENSURE( failures == 0 );
|
||||
|
||||
b3DestroyHeightField( hf );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Brute-force ray cast: cast the ray against every (non-hole) triangle and keep
|
||||
// the closest hit. b3GetHeightFieldTriangle returns vertices in the same winding
|
||||
// order that b3ShapeCastHeightField feeds to b3IntersectRayTriangle, so this is a
|
||||
// pure traversal/culling check — the per-triangle math is identical.
|
||||
static b3CastOutput BruteForceRayCast( const b3HeightField* hf, const b3RayCastInput* input )
|
||||
{
|
||||
b3CastOutput best = { 0 };
|
||||
float bestFraction = input->maxFraction;
|
||||
|
||||
b3V32 rayStart = b3LoadV( &input->origin.x );
|
||||
b3V32 rayDelta = b3LoadV( &input->translation.x );
|
||||
|
||||
int triangleCount = b3GetHeightFieldTriangleCount( hf );
|
||||
for ( int t = 0; t < triangleCount; ++t )
|
||||
{
|
||||
int cellIndex = t >> 1;
|
||||
if ( hf->materialIndices[cellIndex] == B3_HEIGHT_FIELD_HOLE )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
b3Triangle tri = b3GetHeightFieldTriangle( hf, t );
|
||||
b3V32 v1 = b3LoadV( &tri.vertices[0].x );
|
||||
b3V32 v2 = b3LoadV( &tri.vertices[1].x );
|
||||
b3V32 v3 = b3LoadV( &tri.vertices[2].x );
|
||||
|
||||
// b3IntersectRayTriangle returns 1.0 on a miss.
|
||||
float alpha = b3IntersectRayTriangle( rayStart, rayDelta, v1, v2, v3 );
|
||||
if ( alpha < bestFraction )
|
||||
{
|
||||
bestFraction = alpha;
|
||||
best.hit = true;
|
||||
best.fraction = alpha;
|
||||
best.triangleIndex = t;
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
static int RayCastBruteForce( void )
|
||||
{
|
||||
// b3RayCastHeightField routes through the same grid walk as the shape cast
|
||||
// (radius-zero point proxy), so it is subject to the same early-termination
|
||||
// bug. The brute-force cast against every triangle is the ground truth.
|
||||
b3Vec3 scale = { 2.0f, 1.5f, 2.0f };
|
||||
b3HeightField* hf = b3CreateWave( 10, 10, scale, 0.1f, 0.03333f, false );
|
||||
|
||||
const b3Vec3 deltas[] = {
|
||||
{ 0.0f, -8.0f, 0.0f }, // straight down
|
||||
{ 0.0f, -8.0f, 12.0f }, // down + z
|
||||
{ 12.0f, -8.0f, 0.0f }, // down + x
|
||||
{ 0.0f, -8.0f, -12.0f }, // down - z
|
||||
{ -12.0f, -8.0f, 0.0f }, // down - x
|
||||
{ 14.0f, -8.0f, 11.0f }, // diagonal
|
||||
{ -13.0f, -8.0f, 9.0f }, // diagonal, mixed sign
|
||||
{ 16.0f, -4.0f, -15.0f }, // shallow diagonal
|
||||
};
|
||||
|
||||
int failures = 0;
|
||||
for ( int xi = 0; xi < 5; ++xi )
|
||||
{
|
||||
for ( int zi = 0; zi < 5; ++zi )
|
||||
{
|
||||
// 0.05 nudge keeps the ray off cell boundaries.
|
||||
b3Vec3 origin = { 1.0f + 4.0f * xi + 0.05f, 4.0f, 1.0f + 4.0f * zi + 0.05f };
|
||||
|
||||
for ( int di = 0; di < ARRAY_COUNT( deltas ); ++di )
|
||||
{
|
||||
b3RayCastInput input = { 0 };
|
||||
input.origin = origin;
|
||||
input.translation = deltas[di];
|
||||
input.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput grid = b3RayCastHeightField( hf, &input );
|
||||
b3CastOutput brute = BruteForceRayCast( hf, &input );
|
||||
|
||||
float diff = grid.fraction - brute.fraction;
|
||||
diff = diff < 0.0f ? -diff : diff;
|
||||
|
||||
if ( grid.hit != brute.hit || ( brute.hit && diff > 1e-4f ) )
|
||||
{
|
||||
printf( " mismatch: origin=(%.2f,%.2f,%.2f) delta=(%.2f,%.2f,%.2f)"
|
||||
" grid(hit=%d,f=%.6f tri=%d) brute(hit=%d,f=%.6f tri=%d)\n",
|
||||
origin.x, origin.y, origin.z, deltas[di].x, deltas[di].y, deltas[di].z, grid.hit,
|
||||
grid.fraction, grid.triangleIndex, brute.hit, brute.fraction, brute.triangleIndex );
|
||||
failures += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ENSURE( failures == 0 );
|
||||
|
||||
b3DestroyHeightField( hf );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int HeightFieldTest( void )
|
||||
{
|
||||
RUN_SUBTEST( HeightFieldCreate );
|
||||
|
|
@ -241,6 +535,9 @@ int HeightFieldTest( void )
|
|||
RUN_SUBTEST( RayCastFlatField );
|
||||
RUN_SUBTEST( OverlapAtSurface );
|
||||
RUN_SUBTEST( FileRoundtrip );
|
||||
RUN_SUBTEST( ShapeCastVerticalStraddle );
|
||||
RUN_SUBTEST( ShapeCastBruteForce );
|
||||
RUN_SUBTEST( RayCastBruteForce );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue