Build polish (#18)
Improve build process and documentation. Improved ray versus capsule accuracy. UI improvements
This commit is contained in:
parent
0c8ef46931
commit
69037feaba
19 changed files with 1719 additions and 158 deletions
|
|
@ -7,6 +7,7 @@
|
|||
#include "box3d/math_functions.h"
|
||||
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
static b3Capsule capsule = { { -1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f }, 1.0f };
|
||||
static b3Sphere sphere = { { 1.0f, 0.0f, 0.0f }, 1.0f };
|
||||
|
|
@ -272,6 +273,547 @@ static int PointInShapeTest( void )
|
|||
}
|
||||
#endif
|
||||
|
||||
// Shared assertions for a surface hit. The normal points outward toward the ray,
|
||||
// the point sits on the surface, and the point lies on the ray at the reported fraction.
|
||||
static int CheckCastHit( b3CastOutput out, b3Vec3 origin, b3Vec3 translation, b3Vec3 point, b3Vec3 normal,
|
||||
float fraction, float tol )
|
||||
{
|
||||
ENSURE( out.hit );
|
||||
ENSURE_SMALL( out.fraction - fraction, tol );
|
||||
ENSURE_SMALL( out.point.x - point.x, tol );
|
||||
ENSURE_SMALL( out.point.y - point.y, tol );
|
||||
ENSURE_SMALL( out.point.z - point.z, tol );
|
||||
ENSURE_SMALL( out.normal.x - normal.x, tol );
|
||||
ENSURE_SMALL( out.normal.y - normal.y, tol );
|
||||
ENSURE_SMALL( out.normal.z - normal.z, tol );
|
||||
|
||||
b3Vec3 onRay = b3MulAdd( origin, out.fraction, translation );
|
||||
ENSURE_SMALL( b3Distance( out.point, onRay ), tol );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The shared initial overlap convention: a ray starting inside a solid reports the origin
|
||||
// with zero fraction and no normal.
|
||||
static int CheckInitialOverlap( b3CastOutput out, b3Vec3 origin )
|
||||
{
|
||||
ENSURE( out.hit );
|
||||
ENSURE( out.fraction == 0.0f );
|
||||
ENSURE_SMALL( b3Distance( out.point, origin ), FLT_EPSILON );
|
||||
ENSURE( out.normal.x == 0.0f && out.normal.y == 0.0f && out.normal.z == 0.0f );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RayCastSphereHitTest( void )
|
||||
{
|
||||
b3Sphere s = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
|
||||
// Hit along each principal axis. Surface at distance 3 over a length 8 ray.
|
||||
{
|
||||
b3RayCastInput input = { { -4.0f, 0.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastSphere( &s, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -1.0f, 0.0f, 0.0f },
|
||||
( b3Vec3 ){ -1.0f, 0.0f, 0.0f }, 3.0f / 8.0f, 1e-5f ) )
|
||||
return 1;
|
||||
}
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 4.0f, 0.0f }, { 0.0f, -8.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastSphere( &s, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 0.0f, 1.0f, 0.0f },
|
||||
( b3Vec3 ){ 0.0f, 1.0f, 0.0f }, 3.0f / 8.0f, 1e-5f ) )
|
||||
return 1;
|
||||
}
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 0.0f, -4.0f }, { 0.0f, 0.0f, 8.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastSphere( &s, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 0.0f, 0.0f, -1.0f },
|
||||
( b3Vec3 ){ 0.0f, 0.0f, -1.0f }, 3.0f / 8.0f, 1e-5f ) )
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Offset center, hit partway along the ray.
|
||||
{
|
||||
b3Sphere s2 = { { 5.0f, 0.0f, 0.0f }, 2.0f };
|
||||
b3RayCastInput input = { { 0.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastSphere( &s2, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 3.0f, 0.0f, 0.0f },
|
||||
( b3Vec3 ){ -1.0f, 0.0f, 0.0f }, 0.3f, 1e-5f ) )
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Diagonal ray straight through the center.
|
||||
{
|
||||
float k = 0.70710678f;
|
||||
b3RayCastInput input = { { -3.0f, -3.0f, 0.0f }, { 6.0f, 6.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastSphere( &s, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -k, -k, 0.0f },
|
||||
( b3Vec3 ){ -k, -k, 0.0f }, 0.382149f, 1e-4f ) )
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RayCastSphereMissTest( void )
|
||||
{
|
||||
b3Sphere s = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
|
||||
// Pointing away.
|
||||
{
|
||||
b3RayCastInput input = { { -4.0f, 0.0f, 0.0f }, { -8.0f, 0.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastSphere( &s, &input ).hit == false );
|
||||
}
|
||||
// Passes wide of the sphere.
|
||||
{
|
||||
b3RayCastInput input = { { -4.0f, 3.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastSphere( &s, &input ).hit == false );
|
||||
}
|
||||
// Aimed at the sphere but the translation stops short.
|
||||
{
|
||||
b3RayCastInput input = { { -4.0f, 0.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 0.3f };
|
||||
ENSURE( b3RayCastSphere( &s, &input ).hit == false );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RayCastSphereClipTest( void )
|
||||
{
|
||||
b3Sphere s = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
|
||||
// The surface is reached at fraction 3/8. Straddle it with maxFraction.
|
||||
{
|
||||
b3RayCastInput input = { { -4.0f, 0.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 0.374f };
|
||||
ENSURE( b3RayCastSphere( &s, &input ).hit == false );
|
||||
}
|
||||
{
|
||||
b3RayCastInput input = { { -4.0f, 0.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 0.376f };
|
||||
b3CastOutput out = b3RayCastSphere( &s, &input );
|
||||
ENSURE( out.hit );
|
||||
ENSURE_SMALL( out.fraction - 3.0f / 8.0f, 1e-5f );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RayCastSphereInteriorTest( void )
|
||||
{
|
||||
b3Sphere s = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
|
||||
// Origin inside reports the origin with zero fraction.
|
||||
{
|
||||
b3RayCastInput input = { { 0.3f, 0.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastSphere( &s, &input );
|
||||
ENSURE( out.hit );
|
||||
ENSURE( out.fraction == 0.0f );
|
||||
ENSURE_SMALL( b3Distance( out.point, input.origin ), FLT_EPSILON );
|
||||
}
|
||||
// Zero length ray inside.
|
||||
{
|
||||
b3RayCastInput input = { { 0.5f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastSphere( &s, &input );
|
||||
ENSURE( out.hit );
|
||||
ENSURE_SMALL( b3Distance( out.point, input.origin ), FLT_EPSILON );
|
||||
}
|
||||
// Zero length ray outside.
|
||||
{
|
||||
b3RayCastInput input = { { 3.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastSphere( &s, &input ).hit == false );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RayCastSphereGrazeTest( void )
|
||||
{
|
||||
b3Sphere s = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
|
||||
// Just inside the radius grazes a hit, just outside misses.
|
||||
{
|
||||
b3RayCastInput input = { { -4.0f, 0.999f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastSphere( &s, &input ).hit );
|
||||
}
|
||||
{
|
||||
b3RayCastInput input = { { -4.0f, 1.001f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastSphere( &s, &input ).hit == false );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Capsule along x from -2 to 2, radius 1. Reused by the capsule ray cast subtests.
|
||||
static const b3Capsule rayCapsule = { { -2.0f, 0.0f, 0.0f }, { 2.0f, 0.0f, 0.0f }, 1.0f };
|
||||
|
||||
static int RayCastCapsuleSideTest( void )
|
||||
{
|
||||
// Perpendicular hit on the cylindrical side. Surface at distance 2 over a length 6 ray.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 3.0f, 0.0f }, { 0.0f, -6.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 0.0f, 1.0f, 0.0f },
|
||||
( b3Vec3 ){ 0.0f, 1.0f, 0.0f }, 1.0f / 3.0f, 1e-5f ) )
|
||||
return 1;
|
||||
}
|
||||
// Same from +z to exercise the other transverse direction.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 0.0f, 3.0f }, { 0.0f, 0.0f, -6.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 0.0f, 0.0f, 1.0f },
|
||||
( b3Vec3 ){ 0.0f, 0.0f, 1.0f }, 1.0f / 3.0f, 1e-5f ) )
|
||||
return 1;
|
||||
}
|
||||
// Side hit nearer the c1 end.
|
||||
{
|
||||
b3RayCastInput input = { { -1.0f, 3.0f, 0.0f }, { 0.0f, -6.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -1.0f, 1.0f, 0.0f },
|
||||
( b3Vec3 ){ 0.0f, 1.0f, 0.0f }, 1.0f / 3.0f, 1e-5f ) )
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RayCastCapsuleObliqueTest( void )
|
||||
{
|
||||
// Oblique ray in the z=0 plane. It crosses y=1 inside the cylinder span, so the
|
||||
// normal stays transverse. Exercises the non perpendicular ray/axis solve where
|
||||
// dot(axis, rayAxis) != 0.
|
||||
b3RayCastInput input = { { -3.0f, 3.0f, 0.0f }, { 4.0f, -4.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
return CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -1.0f, 1.0f, 0.0f },
|
||||
( b3Vec3 ){ 0.0f, 1.0f, 0.0f }, 0.5f, 1e-4f );
|
||||
}
|
||||
|
||||
static int RayCastCapsuleCapTest( void )
|
||||
{
|
||||
float k = 0.70710678f;
|
||||
|
||||
// Collinear ray hits the c2 hemisphere from beyond the end.
|
||||
{
|
||||
b3RayCastInput input = { { 5.0f, 0.0f, 0.0f }, { -8.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 3.0f, 0.0f, 0.0f },
|
||||
( b3Vec3 ){ 1.0f, 0.0f, 0.0f }, 1.0f / 4.0f, 1e-5f ) )
|
||||
return 1;
|
||||
}
|
||||
// Off-axis ray through the c2 cap center, approaching from outside the cylinder.
|
||||
{
|
||||
b3RayCastInput input = { { 4.0f, 2.0f, 0.0f }, { -4.0f, -4.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ 2.0f + k, k, 0.0f },
|
||||
( b3Vec3 ){ k, k, 0.0f }, 0.323223f, 1e-4f ) )
|
||||
return 1;
|
||||
}
|
||||
// Mirror through the c1 cap center.
|
||||
{
|
||||
b3RayCastInput input = { { -4.0f, 2.0f, 0.0f }, { 4.0f, -4.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
if ( CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -2.0f - k, k, 0.0f },
|
||||
( b3Vec3 ){ -k, k, 0.0f }, 0.323223f, 1e-4f ) )
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RayCastCapsuleMissTest( void )
|
||||
{
|
||||
// Pointing away.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 3.0f, 0.0f }, { 0.0f, 4.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastCapsule( &rayCapsule, &input ).hit == false );
|
||||
}
|
||||
// Crosses above the axis more than a radius away.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 4.0f, 2.0f }, { 0.0f, -8.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastCapsule( &rayCapsule, &input ).hit == false );
|
||||
}
|
||||
// Aimed at the side but the translation stops short.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 5.0f, 0.0f }, { 0.0f, -1.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastCapsule( &rayCapsule, &input ).hit == false );
|
||||
}
|
||||
// Parallel to the axis and outside the cylinder.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 3.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastCapsule( &rayCapsule, &input ).hit == false );
|
||||
}
|
||||
// Descends past the rounded c2 end, beyond cap reach.
|
||||
{
|
||||
b3RayCastInput input = { { 4.0f, 3.0f, 0.0f }, { 0.0f, -6.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastCapsule( &rayCapsule, &input ).hit == false );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RayCastCapsuleInteriorTest( void )
|
||||
{
|
||||
// Origin on the axis between the caps.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 0.0f, 0.0f }, { 0.0f, -5.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
ENSURE( out.hit );
|
||||
ENSURE( out.fraction == 0.0f );
|
||||
ENSURE_SMALL( b3Distance( out.point, input.origin ), FLT_EPSILON );
|
||||
}
|
||||
// Origin inside the c2 hemisphere, past the cylinder end.
|
||||
{
|
||||
b3RayCastInput input = { { 2.5f, 0.0f, 0.0f }, { 0.0f, 0.0f, 5.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
ENSURE( out.hit );
|
||||
ENSURE( out.fraction == 0.0f );
|
||||
ENSURE_SMALL( b3Distance( out.point, input.origin ), FLT_EPSILON );
|
||||
}
|
||||
// Zero length ray inside.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
ENSURE( out.hit );
|
||||
ENSURE_SMALL( b3Distance( out.point, input.origin ), FLT_EPSILON );
|
||||
}
|
||||
// Zero length ray outside.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 3.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastCapsule( &rayCapsule, &input ).hit == false );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RayCastCapsuleDegenerateTest( void )
|
||||
{
|
||||
// Coincident centers collapse to a sphere.
|
||||
b3Capsule c = { { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3RayCastInput input = { { -4.0f, 0.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &c, &input );
|
||||
return CheckCastHit( out, input.origin, input.translation, ( b3Vec3 ){ -1.0f, 0.0f, 0.0f },
|
||||
( b3Vec3 ){ -1.0f, 0.0f, 0.0f }, 3.0f / 8.0f, 1e-5f );
|
||||
}
|
||||
|
||||
static int RayCastCapsuleClipTest( void )
|
||||
{
|
||||
// The side hit occurs at fraction 1/3. Straddle it with maxFraction.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 3.0f, 0.0f }, { 0.0f, -6.0f, 0.0f }, 0.3f };
|
||||
ENSURE( b3RayCastCapsule( &rayCapsule, &input ).hit == false );
|
||||
}
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 3.0f, 0.0f }, { 0.0f, -6.0f, 0.0f }, 0.5f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
ENSURE( out.hit );
|
||||
ENSURE_SMALL( out.fraction - 1.0f / 3.0f, 1e-5f );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// A ray within a hair of the capsule axis must still hit when it slowly converges onto the
|
||||
// surface. The closest point solver is ill conditioned in this band, so this guards the near
|
||||
// parallel fallback that intersects the infinite cylinder directly.
|
||||
static int RayCastCapsuleParallelTest( void )
|
||||
{
|
||||
// Capsule along y. A long ray almost parallel to the axis drifts inward from just outside the
|
||||
// cylinder and dips through the far endcap. The naive solve loses this hit to a determinant of zero.
|
||||
b3Capsule axisY = { { 0.0f, 0.0f, 0.0f }, { 0.0f, 10.0f, 0.0f }, 1.0f };
|
||||
{
|
||||
b3RayCastInput input = { { 1.0001f, 100.0f, 0.0f }, { -0.001f, -200.0f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &axisY, &input );
|
||||
ENSURE( out.hit );
|
||||
|
||||
// The hit lands on the capsule surface and on the ray.
|
||||
b3Vec3 onSeg = b3PointToSegmentDistance( axisY.center1, axisY.center2, out.point );
|
||||
ENSURE_SMALL( b3Distance( out.point, onSeg ) - axisY.radius, 1e-3f );
|
||||
b3Vec3 onRay = b3MulAdd( input.origin, out.fraction, input.translation );
|
||||
ENSURE_SMALL( b3Distance( out.point, onRay ), 1e-3f );
|
||||
}
|
||||
|
||||
// Near parallel ray converging onto the x-axis capsule from far away.
|
||||
{
|
||||
b3RayCastInput input = { { -1000.0f, 1.0001f, 0.0f }, { 2000.0f, -0.001f, 0.0f }, 1.0f };
|
||||
b3CastOutput out = b3RayCastCapsule( &rayCapsule, &input );
|
||||
ENSURE( out.hit );
|
||||
b3Vec3 onSeg = b3PointToSegmentDistance( rayCapsule.center1, rayCapsule.center2, out.point );
|
||||
ENSURE_SMALL( b3Distance( out.point, onSeg ) - rayCapsule.radius, 1e-3f );
|
||||
}
|
||||
|
||||
// Exactly parallel and outside the cylinder still misses.
|
||||
{
|
||||
b3RayCastInput input = { { 0.0f, 3.0f, 0.0f }, { 8.0f, 0.0f, 0.0f }, 1.0f };
|
||||
ENSURE( b3RayCastCapsule( &rayCapsule, &input ).hit == false );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Zero length rays and initial overlap behave the same across the solid shapes. A moving ray
|
||||
// and a zero length ray that both start inside report the origin with zero fraction, and a zero
|
||||
// length ray that starts outside misses.
|
||||
static int RayCastOverlapConventionTest( void )
|
||||
{
|
||||
b3Vec3 zero = { 0.0f, 0.0f, 0.0f };
|
||||
b3Vec3 ray = { 8.0f, 0.0f, 0.0f };
|
||||
|
||||
// Sphere
|
||||
{
|
||||
b3Sphere s = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3Vec3 inside = { 0.2f, 0.0f, 0.0f };
|
||||
b3Vec3 outside = { 3.0f, 0.0f, 0.0f };
|
||||
b3RayCastInput moving = { inside, ray, 1.0f };
|
||||
b3RayCastInput pointInside = { inside, zero, 1.0f };
|
||||
b3RayCastInput pointOutside = { outside, zero, 1.0f };
|
||||
if ( CheckInitialOverlap( b3RayCastSphere( &s, &moving ), inside ) )
|
||||
return 1;
|
||||
if ( CheckInitialOverlap( b3RayCastSphere( &s, &pointInside ), inside ) )
|
||||
return 1;
|
||||
ENSURE( b3RayCastSphere( &s, &pointOutside ).hit == false );
|
||||
}
|
||||
|
||||
// Capsule
|
||||
{
|
||||
b3Vec3 inside = { 0.0f, 0.0f, 0.0f };
|
||||
b3Vec3 outside = { 0.0f, 3.0f, 0.0f };
|
||||
b3RayCastInput moving = { inside, ray, 1.0f };
|
||||
b3RayCastInput pointInside = { inside, zero, 1.0f };
|
||||
b3RayCastInput pointOutside = { outside, zero, 1.0f };
|
||||
if ( CheckInitialOverlap( b3RayCastCapsule( &rayCapsule, &moving ), inside ) )
|
||||
return 1;
|
||||
if ( CheckInitialOverlap( b3RayCastCapsule( &rayCapsule, &pointInside ), inside ) )
|
||||
return 1;
|
||||
ENSURE( b3RayCastCapsule( &rayCapsule, &pointOutside ).hit == false );
|
||||
}
|
||||
|
||||
// Hull
|
||||
{
|
||||
b3Vec3 inside = { 0.3f, 0.2f, 0.1f };
|
||||
b3Vec3 outside = { 3.0f, 0.0f, 0.0f };
|
||||
b3RayCastInput moving = { inside, ray, 1.0f };
|
||||
b3RayCastInput pointInside = { inside, zero, 1.0f };
|
||||
b3RayCastInput pointOutside = { outside, zero, 1.0f };
|
||||
if ( CheckInitialOverlap( b3RayCastHull( &box.base, &moving ), inside ) )
|
||||
return 1;
|
||||
if ( CheckInitialOverlap( b3RayCastHull( &box.base, &pointInside ), inside ) )
|
||||
return 1;
|
||||
ENSURE( b3RayCastHull( &box.base, &pointOutside ).hit == false );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Distance, in double precision, from a single precision hit point to the analytic first
|
||||
// ray/sphere intersection of the same float ray. Isolates the single precision method error:
|
||||
// the reference carries no float rounding, so what remains is purely what the method lost.
|
||||
static double SphereHitError( b3Sphere shape, b3RayCastInput input, b3Vec3 point )
|
||||
{
|
||||
double r = shape.radius;
|
||||
double sx = (double)input.origin.x - shape.center.x;
|
||||
double sy = (double)input.origin.y - shape.center.y;
|
||||
double sz = (double)input.origin.z - shape.center.z;
|
||||
double tx = input.translation.x, ty = input.translation.y, tz = input.translation.z;
|
||||
double len = sqrt( tx * tx + ty * ty + tz * tz );
|
||||
double dx = tx / len, dy = ty / len, dz = tz / len;
|
||||
double b = sx * dx + sy * dy + sz * dz;
|
||||
double c = sx * sx + sy * sy + sz * sz - r * r;
|
||||
double t = -b - sqrt( b * b - c );
|
||||
double ex = point.x - ( input.origin.x + t * dx );
|
||||
double ey = point.y - ( input.origin.y + t * dy );
|
||||
double ez = point.z - ( input.origin.z + t * dz );
|
||||
return sqrt( ex * ex + ey * ey + ez * ez );
|
||||
}
|
||||
|
||||
// Same idea for the ray/infinite-cylinder intersection, used where the hit lands on the side.
|
||||
static double CapsuleHitError( b3Capsule shape, b3RayCastInput input, b3Vec3 point )
|
||||
{
|
||||
double ax = (double)shape.center2.x - shape.center1.x;
|
||||
double ay = (double)shape.center2.y - shape.center1.y;
|
||||
double az = (double)shape.center2.z - shape.center1.z;
|
||||
double alen = sqrt( ax * ax + ay * ay + az * az );
|
||||
ax /= alen, ay /= alen, az /= alen;
|
||||
|
||||
double sx = (double)input.origin.x - shape.center1.x;
|
||||
double sy = (double)input.origin.y - shape.center1.y;
|
||||
double sz = (double)input.origin.z - shape.center1.z;
|
||||
double tx = input.translation.x, ty = input.translation.y, tz = input.translation.z;
|
||||
double tlen = sqrt( tx * tx + ty * ty + tz * tz );
|
||||
double dx = tx / tlen, dy = ty / tlen, dz = tz / tlen;
|
||||
|
||||
double sa = sx * ax + sy * ay + sz * az;
|
||||
double da = dx * ax + dy * ay + dz * az;
|
||||
double spx = sx - sa * ax, spy = sy - sa * ay, spz = sz - sa * az;
|
||||
double dpx = dx - da * ax, dpy = dy - da * ay, dpz = dz - da * az;
|
||||
double A = dpx * dpx + dpy * dpy + dpz * dpz;
|
||||
double B = 2.0 * ( spx * dpx + spy * dpy + spz * dpz );
|
||||
double r = shape.radius;
|
||||
double C = spx * spx + spy * spy + spz * spz - r * r;
|
||||
double tau = ( -B - sqrt( B * B - 4.0 * A * C ) ) / ( 2.0 * A );
|
||||
double ex = point.x - ( input.origin.x + tau * dx );
|
||||
double ey = point.y - ( input.origin.y + tau * dy );
|
||||
double ez = point.z - ( input.origin.z + tau * dz );
|
||||
return sqrt( ex * ex + ey * ey + ez * ez );
|
||||
}
|
||||
|
||||
// A miss is the worst possible outcome, so fold it into the error as a large sentinel.
|
||||
#define RAY_MISS 1.0e30
|
||||
|
||||
static int RayCastFarOriginTest( void )
|
||||
{
|
||||
b3Sphere s = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3Capsule c = { { -2.0f, 0.0f, 0.0f }, { 2.0f, 0.0f, 0.0f }, 1.0f };
|
||||
|
||||
// (0,0,1) lies on the unit sphere and on the capsule side. The ray dives in from a far origin
|
||||
// along H + D*u for a fan of directions u skewed off the surface normal, so the capsule solve
|
||||
// sees a real perpendicular gap rather than a free exact cancellation.
|
||||
b3Vec3 h = { 0.0f, 0.0f, 1.0f };
|
||||
float offsets[] = { -0.7f, 0.0f, 0.7f };
|
||||
float distances[] = { 1e1f, 1e2f, 1e3f, 1e4f, 1e5f, 1e6f, 1e7f };
|
||||
|
||||
double worstSphere[ARRAY_COUNT( distances )];
|
||||
double worstCapsule[ARRAY_COUNT( distances )];
|
||||
|
||||
printf( " worst hit point error over a fan of skew rays, by origin distance:\n" );
|
||||
printf( " %-9s %-13s %-13s\n", "distance", "sphere", "capsule" );
|
||||
for ( int i = 0; i < ARRAY_COUNT( distances ); ++i )
|
||||
{
|
||||
float d = distances[i];
|
||||
double maxS = 0.0, maxC = 0.0;
|
||||
|
||||
for ( int ia = 0; ia < ARRAY_COUNT( offsets ); ++ia )
|
||||
{
|
||||
for ( int ib = 0; ib < ARRAY_COUNT( offsets ); ++ib )
|
||||
{
|
||||
b3Vec3 u = b3Normalize( ( b3Vec3 ){ offsets[ia], offsets[ib], 1.0f } );
|
||||
b3Vec3 origin = b3MulAdd( h, d, u );
|
||||
b3Vec3 translation = b3MulSV( -2.0f * d, u );
|
||||
b3RayCastInput input = { origin, translation, 1.0f };
|
||||
|
||||
b3CastOutput os = b3RayCastSphere( &s, &input );
|
||||
b3CastOutput oc = b3RayCastCapsule( &c, &input );
|
||||
|
||||
double errS = os.hit ? SphereHitError( s, input, os.point ) : RAY_MISS;
|
||||
double errC = oc.hit ? CapsuleHitError( c, input, oc.point ) : RAY_MISS;
|
||||
|
||||
maxS = errS > maxS ? errS : maxS;
|
||||
maxC = errC > maxC ? errC : maxC;
|
||||
}
|
||||
}
|
||||
|
||||
worstSphere[i] = maxS;
|
||||
worstCapsule[i] = maxC;
|
||||
printf( " %-9.0e %-13.3e %-13.3e\n", d, maxS, maxC );
|
||||
}
|
||||
|
||||
// The closest point formulation keeps the error at the single precision floor: it grows only
|
||||
// linearly with origin distance, error ~ distance * FLT_EPSILON, with no catastrophic loss. This
|
||||
// holds out to a million units. At ten million the origin coordinates carry a meter sized ULP,
|
||||
// larger than the unit radius, so the ray genuinely drops the hit. That row is printed for insight
|
||||
// but left unasserted rather than baking in the breakdown.
|
||||
for ( int i = 0; i < ARRAY_COUNT( distances ); ++i )
|
||||
{
|
||||
if ( distances[i] < 1.0e7f )
|
||||
{
|
||||
double floor = 16.0 * distances[i] * FLT_EPSILON + 2.0e-6;
|
||||
ENSURE( worstSphere[i] < floor );
|
||||
ENSURE( worstCapsule[i] < floor );
|
||||
}
|
||||
}
|
||||
|
||||
// Still a clean sub-meter hit at a million units out.
|
||||
ENSURE( worstSphere[5] < 0.5 && worstCapsule[5] < 0.5 );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int RayCastShapeTest( void )
|
||||
{
|
||||
b3RayCastInput input = {
|
||||
|
|
@ -319,5 +861,23 @@ int ShapeTest( void )
|
|||
// RUN_SUBTEST( PointInShapeTest );
|
||||
RUN_SUBTEST( RayCastShapeTest );
|
||||
|
||||
RUN_SUBTEST( RayCastSphereHitTest );
|
||||
RUN_SUBTEST( RayCastSphereMissTest );
|
||||
RUN_SUBTEST( RayCastSphereClipTest );
|
||||
RUN_SUBTEST( RayCastSphereInteriorTest );
|
||||
RUN_SUBTEST( RayCastSphereGrazeTest );
|
||||
|
||||
RUN_SUBTEST( RayCastCapsuleSideTest );
|
||||
RUN_SUBTEST( RayCastCapsuleObliqueTest );
|
||||
RUN_SUBTEST( RayCastCapsuleCapTest );
|
||||
RUN_SUBTEST( RayCastCapsuleMissTest );
|
||||
RUN_SUBTEST( RayCastCapsuleInteriorTest );
|
||||
RUN_SUBTEST( RayCastCapsuleDegenerateTest );
|
||||
RUN_SUBTEST( RayCastCapsuleClipTest );
|
||||
RUN_SUBTEST( RayCastCapsuleParallelTest );
|
||||
|
||||
RUN_SUBTEST( RayCastOverlapConventionTest );
|
||||
RUN_SUBTEST( RayCastFarOriginTest );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue