clockwise option, joint is awake (#130)
- b3MeshDef gets stride and clockwise option - Added b3Joint_IsAwake - Added b3Body_TimeOfImpactMover - #116 - Smoother mover collision: #141 - Shape casts now ignore backside collision on mesh and height-field triangles, same as ray casts.
This commit is contained in:
parent
30c67b5e6d
commit
47d7f7cc7e
35 changed files with 2466 additions and 303 deletions
|
|
@ -20,6 +20,7 @@ set(BOX3D_TEST_FILES
|
|||
test_macros.h
|
||||
test_manifold.c
|
||||
test_math.c
|
||||
test_mesh.c
|
||||
test_mover.c
|
||||
test_name_cache.c
|
||||
test_recording.c
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ extern int JointTest( void );
|
|||
extern int LargeWorldTest( void );
|
||||
extern int ManifoldTest( void );
|
||||
extern int MathTest( void );
|
||||
extern int MeshTest( void );
|
||||
extern int MoverTest( void );
|
||||
extern int NameCacheTest( void );
|
||||
extern int RecordingTest( void );
|
||||
|
|
@ -116,6 +117,7 @@ int main( int argc, char** argv )
|
|||
MAYBE_RUN_TEST( LargeWorldTest );
|
||||
MAYBE_RUN_TEST( ManifoldTest );
|
||||
MAYBE_RUN_TEST( MathTest );
|
||||
MAYBE_RUN_TEST( MeshTest );
|
||||
MAYBE_RUN_TEST( MoverTest );
|
||||
MAYBE_RUN_TEST( NameCacheTest );
|
||||
MAYBE_RUN_TEST( RecordingTest );
|
||||
|
|
|
|||
|
|
@ -385,6 +385,96 @@ static int SetMassDataConsistentVelocity( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Extents bound the shapes about the center of mass, per axis. An offset shape must count its
|
||||
// offset, not just its own size.
|
||||
static int ShapeExtents( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.density = 1.0f;
|
||||
|
||||
// Kinematic bodies measure from the body origin
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_kinematicBody;
|
||||
|
||||
b3BodyId capsuleId = b3CreateBody( worldId, &bodyDef );
|
||||
b3Capsule capsule = { { -2.0f, 0.0f, 0.0f }, { -1.0f, 0.0f, 0.0f }, 0.2f };
|
||||
b3CreateCapsuleShape( capsuleId, &shapeDef, &capsule );
|
||||
|
||||
b3Vec3 maxExtent = b3Body_GetMaxExtent( capsuleId );
|
||||
ENSURE_SMALL( maxExtent.x - 2.2f, 1e-5f );
|
||||
ENSURE_SMALL( maxExtent.y - 0.2f, 1e-5f );
|
||||
ENSURE_SMALL( maxExtent.z - 0.2f, 1e-5f );
|
||||
ENSURE_SMALL( b3Body_GetMinExtent( capsuleId ) - 0.2f, 1e-5f );
|
||||
|
||||
b3BodyId sphereId = b3CreateBody( worldId, &bodyDef );
|
||||
b3Sphere sphere = { { 1.0f, 2.0f, 3.0f }, 0.5f };
|
||||
b3CreateSphereShape( sphereId, &shapeDef, &sphere );
|
||||
|
||||
maxExtent = b3Body_GetMaxExtent( sphereId );
|
||||
ENSURE_SMALL( maxExtent.x - 1.5f, 1e-5f );
|
||||
ENSURE_SMALL( maxExtent.y - 2.5f, 1e-5f );
|
||||
ENSURE_SMALL( maxExtent.z - 3.5f, 1e-5f );
|
||||
ENSURE_SMALL( b3Body_GetMinExtent( sphereId ) - 0.5f, 1e-5f );
|
||||
|
||||
// Dynamic bodies measure from the center of mass. A light sphere hung off a cube pulls the
|
||||
// center toward it, so the far side of the sphere is the widest point.
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
b3BodyId cubeId = b3CreateBody( worldId, &bodyDef );
|
||||
b3BoxHull cube = b3MakeCubeHull( 0.5f );
|
||||
b3CreateHullShape( cubeId, &shapeDef, &cube.base );
|
||||
b3Sphere offsetSphere = { { 1.0f, 0.0f, 0.0f }, 0.2f };
|
||||
b3CreateSphereShape( cubeId, &shapeDef, &offsetSphere );
|
||||
|
||||
b3Vec3 localCenter = b3Body_GetLocalCenter( cubeId );
|
||||
ENSURE( 0.0f < localCenter.x && localCenter.x < 0.5f );
|
||||
|
||||
maxExtent = b3Body_GetMaxExtent( cubeId );
|
||||
ENSURE_SMALL( maxExtent.x - ( 1.2f - localCenter.x ), 1e-5f );
|
||||
ENSURE_SMALL( maxExtent.y - 0.5f, 1e-5f );
|
||||
ENSURE_SMALL( maxExtent.z - 0.5f, 1e-5f );
|
||||
ENSURE_SMALL( b3Body_GetMinExtent( cubeId ) - 0.2f, 1e-5f );
|
||||
|
||||
b3Vec3 originExtent = b3Body_GetMaxExtentOrigin( cubeId );
|
||||
ENSURE_SMALL( originExtent.x - 1.2f, 1e-5f );
|
||||
ENSURE_SMALL( originExtent.y - 0.5f, 1e-5f );
|
||||
ENSURE_SMALL( originExtent.z - 0.5f, 1e-5f );
|
||||
|
||||
// A mesh has no mass, so the sphere alone places the center at x = 1 and the far edge of the
|
||||
// mesh at x = -3 is four units out.
|
||||
b3Vec3 vertices[6] = {
|
||||
{ -3.0f, 0.0f, -1.0f }, { -2.0f, 0.0f, 1.0f }, { -1.0f, 0.0f, -1.0f },
|
||||
{ 1.0f, 0.0f, -1.0f }, { 2.0f, 0.0f, 1.0f }, { 3.0f, 0.0f, -1.0f },
|
||||
};
|
||||
int32_t indices[6] = { 0, 1, 2, 3, 4, 5 };
|
||||
b3MeshDef meshDef = { 0 };
|
||||
meshDef.vertices = vertices;
|
||||
meshDef.stride = sizeof( b3Vec3 );
|
||||
meshDef.indices = indices;
|
||||
meshDef.vertexCount = 6;
|
||||
meshDef.triangleCount = 2;
|
||||
b3MeshData* mesh = b3CreateMesh( &meshDef, NULL, 0 );
|
||||
|
||||
b3BodyId meshId = b3CreateBody( worldId, &bodyDef );
|
||||
b3CreateSphereShape( meshId, &shapeDef, &offsetSphere );
|
||||
b3CreateMeshShape( meshId, &shapeDef, mesh, (b3Vec3){ 1.0f, 1.0f, 1.0f } );
|
||||
|
||||
localCenter = b3Body_GetLocalCenter( meshId );
|
||||
ENSURE_SMALL( localCenter.x - 1.0f, 1e-5f );
|
||||
|
||||
maxExtent = b3Body_GetMaxExtent( meshId );
|
||||
ENSURE_SMALL( maxExtent.x - 4.0f, 1e-4f );
|
||||
ENSURE_SMALL( maxExtent.y - 0.2f, 1e-4f );
|
||||
ENSURE_SMALL( maxExtent.z - 1.0f, 1e-4f );
|
||||
ENSURE_SMALL( b3Body_GetMinExtent( meshId ) - 0.2f, 1e-5f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
b3DestroyMesh( mesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BodyTest( void )
|
||||
{
|
||||
RUN_SUBTEST( FarSingleSphereMass );
|
||||
|
|
@ -395,5 +485,6 @@ int BodyTest( void )
|
|||
RUN_SUBTEST( SetMassDataFixedRotation );
|
||||
RUN_SUBTEST( SetMassDataZeroMass );
|
||||
RUN_SUBTEST( SetMassDataConsistentVelocity );
|
||||
RUN_SUBTEST( ShapeExtents );
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -565,6 +565,412 @@ static int MoverCapacity( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
// TimeOfImpactMover ------------------------------------------------------------------------
|
||||
|
||||
// The mover capsule is expressed in the query frame, so a core segment starting at the origin
|
||||
// stands the character on the query point. Targets sit on the sweep line at y = 0.
|
||||
static b3Capsule MakeStandingMover( void )
|
||||
{
|
||||
return (b3Capsule){ { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 0.25f };
|
||||
}
|
||||
|
||||
static int MoverTOIHitsBox( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3ShapeId shapeId = b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
// Face at x = 4.5, mover radius 0.25, so 4.25 of the 10 unit sweep is free.
|
||||
b3Capsule mover = MakeStandingMover();
|
||||
b3WorldTransform bodyTransform = IdentityAt( 5.0f, 0.0f, 0.0f );
|
||||
b3BodyTOIResult result = b3Body_TimeOfImpactMover( bodyId, b3Pos_zero, &mover, (b3Vec3){ 10.0f, 0.0f, 0.0f },
|
||||
b3DefaultQueryFilter(), bodyTransform, bodyTransform );
|
||||
|
||||
ENSURE_SMALL( result.fraction - 0.425f, 1e-2f );
|
||||
ENSURE( b3IsNormalized( result.normal ) );
|
||||
ENSURE_SMALL( result.normal.x + 1.0f, 1e-3f );
|
||||
|
||||
// The result carries a shape id, so the hit shape must come back identified.
|
||||
ENSURE( b3Shape_IsValid( result.shapeId ) );
|
||||
ENSURE( result.shapeId.index1 == shapeId.index1 );
|
||||
ENSURE( result.shapeId.generation == shapeId.generation );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverTOISeparated( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
// Sweeping along +Y holds the X gap at 4.25 for the whole interval.
|
||||
b3Capsule mover = MakeStandingMover();
|
||||
b3WorldTransform bodyTransform = IdentityAt( 5.0f, 0.0f, 0.0f );
|
||||
b3BodyTOIResult result = b3Body_TimeOfImpactMover( bodyId, b3Pos_zero, &mover, (b3Vec3){ 0.0f, 10.0f, 0.0f },
|
||||
b3DefaultQueryFilter(), bodyTransform, bodyTransform );
|
||||
|
||||
ENSURE_SMALL( result.fraction - 1.0f, 1e-6f );
|
||||
ENSURE( b3Shape_IsValid( result.shapeId ) == false );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverTOIOverlapped( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
// Mover starts buried in the box, so there is no free interval to search.
|
||||
b3Capsule mover = MakeStandingMover();
|
||||
b3WorldTransform bodyTransform = IdentityAt( 0.0f, 0.0f, 0.0f );
|
||||
b3BodyTOIResult result = b3Body_TimeOfImpactMover( bodyId, b3Pos_zero, &mover, (b3Vec3){ 10.0f, 0.0f, 0.0f },
|
||||
b3DefaultQueryFilter(), bodyTransform, bodyTransform );
|
||||
|
||||
// Overlap should be ignored.
|
||||
ENSURE_SMALL( result.fraction - 1.0f, 1e-6f );
|
||||
ENSURE( B3_IS_NULL(result.shapeId) );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Two shapes on the sweep line must resolve to the nearer one whatever order the shape list
|
||||
// hands them to the loop. Shapes are pushed on the head of the list, so the two bodies below
|
||||
// walk their shapes in opposite orders.
|
||||
static int MoverTOIClosestShape( void )
|
||||
{
|
||||
b3BodyId nearFirstId;
|
||||
b3WorldId worldId = CreateQueryWorld( &nearFirstId );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
b3BodyId nearLastId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3Sphere nearSphere = { { 5.0f, 0.0f, 0.0f }, 0.5f };
|
||||
b3Sphere farSphere = { { 9.0f, 0.0f, 0.0f }, 0.5f };
|
||||
|
||||
b3ShapeId nearFirstHit = b3CreateSphereShape( nearFirstId, &shapeDef, &nearSphere );
|
||||
b3CreateSphereShape( nearFirstId, &shapeDef, &farSphere );
|
||||
|
||||
b3CreateSphereShape( nearLastId, &shapeDef, &farSphere );
|
||||
b3ShapeId nearLastHit = b3CreateSphereShape( nearLastId, &shapeDef, &nearSphere );
|
||||
|
||||
b3Capsule mover = MakeStandingMover();
|
||||
b3WorldTransform bodyTransform = IdentityAt( 0.0f, 0.0f, 0.0f );
|
||||
b3Vec3 translation = { 10.0f, 0.0f, 0.0f };
|
||||
|
||||
b3BodyTOIResult nearFirst = b3Body_TimeOfImpactMover( nearFirstId, b3Pos_zero, &mover, translation, b3DefaultQueryFilter(),
|
||||
bodyTransform, bodyTransform );
|
||||
b3BodyTOIResult nearLast = b3Body_TimeOfImpactMover( nearLastId, b3Pos_zero, &mover, translation, b3DefaultQueryFilter(),
|
||||
bodyTransform, bodyTransform );
|
||||
|
||||
ENSURE_SMALL( nearFirst.fraction - 0.425f, 1e-2f );
|
||||
ENSURE( nearFirst.shapeId.index1 == nearFirstHit.index1 );
|
||||
|
||||
ENSURE_SMALL( nearLast.fraction - nearFirst.fraction, 1e-4f );
|
||||
ENSURE( nearLast.shapeId.index1 == nearLastHit.index1 );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// A shape the sweep clears must not erase a hit found earlier in the list. The off path sphere
|
||||
// is created first so the list hands it over last.
|
||||
static int MoverTOIKeepsHitAfterMiss( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3Sphere offPath = { { 5.0f, 0.0f, 20.0f }, 0.5f };
|
||||
b3Sphere onPath = { { 5.0f, 0.0f, 0.0f }, 0.5f };
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &offPath );
|
||||
b3ShapeId onPathId = b3CreateSphereShape( bodyId, &shapeDef, &onPath );
|
||||
|
||||
b3Capsule mover = MakeStandingMover();
|
||||
b3WorldTransform bodyTransform = IdentityAt( 0.0f, 0.0f, 0.0f );
|
||||
b3BodyTOIResult result = b3Body_TimeOfImpactMover( bodyId, b3Pos_zero, &mover, (b3Vec3){ 10.0f, 0.0f, 0.0f },
|
||||
b3DefaultQueryFilter(), bodyTransform, bodyTransform );
|
||||
|
||||
ENSURE_SMALL( result.fraction - 0.425f, 1e-2f );
|
||||
ENSURE( result.shapeId.index1 == onPathId.index1 );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The mover capsule points live in the query frame, so sliding both the mover and the body by
|
||||
// the same offset must not move the impact.
|
||||
static int MoverTOIMoverOffset( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
b3Vec3 translation = { 10.0f, 0.0f, 0.0f };
|
||||
b3QueryFilter filter = b3DefaultQueryFilter();
|
||||
|
||||
b3Capsule atOrigin = MakeStandingMover();
|
||||
b3WorldTransform transformA = IdentityAt( 5.0f, 0.0f, 0.0f );
|
||||
b3BodyTOIResult resultA =
|
||||
b3Body_TimeOfImpactMover( bodyId, b3Pos_zero, &atOrigin, translation, filter, transformA, transformA );
|
||||
|
||||
b3Capsule offset = { { 2.0f, 0.0f, 0.0f }, { 2.0f, 1.0f, 0.0f }, 0.25f };
|
||||
b3WorldTransform transformB = IdentityAt( 7.0f, 0.0f, 0.0f );
|
||||
b3BodyTOIResult resultB = b3Body_TimeOfImpactMover( bodyId, b3Pos_zero, &offset, translation, filter, transformB, transformB );
|
||||
|
||||
ENSURE_SMALL( resultB.fraction - resultA.fraction, 1e-4f );
|
||||
ENSURE_SMALL( resultB.point.x - resultA.point.x - 2.0f, 1e-3f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The body sweeps between the two supplied transforms. A bar spinning a quarter turn about Y
|
||||
// reaches a mover that its start pose clears.
|
||||
static int MoverTOIRotatingBody( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull bar = b3MakeBoxHull( 2.0f, 0.25f, 0.25f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &bar.base );
|
||||
|
||||
// Query origin puts the mover at world (0,0,2.1), just inside the swept end of the bar.
|
||||
b3Pos origin = { 0.0f, 0.0f, 2.1f };
|
||||
b3Capsule mover = MakeStandingMover();
|
||||
b3WorldTransform transform1 = IdentityAt( 0.0f, 0.0f, 0.0f );
|
||||
b3WorldTransform transform2 = {
|
||||
.p = (b3Pos){ 0.0f, 0.0f, 0.0f },
|
||||
.q = b3MakeQuatFromAxisAngle( (b3Vec3){ 0.0f, 1.0f, 0.0f }, 0.5f * B3_PI ),
|
||||
};
|
||||
|
||||
b3BodyTOIResult spinning =
|
||||
b3Body_TimeOfImpactMover( bodyId, origin, &mover, b3Vec3_zero, b3DefaultQueryFilter(), transform1, transform2 );
|
||||
|
||||
ENSURE( 0.0f < spinning.fraction && spinning.fraction < 1.0f );
|
||||
|
||||
// Holding the start pose leaves the bar along X and well clear.
|
||||
b3BodyTOIResult still =
|
||||
b3Body_TimeOfImpactMover( bodyId, origin, &mover, b3Vec3_zero, b3DefaultQueryFilter(), transform1, transform1 );
|
||||
|
||||
ENSURE_SMALL( still.fraction - 1.0f, 1e-6f );
|
||||
ENSURE( B3_IS_NULL( still.shapeId ) );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Everything is re-centered on the origin, so a huge origin must not shift the fraction and the
|
||||
// hit point comes back in the origin frame.
|
||||
static int MoverTOIFarFromOrigin( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
b3Pos origin = { 1.0e6f, -2.0e6f, 5.0e5f };
|
||||
b3Capsule mover = MakeStandingMover();
|
||||
b3WorldTransform bodyTransform = { .p = b3OffsetPos( origin, (b3Vec3){ 5.0f, 0.0f, 0.0f } ), .q = b3Quat_identity };
|
||||
b3BodyTOIResult result = b3Body_TimeOfImpactMover( bodyId, origin, &mover, (b3Vec3){ 10.0f, 0.0f, 0.0f },
|
||||
b3DefaultQueryFilter(), bodyTransform, bodyTransform );
|
||||
|
||||
ENSURE_SMALL( result.fraction - 0.425f, 1e-2f );
|
||||
ENSURE_SMALL( result.normal.x + 1.0f, 1e-3f );
|
||||
ENSURE_SMALL( (result.point.x - origin.x) - 4.4f, 0.5f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverTOIFilter( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
// Geometry is on the sweep line, but a zero mask rejects every category.
|
||||
b3Capsule mover = MakeStandingMover();
|
||||
b3QueryFilter filter = b3DefaultQueryFilter();
|
||||
filter.maskBits = 0;
|
||||
b3WorldTransform bodyTransform = IdentityAt( 5.0f, 0.0f, 0.0f );
|
||||
b3BodyTOIResult result = b3Body_TimeOfImpactMover( bodyId, b3Pos_zero, &mover, (b3Vec3){ 10.0f, 0.0f, 0.0f }, filter,
|
||||
bodyTransform, bodyTransform );
|
||||
|
||||
ENSURE_SMALL( result.fraction - 1.0f, 1e-6f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// A shape the mover starts inside is skipped, but the sweep still runs against the other shapes
|
||||
// on the body.
|
||||
static int MoverTOIOverlapSkipsShape( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
|
||||
b3BoxHull buried = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3CreateHullShape( bodyId, &shapeDef, &buried.base );
|
||||
|
||||
// Face at x = 4.5 like MoverTOIHitsBox
|
||||
b3BoxHull onPath = b3MakeOffsetBoxHull( 0.5f, 0.5f, 0.5f, (b3Vec3){ 5.0f, 0.0f, 0.0f } );
|
||||
b3ShapeId onPathId = b3CreateHullShape( bodyId, &shapeDef, &onPath.base );
|
||||
|
||||
b3Capsule mover = MakeStandingMover();
|
||||
b3WorldTransform bodyTransform = IdentityAt( 0.0f, 0.0f, 0.0f );
|
||||
b3BodyTOIResult result = b3Body_TimeOfImpactMover( bodyId, b3Pos_zero, &mover, (b3Vec3){ 10.0f, 0.0f, 0.0f },
|
||||
b3DefaultQueryFilter(), bodyTransform, bodyTransform );
|
||||
|
||||
ENSURE_SMALL( result.fraction - 0.425f, 1e-2f );
|
||||
ENSURE( result.shapeId.index1 == onPathId.index1 );
|
||||
ENSURE_SMALL( result.normal.x + 1.0f, 1e-3f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static float RandomFloat( uint32_t* seed, float lower, float upper )
|
||||
{
|
||||
uint32_t x = *seed;
|
||||
x ^= x << 13;
|
||||
x ^= x >> 17;
|
||||
x ^= x << 5;
|
||||
*seed = x;
|
||||
float unit = (float)( x >> 8 ) / 16777216.0f;
|
||||
return lower + ( upper - lower ) * unit;
|
||||
}
|
||||
|
||||
static b3Vec3 RandomVector( uint32_t* seed, float lower, float upper )
|
||||
{
|
||||
b3Vec3 v;
|
||||
v.x = RandomFloat( seed, lower, upper );
|
||||
v.y = RandomFloat( seed, lower, upper );
|
||||
v.z = RandomFloat( seed, lower, upper );
|
||||
return v;
|
||||
}
|
||||
|
||||
static b3Vec3 RandomDirection( uint32_t* seed )
|
||||
{
|
||||
for ( ;; )
|
||||
{
|
||||
b3Vec3 v = RandomVector( seed, -1.0f, 1.0f );
|
||||
float length = b3Length( v );
|
||||
if ( 0.1f < length && length < 1.0f )
|
||||
{
|
||||
return b3MulSV( 1.0f / length, v );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The mover sweep follows the solver's continuous rule: any time of impact strictly inside the
|
||||
// sweep counts, whatever the root finder reports. A capsule pivoting around a hull vertex can
|
||||
// exhaust the iteration cap, and that must not read as a clean miss. Compare against the raw
|
||||
// sweep built from the same inputs.
|
||||
static int MoverTOIMatchesSweep( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull bar = b3MakeBoxHull( 2.0f, 0.25f, 0.25f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3ShapeId shapeId = b3CreateHullShape( bodyId, &shapeDef, &bar.base );
|
||||
|
||||
uint32_t seed = 0x9E3779B9u;
|
||||
int hitCount = 0;
|
||||
for ( int i = 0; i < 2000; ++i )
|
||||
{
|
||||
b3Capsule mover;
|
||||
mover.center1 = RandomVector( &seed, -0.2f, 0.2f );
|
||||
mover.center2 = b3MulAdd( mover.center1, RandomFloat( &seed, 0.05f, 1.5f ), RandomDirection( &seed ) );
|
||||
mover.radius = RandomFloat( &seed, 0.05f, 0.5f );
|
||||
b3Vec3 capsulePoints[2] = { mover.center1, mover.center2 };
|
||||
|
||||
b3Vec3 o = RandomVector( &seed, -3.5f, 3.5f );
|
||||
b3Pos origin = { o.x, o.y, o.z };
|
||||
b3Vec3 translation = b3MulSV( RandomFloat( &seed, 0.0f, 3.0f ), RandomDirection( &seed ) );
|
||||
|
||||
b3WorldTransform transform1 = {
|
||||
.p = b3Pos_zero,
|
||||
.q = b3MakeQuatFromAxisAngle( RandomDirection( &seed ), RandomFloat( &seed, 0.0f, B3_PI ) ),
|
||||
};
|
||||
b3Quat spin = b3MakeQuatFromAxisAngle( RandomDirection( &seed ), RandomFloat( &seed, 0.001f, B3_PI ) );
|
||||
b3Vec3 shift = RandomVector( &seed, -1.0f, 1.0f );
|
||||
b3WorldTransform transform2 = {
|
||||
.p = (b3Pos){ shift.x, shift.y, shift.z },
|
||||
.q = b3MulQuat( spin, transform1.q ),
|
||||
};
|
||||
|
||||
b3BodyTOIResult result = b3Body_TimeOfImpactMover( bodyId, origin, &mover, translation, b3DefaultQueryFilter(),
|
||||
transform1, transform2 );
|
||||
|
||||
// Same sweep the body query builds. The static body keeps its center at the body origin.
|
||||
b3Transform xf1 = b3ToRelativeTransform( transform1, origin );
|
||||
b3Transform xf2 = b3ToRelativeTransform( transform2, origin );
|
||||
|
||||
b3TOIInput input = { 0 };
|
||||
input.proxyA = (b3ShapeProxy){ b3GetHullPoints( &bar.base ), bar.base.vertexCount, 0.0f };
|
||||
input.proxyB = (b3ShapeProxy){ capsulePoints, 2, mover.radius };
|
||||
input.sweepA.c1 = xf1.p;
|
||||
input.sweepA.c2 = xf2.p;
|
||||
input.sweepA.q1 = transform1.q;
|
||||
input.sweepA.q2 = transform2.q;
|
||||
input.sweepA.localCenter = b3Vec3_zero;
|
||||
input.sweepB.c1 = b3Vec3_zero;
|
||||
input.sweepB.c2 = translation;
|
||||
input.sweepB.q1 = b3Quat_identity;
|
||||
input.sweepB.q2 = b3Quat_identity;
|
||||
input.sweepB.localCenter = b3Vec3_zero;
|
||||
input.maxFraction = 1.0f;
|
||||
|
||||
b3TOIOutput raw = b3TimeOfImpact( &input );
|
||||
|
||||
if ( 0.0f < raw.fraction && raw.fraction < 1.0f )
|
||||
{
|
||||
ENSURE_SMALL( result.fraction - raw.fraction, 1e-6f );
|
||||
ENSURE( result.shapeId.index1 == shapeId.index1 );
|
||||
ENSURE( b3IsNormalized( result.normal ) );
|
||||
ENSURE_SMALL( b3Distance( b3SubPos( result.point, origin ), raw.point ), 1e-4f );
|
||||
hitCount += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ENSURE_SMALL( result.fraction - 1.0f, 1e-6f );
|
||||
ENSURE( B3_IS_NULL( result.shapeId ) );
|
||||
}
|
||||
}
|
||||
|
||||
ENSURE( hitCount > 0 );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BodyQueryTest( void )
|
||||
{
|
||||
RUN_SUBTEST( CastRayHitsSphere );
|
||||
|
|
@ -589,6 +995,17 @@ int BodyQueryTest( void )
|
|||
RUN_SUBTEST( MoverSeparated );
|
||||
RUN_SUBTEST( MoverRotatedBody );
|
||||
RUN_SUBTEST( MoverCapacity );
|
||||
RUN_SUBTEST( MoverTOIHitsBox );
|
||||
RUN_SUBTEST( MoverTOISeparated );
|
||||
RUN_SUBTEST( MoverTOIOverlapped );
|
||||
RUN_SUBTEST( MoverTOIClosestShape );
|
||||
RUN_SUBTEST( MoverTOIKeepsHitAfterMiss );
|
||||
RUN_SUBTEST( MoverTOIMoverOffset );
|
||||
RUN_SUBTEST( MoverTOIRotatingBody );
|
||||
RUN_SUBTEST( MoverTOIFarFromOrigin );
|
||||
RUN_SUBTEST( MoverTOIFilter );
|
||||
RUN_SUBTEST( MoverTOIOverlapSkipsShape );
|
||||
RUN_SUBTEST( MoverTOIMatchesSweep );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -526,6 +526,153 @@ static int RayCastBruteForce( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Flat 3x3 vertex field at y = 0, one cell material per entry. Caller destroys.
|
||||
static b3HeightFieldData* MakeFlatField( uint8_t* materials, bool clockwise )
|
||||
{
|
||||
float heights[9] = { 0 };
|
||||
|
||||
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 = clockwise;
|
||||
|
||||
return b3CreateHeightField( &def );
|
||||
}
|
||||
|
||||
static int ShapeCastBackside( void )
|
||||
{
|
||||
uint8_t materials[4] = { 1, 0, 0, 0 };
|
||||
b3HeightFieldData* hf = MakeFlatField( materials, false );
|
||||
|
||||
// Falling onto the front (upper) face reports the hit, the triangle, and its material.
|
||||
// The surface sits at y = 0, so a sphere of radius 0.2 stops with its center at y = 0.2.
|
||||
b3Vec3 above = { 0.3f, 5.0f, 0.25f };
|
||||
b3ShapeCastInput down = { 0 };
|
||||
down.proxy = (b3ShapeProxy){ &above, 1, 0.2f };
|
||||
down.translation = (b3Vec3){ 0.0f, -10.0f, 0.0f };
|
||||
down.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput out = b3ShapeCastHeightField( hf, &down );
|
||||
ENSURE( out.hit == true );
|
||||
ENSURE_SMALL( out.fraction - 0.48f, 1e-2f );
|
||||
ENSURE( out.normal.y > 0.99f );
|
||||
ENSURE( out.triangleIndex == 0 || out.triangleIndex == 1 );
|
||||
ENSURE( out.materialIndex == 1 );
|
||||
|
||||
// Rising from behind the face is culled
|
||||
b3Vec3 below = { 0.3f, -5.0f, 0.25f };
|
||||
b3ShapeCastInput up = { 0 };
|
||||
up.proxy = (b3ShapeProxy){ &below, 1, 0.2f };
|
||||
up.translation = (b3Vec3){ 0.0f, 10.0f, 0.0f };
|
||||
up.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput missed = b3ShapeCastHeightField( hf, &up );
|
||||
ENSURE( missed.hit == false );
|
||||
|
||||
b3DestroyHeightField( hf );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// A clockwise height field faces down, see HeightFieldWinding. Shape cast backside
|
||||
// culling must respect the flag: a rising sphere hits from below, not from above.
|
||||
static int ShapeCastClockwiseWinding( void )
|
||||
{
|
||||
uint8_t materials[4] = { 1, 0, 0, 0 };
|
||||
b3HeightFieldData* hf = MakeFlatField( materials, true );
|
||||
|
||||
b3Vec3 below = { 0.3f, -5.0f, 0.25f };
|
||||
b3ShapeCastInput up = { 0 };
|
||||
up.proxy = (b3ShapeProxy){ &below, 1, 0.2f };
|
||||
up.translation = (b3Vec3){ 0.0f, 10.0f, 0.0f };
|
||||
up.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput out = b3ShapeCastHeightField( hf, &up );
|
||||
ENSURE( out.hit == true );
|
||||
ENSURE_SMALL( out.fraction - 0.48f, 1e-2f );
|
||||
ENSURE( out.normal.y < -0.99f );
|
||||
ENSURE( out.triangleIndex == 0 || out.triangleIndex == 1 );
|
||||
ENSURE( out.materialIndex == 1 );
|
||||
|
||||
b3Vec3 above = { 0.3f, 5.0f, 0.25f };
|
||||
b3ShapeCastInput down = { 0 };
|
||||
down.proxy = (b3ShapeProxy){ &above, 1, 0.2f };
|
||||
down.translation = (b3Vec3){ 0.0f, -10.0f, 0.0f };
|
||||
down.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput missed = b3ShapeCastHeightField( hf, &down );
|
||||
ENSURE( missed.hit == false );
|
||||
|
||||
b3DestroyHeightField( hf );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Ray casts run through the shape cast. The default winding faces up, so a ray from below is the
|
||||
// back side and gets culled.
|
||||
static int RayCastBackside( void )
|
||||
{
|
||||
uint8_t materials[4] = { 1, 0, 0, 0 };
|
||||
b3HeightFieldData* hf = MakeFlatField( materials, false );
|
||||
|
||||
b3RayCastInput down = { 0 };
|
||||
down.origin = (b3Vec3){ 0.3f, 5.0f, 0.25f };
|
||||
down.translation = (b3Vec3){ 0.0f, -10.0f, 0.0f };
|
||||
down.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput out = b3RayCastHeightField( hf, &down );
|
||||
ENSURE( out.hit == true );
|
||||
ENSURE_SMALL( out.fraction - 0.5f, 1e-3f );
|
||||
ENSURE( out.normal.y > 0.99f );
|
||||
ENSURE( out.triangleIndex == 0 || out.triangleIndex == 1 );
|
||||
ENSURE( out.materialIndex == 1 );
|
||||
|
||||
b3RayCastInput up = { 0 };
|
||||
up.origin = (b3Vec3){ 0.3f, -5.0f, 0.25f };
|
||||
up.translation = (b3Vec3){ 0.0f, 10.0f, 0.0f };
|
||||
up.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput missed = b3RayCastHeightField( hf, &up );
|
||||
ENSURE( missed.hit == false );
|
||||
|
||||
b3DestroyHeightField( hf );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// A clockwise field faces down. The winding swap already flips the triangle, so the ray normal
|
||||
// must not be flipped a second time.
|
||||
static int RayCastClockwiseWinding( void )
|
||||
{
|
||||
uint8_t materials[4] = { 1, 0, 0, 0 };
|
||||
b3HeightFieldData* hf = MakeFlatField( materials, true );
|
||||
|
||||
b3RayCastInput up = { 0 };
|
||||
up.origin = (b3Vec3){ 0.3f, -5.0f, 0.25f };
|
||||
up.translation = (b3Vec3){ 0.0f, 10.0f, 0.0f };
|
||||
up.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput out = b3RayCastHeightField( hf, &up );
|
||||
ENSURE( out.hit == true );
|
||||
ENSURE_SMALL( out.fraction - 0.5f, 1e-3f );
|
||||
ENSURE( out.normal.y < -0.99f );
|
||||
ENSURE( out.triangleIndex == 0 || out.triangleIndex == 1 );
|
||||
ENSURE( out.materialIndex == 1 );
|
||||
|
||||
b3RayCastInput down = { 0 };
|
||||
down.origin = (b3Vec3){ 0.3f, 5.0f, 0.25f };
|
||||
down.translation = (b3Vec3){ 0.0f, -10.0f, 0.0f };
|
||||
down.maxFraction = 1.0f;
|
||||
|
||||
b3CastOutput missed = b3RayCastHeightField( hf, &down );
|
||||
ENSURE( missed.hit == false );
|
||||
|
||||
b3DestroyHeightField( hf );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int HeightFieldTest( void )
|
||||
{
|
||||
RUN_SUBTEST( HeightFieldCreate );
|
||||
|
|
@ -537,6 +684,10 @@ int HeightFieldTest( void )
|
|||
RUN_SUBTEST( ShapeCastVerticalStraddle );
|
||||
RUN_SUBTEST( ShapeCastBruteForce );
|
||||
RUN_SUBTEST( RayCastBruteForce );
|
||||
RUN_SUBTEST( ShapeCastBackside );
|
||||
RUN_SUBTEST( ShapeCastClockwiseWinding );
|
||||
RUN_SUBTEST( RayCastBackside );
|
||||
RUN_SUBTEST( RayCastClockwiseWinding );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
355
test/test_mesh.c
Normal file
355
test/test_mesh.c
Normal file
|
|
@ -0,0 +1,355 @@
|
|||
// SPDX-FileCopyrightText: 2026 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
#include "box3d/collision.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
||||
#include <float.h>
|
||||
#include <string.h>
|
||||
|
||||
// Two quads meeting at a concave crease along the shared edge 1-4. The crease exercises
|
||||
// edge identification, which reads the baked winding rather than the input winding.
|
||||
#define VALLEY_VERTEX_COUNT 6
|
||||
#define VALLEY_TRIANGLE_COUNT 4
|
||||
|
||||
static void MakeValley( b3Vec3* vertices, int32_t* indices )
|
||||
{
|
||||
b3Vec3 v[VALLEY_VERTEX_COUNT] = {
|
||||
{ -1.0f, 1.0f, -1.0f }, { 0.0f, 0.0f, -1.0f }, { 1.0f, 1.0f, -1.0f },
|
||||
{ -1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f },
|
||||
};
|
||||
|
||||
int32_t i[3 * VALLEY_TRIANGLE_COUNT] = {
|
||||
0, 3, 1, // left quad
|
||||
3, 4, 1,
|
||||
1, 4, 2, // right quad
|
||||
4, 5, 2,
|
||||
};
|
||||
|
||||
memcpy( vertices, v, sizeof( v ) );
|
||||
memcpy( indices, i, sizeof( i ) );
|
||||
}
|
||||
|
||||
// A vertex laid out the way a renderer would hand it over. The position sits off the
|
||||
// front of the struct so a non-zero base offset gets exercised too.
|
||||
typedef struct FatVertex
|
||||
{
|
||||
float weight;
|
||||
b3Vec3 position;
|
||||
float uv[2];
|
||||
} FatVertex;
|
||||
|
||||
static void MakeFatVertices( FatVertex* fat, const b3Vec3* vertices, int count )
|
||||
{
|
||||
for ( int i = 0; i < count; ++i )
|
||||
{
|
||||
// Poison the padding so a stride mistake bakes obvious garbage instead of near misses
|
||||
fat[i].weight = 1000.0f + (float)i;
|
||||
fat[i].position = vertices[i];
|
||||
fat[i].uv[0] = -1000.0f;
|
||||
fat[i].uv[1] = -2000.0f;
|
||||
}
|
||||
}
|
||||
|
||||
static void ReverseWinding( int32_t* indices, int triangleCount )
|
||||
{
|
||||
for ( int i = 0; i < triangleCount; ++i )
|
||||
{
|
||||
int32_t temp = indices[3 * i + 1];
|
||||
indices[3 * i + 1] = indices[3 * i + 2];
|
||||
indices[3 * i + 2] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// The hash covers every byte of the mesh block, so this compares the tree, the vertices,
|
||||
// the baked triangles and the edge flags in one shot.
|
||||
static bool MeshesMatch( const b3MeshData* mesh1, const b3MeshData* mesh2 )
|
||||
{
|
||||
return mesh1->byteCount == mesh2->byteCount && mesh1->hash == mesh2->hash;
|
||||
}
|
||||
|
||||
static bool VerticesMatch( const b3MeshData* mesh, const b3Vec3* expected, int count )
|
||||
{
|
||||
if ( mesh->vertexCount != count )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const b3Vec3* vertices = b3GetMeshVertices( mesh );
|
||||
for ( int i = 0; i < count; ++i )
|
||||
{
|
||||
if ( b3DistanceSquared( vertices[i], expected[i] ) > FLT_EPSILON )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// The valley faces up, so every baked triangle should wind counter clockwise about +Y
|
||||
static bool FacesUp( const b3MeshData* mesh )
|
||||
{
|
||||
const b3Vec3* vertices = b3GetMeshVertices( mesh );
|
||||
const b3MeshTriangle* triangles = b3GetMeshTriangles( mesh );
|
||||
|
||||
for ( int i = 0; i < mesh->triangleCount; ++i )
|
||||
{
|
||||
b3Vec3 v1 = vertices[triangles[i].index1];
|
||||
b3Vec3 v2 = vertices[triangles[i].index2];
|
||||
b3Vec3 v3 = vertices[triangles[i].index3];
|
||||
|
||||
b3Vec3 normal = b3Cross( b3Sub( v2, v1 ), b3Sub( v3, v1 ) );
|
||||
if ( normal.y <= 0.0f )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool HasConcaveEdge( const b3MeshData* mesh )
|
||||
{
|
||||
const uint8_t* flags = b3GetMeshFlags( mesh );
|
||||
|
||||
for ( int i = 0; i < mesh->triangleCount; ++i )
|
||||
{
|
||||
if ( ( flags[i] & b3_allConcaveEdges ) != 0 )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static b3MeshDef MakeValleyDef( b3Vec3* vertices, int32_t* indices )
|
||||
{
|
||||
b3MeshDef def = { 0 };
|
||||
def.vertices = vertices;
|
||||
def.stride = sizeof( b3Vec3 );
|
||||
def.indices = indices;
|
||||
def.vertexCount = VALLEY_VERTEX_COUNT;
|
||||
def.triangleCount = VALLEY_TRIANGLE_COUNT;
|
||||
def.identifyEdges = true;
|
||||
return def;
|
||||
}
|
||||
|
||||
// Dense input is the reference the other cases are measured against
|
||||
static int MeshDenseStride( void )
|
||||
{
|
||||
b3Vec3 vertices[VALLEY_VERTEX_COUNT];
|
||||
int32_t indices[3 * VALLEY_TRIANGLE_COUNT];
|
||||
MakeValley( vertices, indices );
|
||||
|
||||
b3MeshDef def = MakeValleyDef( vertices, indices );
|
||||
b3MeshData* mesh = b3CreateMesh( &def, NULL, 0 );
|
||||
ENSURE( mesh != NULL );
|
||||
|
||||
ENSURE( mesh->vertexCount == VALLEY_VERTEX_COUNT );
|
||||
ENSURE( mesh->triangleCount == VALLEY_TRIANGLE_COUNT );
|
||||
ENSURE( mesh->degenerateCount == 0 );
|
||||
ENSURE( VerticesMatch( mesh, vertices, VALLEY_VERTEX_COUNT ) );
|
||||
ENSURE( FacesUp( mesh ) );
|
||||
ENSURE( HasConcaveEdge( mesh ) );
|
||||
|
||||
b3DestroyMesh( mesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Interleaved input must bake to exactly the same mesh as dense input
|
||||
static int MeshFatStride( void )
|
||||
{
|
||||
b3Vec3 vertices[VALLEY_VERTEX_COUNT];
|
||||
int32_t indices[3 * VALLEY_TRIANGLE_COUNT];
|
||||
MakeValley( vertices, indices );
|
||||
|
||||
b3MeshDef denseDef = MakeValleyDef( vertices, indices );
|
||||
b3MeshData* denseMesh = b3CreateMesh( &denseDef, NULL, 0 );
|
||||
ENSURE( denseMesh != NULL );
|
||||
|
||||
FatVertex fat[VALLEY_VERTEX_COUNT];
|
||||
MakeFatVertices( fat, vertices, VALLEY_VERTEX_COUNT );
|
||||
|
||||
b3MeshDef fatDef = MakeValleyDef( &fat[0].position, indices );
|
||||
fatDef.stride = sizeof( FatVertex );
|
||||
b3MeshData* fatMesh = b3CreateMesh( &fatDef, NULL, 0 );
|
||||
ENSURE( fatMesh != NULL );
|
||||
|
||||
ENSURE( VerticesMatch( fatMesh, vertices, VALLEY_VERTEX_COUNT ) );
|
||||
ENSURE( FacesUp( fatMesh ) );
|
||||
ENSURE( MeshesMatch( denseMesh, fatMesh ) );
|
||||
|
||||
b3DestroyMesh( fatMesh );
|
||||
b3DestroyMesh( denseMesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Welding reads the source vertices through the stride as well
|
||||
static int MeshStrideWeld( void )
|
||||
{
|
||||
b3Vec3 vertices[VALLEY_VERTEX_COUNT];
|
||||
int32_t indices[3 * VALLEY_TRIANGLE_COUNT];
|
||||
MakeValley( vertices, indices );
|
||||
|
||||
// Split the crease so each quad owns a copy of the shared edge
|
||||
b3Vec3 splitVertices[8];
|
||||
memcpy( splitVertices, vertices, sizeof( vertices ) );
|
||||
splitVertices[6] = vertices[1];
|
||||
splitVertices[7] = vertices[4];
|
||||
|
||||
// Point the right quad at the copies
|
||||
int32_t splitIndices[3 * VALLEY_TRIANGLE_COUNT];
|
||||
memcpy( splitIndices, indices, sizeof( indices ) );
|
||||
splitIndices[6] = 6;
|
||||
splitIndices[7] = 7;
|
||||
splitIndices[9] = 7;
|
||||
|
||||
FatVertex fat[8];
|
||||
MakeFatVertices( fat, splitVertices, 8 );
|
||||
|
||||
b3MeshDef def = MakeValleyDef( &fat[0].position, splitIndices );
|
||||
def.stride = sizeof( FatVertex );
|
||||
def.vertexCount = 8;
|
||||
def.weldVertices = true;
|
||||
def.weldTolerance = 0.01f;
|
||||
|
||||
b3MeshData* mesh = b3CreateMesh( &def, NULL, 0 );
|
||||
ENSURE( mesh != NULL );
|
||||
|
||||
ENSURE( mesh->vertexCount == VALLEY_VERTEX_COUNT );
|
||||
ENSURE( mesh->triangleCount == VALLEY_TRIANGLE_COUNT );
|
||||
ENSURE( VerticesMatch( mesh, vertices, VALLEY_VERTEX_COUNT ) );
|
||||
ENSURE( FacesUp( mesh ) );
|
||||
ENSURE( HasConcaveEdge( mesh ) );
|
||||
|
||||
b3DestroyMesh( mesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Clockwise input plus the flag must bake to the same mesh as counter clockwise input
|
||||
static int MeshClockWise( void )
|
||||
{
|
||||
b3Vec3 vertices[VALLEY_VERTEX_COUNT];
|
||||
int32_t indices[3 * VALLEY_TRIANGLE_COUNT];
|
||||
MakeValley( vertices, indices );
|
||||
|
||||
b3MeshDef ccwDef = MakeValleyDef( vertices, indices );
|
||||
b3MeshData* ccwMesh = b3CreateMesh( &ccwDef, NULL, 0 );
|
||||
ENSURE( ccwMesh != NULL );
|
||||
|
||||
int32_t reversed[3 * VALLEY_TRIANGLE_COUNT];
|
||||
memcpy( reversed, indices, sizeof( indices ) );
|
||||
ReverseWinding( reversed, VALLEY_TRIANGLE_COUNT );
|
||||
|
||||
b3MeshDef cwDef = MakeValleyDef( vertices, reversed );
|
||||
cwDef.clockWiseWinding = true;
|
||||
b3MeshData* cwMesh = b3CreateMesh( &cwDef, NULL, 0 );
|
||||
ENSURE( cwMesh != NULL );
|
||||
|
||||
ENSURE( FacesUp( cwMesh ) );
|
||||
ENSURE( HasConcaveEdge( cwMesh ) );
|
||||
ENSURE( MeshesMatch( ccwMesh, cwMesh ) );
|
||||
|
||||
b3DestroyMesh( cwMesh );
|
||||
b3DestroyMesh( ccwMesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Without the flag the same clockwise input must bake inside out
|
||||
static int MeshClockWiseIgnored( void )
|
||||
{
|
||||
b3Vec3 vertices[VALLEY_VERTEX_COUNT];
|
||||
int32_t indices[3 * VALLEY_TRIANGLE_COUNT];
|
||||
MakeValley( vertices, indices );
|
||||
ReverseWinding( indices, VALLEY_TRIANGLE_COUNT );
|
||||
|
||||
b3MeshDef def = MakeValleyDef( vertices, indices );
|
||||
b3MeshData* mesh = b3CreateMesh( &def, NULL, 0 );
|
||||
ENSURE( mesh != NULL );
|
||||
|
||||
ENSURE( FacesUp( mesh ) == false );
|
||||
|
||||
b3DestroyMesh( mesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Winding, stride and welding have to compose
|
||||
static int MeshClockWiseStrideWeld( void )
|
||||
{
|
||||
b3Vec3 vertices[VALLEY_VERTEX_COUNT];
|
||||
int32_t indices[3 * VALLEY_TRIANGLE_COUNT];
|
||||
MakeValley( vertices, indices );
|
||||
|
||||
b3MeshDef ccwDef = MakeValleyDef( vertices, indices );
|
||||
ccwDef.weldVertices = true;
|
||||
ccwDef.weldTolerance = 0.01f;
|
||||
b3MeshData* ccwMesh = b3CreateMesh( &ccwDef, NULL, 0 );
|
||||
ENSURE( ccwMesh != NULL );
|
||||
|
||||
int32_t reversed[3 * VALLEY_TRIANGLE_COUNT];
|
||||
memcpy( reversed, indices, sizeof( indices ) );
|
||||
ReverseWinding( reversed, VALLEY_TRIANGLE_COUNT );
|
||||
|
||||
FatVertex fat[VALLEY_VERTEX_COUNT];
|
||||
MakeFatVertices( fat, vertices, VALLEY_VERTEX_COUNT );
|
||||
|
||||
b3MeshDef cwDef = MakeValleyDef( &fat[0].position, reversed );
|
||||
cwDef.stride = sizeof( FatVertex );
|
||||
cwDef.clockWiseWinding = true;
|
||||
cwDef.weldVertices = true;
|
||||
cwDef.weldTolerance = 0.01f;
|
||||
b3MeshData* cwMesh = b3CreateMesh( &cwDef, NULL, 0 );
|
||||
ENSURE( cwMesh != NULL );
|
||||
|
||||
ENSURE( FacesUp( cwMesh ) );
|
||||
ENSURE( MeshesMatch( ccwMesh, cwMesh ) );
|
||||
|
||||
b3DestroyMesh( cwMesh );
|
||||
b3DestroyMesh( ccwMesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The built in creators fill their own def, so a missed stride shows up here
|
||||
static int MeshCreators( void )
|
||||
{
|
||||
b3Vec3 center = { 1.0f, 2.0f, 3.0f };
|
||||
b3Vec3 extent = { 0.5f, 1.0f, 1.5f };
|
||||
|
||||
b3MeshData* meshes[] = {
|
||||
b3CreateGridMesh( 4, 4, 1.0f, 2, true ),
|
||||
b3CreateWaveMesh( 4, 4, 1.0f, 0.5f, 1.0f, 1.0f ),
|
||||
b3CreateTorusMesh( 8, 6, 2.0f, 0.5f ),
|
||||
b3CreateBoxMesh( center, extent, true ),
|
||||
b3CreateHollowBoxMesh( center, extent ),
|
||||
b3CreatePlatformMesh( center, 2.0f, 1.0f, 2.0f ),
|
||||
};
|
||||
|
||||
for ( int i = 0; i < ARRAY_COUNT( meshes ); ++i )
|
||||
{
|
||||
ENSURE( meshes[i] != NULL );
|
||||
ENSURE( meshes[i]->vertexCount >= 3 );
|
||||
ENSURE( meshes[i]->triangleCount >= 1 );
|
||||
ENSURE( b3IsSaneAABB( meshes[i]->bounds ) );
|
||||
|
||||
b3DestroyMesh( meshes[i] );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MeshTest( void )
|
||||
{
|
||||
RUN_SUBTEST( MeshDenseStride );
|
||||
RUN_SUBTEST( MeshFatStride );
|
||||
RUN_SUBTEST( MeshStrideWeld );
|
||||
RUN_SUBTEST( MeshClockWise );
|
||||
RUN_SUBTEST( MeshClockWiseIgnored );
|
||||
RUN_SUBTEST( MeshClockWiseStrideWeld );
|
||||
RUN_SUBTEST( MeshCreators );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
// b3CollideMoverAndSphere / Capsule / Hull are internal
|
||||
#include "shape.h"
|
||||
|
||||
#include "box3d/box3d.h"
|
||||
#include "box3d/collision.h"
|
||||
|
||||
static int ParallelPlanes( void )
|
||||
|
|
@ -54,15 +55,11 @@ static int GamePlanes( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mover-collide overlap handling
|
||||
//
|
||||
// b3CollideMoverAndSphere / Capsule / Hull must never emit a plane with a
|
||||
// degenerate (zero) normal, even when the mover deeply penetrates the shape.
|
||||
// On deep overlap the GJK path returns a {0,0,0} normal; these tests guard the
|
||||
// fix that replaces it with an analytic (sphere/capsule) or dropped (hull) result.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static int MoverSphereSeparated( void )
|
||||
{
|
||||
b3Sphere shape = { { 0.0f, 0.0f, 0.0f }, 0.5f };
|
||||
|
|
@ -233,6 +230,467 @@ static int MoverHullDeepOverlap( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Mover queries report which material a contact plane came from
|
||||
// b3PlaneResult::materialIndex follows different paths per shape type. Meshes
|
||||
// report the per triangle material index. Compounds remap the child result
|
||||
// through the child material table. Convex shapes report index 0.
|
||||
static b3SurfaceMaterial MakeMaterial( float friction, uint64_t userId )
|
||||
{
|
||||
b3SurfaceMaterial m = b3DefaultSurfaceMaterial();
|
||||
m.friction = friction;
|
||||
m.userMaterialId = userId;
|
||||
return m;
|
||||
}
|
||||
|
||||
// Two separated upward facing triangles on the y = 0 plane, one per material. The bake may
|
||||
// reorder triangles but always keeps a triangle paired with its material index.
|
||||
static b3MeshData* MakeTwoMaterialMesh( void )
|
||||
{
|
||||
b3Vec3 vertices[6] = {
|
||||
{ -3.0f, 0.0f, -1.0f }, { -2.0f, 0.0f, 1.0f }, { -1.0f, 0.0f, -1.0f },
|
||||
{ 1.0f, 0.0f, -1.0f }, { 2.0f, 0.0f, 1.0f }, { 3.0f, 0.0f, -1.0f },
|
||||
};
|
||||
int32_t indices[6] = { 0, 1, 2, 3, 4, 5 };
|
||||
uint8_t materialIndices[2] = { 0, 1 };
|
||||
|
||||
b3MeshDef def = { 0 };
|
||||
def.vertices = vertices;
|
||||
def.stride = sizeof( b3Vec3 );
|
||||
def.indices = indices;
|
||||
def.materialIndices = materialIndices;
|
||||
def.vertexCount = 6;
|
||||
def.triangleCount = 2;
|
||||
|
||||
return b3CreateMesh( &def, NULL, 0 );
|
||||
}
|
||||
|
||||
// Flat 3x3 vertex field at y = 0, one cell material per entry. Caller destroys.
|
||||
static b3HeightFieldData* MakeFlatField( uint8_t* materials, bool clockwise )
|
||||
{
|
||||
float heights[9] = { 0 };
|
||||
|
||||
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 = clockwise;
|
||||
|
||||
return b3CreateHeightField( &def );
|
||||
}
|
||||
|
||||
typedef struct PlaneCapture
|
||||
{
|
||||
b3PlaneResult planes[16];
|
||||
int count;
|
||||
} PlaneCapture;
|
||||
|
||||
static bool CapturePlaneFcn( b3ShapeId shapeId, const b3PlaneResult* planes, int planeCount, void* context )
|
||||
{
|
||||
(void)shapeId;
|
||||
PlaneCapture* capture = context;
|
||||
for ( int i = 0; i < planeCount && capture->count < 16; ++i )
|
||||
{
|
||||
capture->planes[capture->count++] = planes[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int MoverWorldMeshMaterials( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_staticBody;
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3MeshData* mesh = MakeTwoMaterialMesh();
|
||||
ENSURE( mesh != NULL );
|
||||
ENSURE( mesh->materialCount == 2 );
|
||||
|
||||
b3SurfaceMaterial materials[2] = { MakeMaterial( 0.2f, 1 ), MakeMaterial( 0.8f, 2 ) };
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.materials = materials;
|
||||
shapeDef.materialCount = 2;
|
||||
b3CreateMeshShape( bodyId, &shapeDef, mesh, (b3Vec3){ 1.0f, 1.0f, 1.0f } );
|
||||
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 1 );
|
||||
|
||||
const uint8_t* bakedMaterialIndices = b3GetMeshMaterialIndices( mesh );
|
||||
|
||||
// Mover hanging just above the first triangle so its radius reaches 0.05 into the surface
|
||||
b3Capsule mover = { { -2.0f, 0.15f, 0.0f }, { -2.0f, 0.35f, 0.0f }, 0.2f };
|
||||
|
||||
PlaneCapture capture = { 0 };
|
||||
b3World_CollideMover( worldId, b3Pos_zero, &mover, b3DefaultQueryFilter(), CapturePlaneFcn, &capture );
|
||||
|
||||
ENSURE( capture.count == 1 );
|
||||
ENSURE( capture.planes[0].plane.normal.y > 0.99f );
|
||||
ENSURE_SMALL( capture.planes[0].plane.offset - 0.05f, 1e-4f );
|
||||
ENSURE_SMALL( capture.planes[0].point.y, 1e-4f );
|
||||
ENSURE( capture.planes[0].materialIndex == 0 );
|
||||
ENSURE( capture.planes[0].triangleIndex >= 0 );
|
||||
ENSURE( capture.planes[0].triangleIndex < mesh->triangleCount );
|
||||
ENSURE( bakedMaterialIndices[capture.planes[0].triangleIndex] == 0 );
|
||||
|
||||
// Same mover over the second triangle
|
||||
b3Capsule mover2 = { { 2.0f, 0.15f, 0.0f }, { 2.0f, 0.35f, 0.0f }, 0.2f };
|
||||
|
||||
PlaneCapture capture2 = { 0 };
|
||||
b3World_CollideMover( worldId, b3Pos_zero, &mover2, b3DefaultQueryFilter(), CapturePlaneFcn, &capture2 );
|
||||
|
||||
ENSURE( capture2.count == 1 );
|
||||
ENSURE( capture2.planes[0].plane.normal.y > 0.99f );
|
||||
ENSURE( capture2.planes[0].materialIndex == 1 );
|
||||
ENSURE( bakedMaterialIndices[capture2.planes[0].triangleIndex] == 1 );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
b3DestroyMesh( mesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverWorldCompoundMeshMaterials( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_staticBody;
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3MeshData* mesh = MakeTwoMaterialMesh();
|
||||
|
||||
// The hull material and the two mesh materials are distinct, so the compound material
|
||||
// table gets one slot per material. Children bake in hull then mesh order.
|
||||
b3SurfaceMaterial meshMaterials[2] = { MakeMaterial( 0.3f, 101 ), MakeMaterial( 0.6f, 202 ) };
|
||||
b3SurfaceMaterial hullMaterial = MakeMaterial( 0.9f, 303 );
|
||||
|
||||
b3CompoundMeshDef meshChild = {
|
||||
.meshData = mesh,
|
||||
.transform = b3Transform_identity,
|
||||
.scale = { 1.0f, 1.0f, 1.0f },
|
||||
.materials = meshMaterials,
|
||||
.materialCount = 2,
|
||||
};
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3CompoundHullDef hullChild = {
|
||||
.hull = &box.base,
|
||||
.transform = { .p = { -8.0f, 0.0f, 0.0f }, .q = b3Quat_identity },
|
||||
.material = hullMaterial,
|
||||
};
|
||||
|
||||
b3CompoundDef compoundDef = {
|
||||
.hulls = &hullChild,
|
||||
.hullCount = 1,
|
||||
.meshes = &meshChild,
|
||||
.meshCount = 1,
|
||||
};
|
||||
b3CompoundData* compound = b3CreateCompound( &compoundDef );
|
||||
ENSURE( compound != NULL );
|
||||
ENSURE( compound->materialCount == 3 );
|
||||
|
||||
const b3SurfaceMaterial* bakedMaterials = b3GetCompoundMaterials( compound );
|
||||
ENSURE( bakedMaterials[0].userMaterialId == 303 );
|
||||
ENSURE( bakedMaterials[1].userMaterialId == 101 );
|
||||
ENSURE( bakedMaterials[2].userMaterialId == 202 );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateBakedCompoundShape( bodyId, &shapeDef, compound );
|
||||
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 1 );
|
||||
|
||||
// Mover on top of the hull child face at y = 0.5
|
||||
b3Capsule hullMover = { { -8.0f, 0.65f, 0.0f }, { -8.0f, 0.85f, 0.0f }, 0.2f };
|
||||
|
||||
PlaneCapture hullCapture = { 0 };
|
||||
b3World_CollideMover( worldId, b3Pos_zero, &hullMover, b3DefaultQueryFilter(), CapturePlaneFcn, &hullCapture );
|
||||
|
||||
ENSURE( hullCapture.count == 1 );
|
||||
ENSURE( hullCapture.planes[0].plane.normal.y > 0.99f );
|
||||
ENSURE( hullCapture.planes[0].childIndex == 0 );
|
||||
ENSURE( hullCapture.planes[0].materialIndex == 0 );
|
||||
ENSURE( bakedMaterials[hullCapture.planes[0].materialIndex].userMaterialId == 303 );
|
||||
|
||||
// Mover over the second mesh triangle. The mesh reports triangle material 1, which the
|
||||
// compound remaps through the child material table to the shared slot of meshMaterials[1].
|
||||
b3Capsule meshMover = { { 2.0f, 0.15f, 0.0f }, { 2.0f, 0.35f, 0.0f }, 0.2f };
|
||||
|
||||
PlaneCapture meshCapture = { 0 };
|
||||
b3World_CollideMover( worldId, b3Pos_zero, &meshMover, b3DefaultQueryFilter(), CapturePlaneFcn, &meshCapture );
|
||||
|
||||
ENSURE( meshCapture.count == 1 );
|
||||
ENSURE( meshCapture.planes[0].plane.normal.y > 0.99f );
|
||||
ENSURE( meshCapture.planes[0].childIndex == 1 );
|
||||
ENSURE( meshCapture.planes[0].materialIndex == 2 );
|
||||
ENSURE( bakedMaterials[meshCapture.planes[0].materialIndex].userMaterialId == 202 );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
b3DestroyCompound( compound );
|
||||
b3DestroyMesh( mesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverBodyMaterialIndices( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
// One shape of each convex type, spaced out along X
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
|
||||
b3Sphere sphere = { { 0.0f, 0.5f, 0.0f }, 0.5f };
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
|
||||
b3BoxHull box = b3MakeTransformedBoxHull( 0.5f, 0.5f, 0.5f,
|
||||
(b3Transform){ .p = { 5.0f, 0.0f, 0.0f }, .q = b3Quat_identity } );
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
b3Capsule capsule = { { 9.0f, 0.0f, 0.0f }, { 11.0f, 0.0f, 0.0f }, 0.3f };
|
||||
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
|
||||
|
||||
b3WorldTransform bodyTransform = { .p = b3Pos_zero, .q = b3Quat_identity };
|
||||
|
||||
// Convex shapes report the base material as index 0
|
||||
b3Capsule movers[3] = {
|
||||
{ { 0.0f, 1.15f, 0.0f }, { 0.0f, 1.35f, 0.0f }, 0.2f },
|
||||
{ { 5.0f, 0.65f, 0.0f }, { 5.0f, 0.85f, 0.0f }, 0.2f },
|
||||
{ { 10.0f, 0.45f, 0.0f }, { 10.0f, 0.65f, 0.0f }, 0.2f },
|
||||
};
|
||||
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
{
|
||||
b3BodyPlaneResult planes[4];
|
||||
int count = b3Body_CollideMover( bodyId, planes, 4, b3Pos_zero, &movers[i], b3DefaultQueryFilter(), bodyTransform );
|
||||
|
||||
ENSURE( count == 1 );
|
||||
ENSURE( b3Shape_IsValid( planes[0].shapeId ) );
|
||||
ENSURE( planes[0].result.plane.normal.y > 0.99f );
|
||||
ENSURE( planes[0].result.materialIndex == 0 );
|
||||
ENSURE( planes[0].result.childIndex == 0 );
|
||||
}
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverBodySkipsMeshAndCompound( void )
|
||||
{
|
||||
// The body level mover query handles convex shapes only. Mesh and compound shapes are
|
||||
// skipped, so their material indices are only available through b3World_CollideMover.
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
|
||||
b3MeshData* mesh = MakeTwoMaterialMesh();
|
||||
ENSURE( mesh != NULL );
|
||||
b3CreateMeshShape( bodyId, &shapeDef, mesh, (b3Vec3){ 1.0f, 1.0f, 1.0f } );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3CompoundHullDef hullChild = {
|
||||
.hull = &box.base,
|
||||
.transform = { .p = { 10.0f, 0.0f, 0.0f }, .q = b3Quat_identity },
|
||||
};
|
||||
b3CompoundDef compoundDef = {
|
||||
.hulls = &hullChild,
|
||||
.hullCount = 1,
|
||||
};
|
||||
b3CompoundData* compound = b3CreateCompound( &compoundDef );
|
||||
ENSURE( compound != NULL );
|
||||
b3CreateBakedCompoundShape( bodyId, &shapeDef, compound );
|
||||
|
||||
b3WorldTransform bodyTransform = { .p = b3Pos_zero, .q = b3Quat_identity };
|
||||
|
||||
// Mover over the mesh triangle and mover over the compound hull child both find nothing
|
||||
b3Capsule meshMover = { { -2.0f, 0.15f, 0.0f }, { -2.0f, 0.35f, 0.0f }, 0.2f };
|
||||
b3Capsule compoundMover = { { 10.0f, 0.65f, 0.0f }, { 10.0f, 0.85f, 0.0f }, 0.2f };
|
||||
|
||||
b3Capsule movers[2] = { meshMover, compoundMover };
|
||||
for ( int i = 0; i < 2; ++i )
|
||||
{
|
||||
b3BodyPlaneResult planes[4];
|
||||
int count = b3Body_CollideMover( bodyId, planes, 4, b3Pos_zero, &movers[i], b3DefaultQueryFilter(), bodyTransform );
|
||||
ENSURE( count == 0 );
|
||||
}
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
b3DestroyCompound( compound );
|
||||
b3DestroyMesh( mesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// One sided mover collision.
|
||||
// Mover queries keep only triangles facing the mover. The front side follows the
|
||||
// baked winding: up for a default mesh or height field, down when the height
|
||||
// field carries clockwiseWinding. A mirror in the mesh scale must not flip it.
|
||||
static int MoverMeshBackside( void )
|
||||
{
|
||||
b3MeshData* mesh = MakeTwoMaterialMesh();
|
||||
b3Mesh shape = { .data = mesh, .scale = { 1.0f, 1.0f, 1.0f } };
|
||||
|
||||
b3PlaneResult planes[4];
|
||||
|
||||
// On the front of the left triangle, so a plane comes back
|
||||
b3Capsule above = { { -2.0f, 0.15f, 0.0f }, { -2.0f, 0.35f, 0.0f }, 0.2f };
|
||||
int count = b3CollideMoverAndMesh( planes, 4, &shape, &above );
|
||||
ENSURE( count == 1 );
|
||||
ENSURE( planes[0].plane.normal.y > 0.99f );
|
||||
ENSURE( planes[0].childIndex == 0 );
|
||||
ENSURE( planes[0].triangleIndex >= 0 && planes[0].triangleIndex < mesh->triangleCount );
|
||||
|
||||
// The same spot seen from behind the face is culled
|
||||
b3Capsule below = { { -2.0f, -0.35f, 0.0f }, { -2.0f, -0.15f, 0.0f }, 0.2f };
|
||||
count = b3CollideMoverAndMesh( planes, 4, &shape, &below );
|
||||
ENSURE( count == 0 );
|
||||
|
||||
b3DestroyMesh( mesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverMeshMirroredScale( void )
|
||||
{
|
||||
b3MeshData* mesh = MakeTwoMaterialMesh();
|
||||
|
||||
// Reflecting the scale flips the triangle winding, the collision swaps it back,
|
||||
// so the front stays up. Local x = -2 maps onto the source right triangle, material 1.
|
||||
b3Mesh shape = { .data = mesh, .scale = { -1.0f, 1.0f, 1.0f } };
|
||||
|
||||
b3PlaneResult planes[4];
|
||||
|
||||
b3Capsule above = { { -2.0f, 0.15f, 0.0f }, { -2.0f, 0.35f, 0.0f }, 0.2f };
|
||||
int count = b3CollideMoverAndMesh( planes, 4, &shape, &above );
|
||||
ENSURE( count == 1 );
|
||||
ENSURE( planes[0].plane.normal.y > 0.99f );
|
||||
ENSURE( planes[0].materialIndex == 1 );
|
||||
ENSURE( b3GetMeshMaterialIndices( mesh )[planes[0].triangleIndex] == 1 );
|
||||
|
||||
b3Capsule below = { { -2.0f, -0.35f, 0.0f }, { -2.0f, -0.15f, 0.0f }, 0.2f };
|
||||
count = b3CollideMoverAndMesh( planes, 4, &shape, &below );
|
||||
ENSURE( count == 0 );
|
||||
|
||||
b3DestroyMesh( mesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverHeightFieldBackside( void )
|
||||
{
|
||||
uint8_t materials[4] = { 0, 0, 0, 0 };
|
||||
b3HeightFieldData* hf = MakeFlatField( materials, false );
|
||||
|
||||
b3PlaneResult planes[4];
|
||||
|
||||
// Standing on the front (upper) face of the default winding
|
||||
b3Capsule above = { { 0.3f, 0.15f, 0.25f }, { 0.3f, 0.35f, 0.25f }, 0.2f };
|
||||
int count = b3CollideMoverAndHeightField( planes, 4, hf, &above );
|
||||
ENSURE( count == 1 );
|
||||
ENSURE( planes[0].plane.normal.y > 0.99f );
|
||||
ENSURE_SMALL( planes[0].plane.offset - 0.05f, 1e-4f );
|
||||
|
||||
// Under the surface is the back side and gets culled
|
||||
b3Capsule below = { { 0.3f, -0.35f, 0.25f }, { 0.3f, -0.15f, 0.25f }, 0.2f };
|
||||
count = b3CollideMoverAndHeightField( planes, 4, hf, &below );
|
||||
ENSURE( count == 0 );
|
||||
|
||||
b3DestroyHeightField( hf );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverHeightFieldReport( void )
|
||||
{
|
||||
uint8_t materials[4] = { 1, 2, 0, 0 };
|
||||
b3HeightFieldData* hf = MakeFlatField( materials, false );
|
||||
|
||||
b3PlaneResult planes[4];
|
||||
|
||||
// (0.3, 0.25) sits on the x + z <= 1 side of cell (0,0), which holds triangle 0
|
||||
b3Capsule first = { { 0.3f, 0.15f, 0.25f }, { 0.3f, 0.35f, 0.25f }, 0.2f };
|
||||
int count = b3CollideMoverAndHeightField( planes, 4, hf, &first );
|
||||
ENSURE( count == 1 );
|
||||
ENSURE( planes[0].triangleIndex == 0 );
|
||||
ENSURE( planes[0].childIndex == 0 );
|
||||
ENSURE( planes[0].materialIndex == 1 );
|
||||
|
||||
// (1.3, 0.3) sits on the x + z <= 2 side of cell (0,1), which holds triangle 2
|
||||
b3Capsule second = { { 1.3f, 0.15f, 0.3f }, { 1.3f, 0.35f, 0.3f }, 0.2f };
|
||||
count = b3CollideMoverAndHeightField( planes, 4, hf, &second );
|
||||
ENSURE( count == 1 );
|
||||
ENSURE( planes[0].triangleIndex == 2 );
|
||||
ENSURE( planes[0].materialIndex == 2 );
|
||||
|
||||
b3DestroyHeightField( hf );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// A clockwise height field faces down, see HeightFieldWinding. Backside culling
|
||||
// must respect the flag: below the surface is the front side, above is the back.
|
||||
static int MoverHeightFieldClockwise( void )
|
||||
{
|
||||
uint8_t materials[4] = { 0, 0, 0, 0 };
|
||||
b3HeightFieldData* hf = MakeFlatField( materials, true );
|
||||
|
||||
b3PlaneResult planes[4];
|
||||
|
||||
b3Capsule below = { { 0.3f, -0.35f, 0.25f }, { 0.3f, -0.15f, 0.25f }, 0.2f };
|
||||
int count = b3CollideMoverAndHeightField( planes, 4, hf, &below );
|
||||
ENSURE( count == 1 );
|
||||
ENSURE( planes[0].plane.normal.y < -0.99f );
|
||||
ENSURE_SMALL( planes[0].plane.offset - 0.05f, 1e-4f );
|
||||
ENSURE( planes[0].triangleIndex == 0 || planes[0].triangleIndex == 1 );
|
||||
|
||||
b3Capsule above = { { 0.3f, 0.15f, 0.25f }, { 0.3f, 0.35f, 0.25f }, 0.2f };
|
||||
count = b3CollideMoverAndHeightField( planes, 4, hf, &above );
|
||||
ENSURE( count == 0 );
|
||||
|
||||
b3DestroyHeightField( hf );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// A mesh can be baked with more materials than the shape carries. The reported index must stay
|
||||
// inside the shape's material array.
|
||||
static int MoverWorldMeshMaterialClamp( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_staticBody;
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3MeshData* mesh = MakeTwoMaterialMesh();
|
||||
ENSURE( mesh->materialCount == 2 );
|
||||
|
||||
// Base material only
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3ShapeId shapeId = b3CreateMeshShape( bodyId, &shapeDef, mesh, (b3Vec3){ 1.0f, 1.0f, 1.0f } );
|
||||
int shapeMaterialCount = b3Shape_GetMeshMaterialCount( shapeId );
|
||||
ENSURE( shapeMaterialCount == 1 );
|
||||
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 1 );
|
||||
|
||||
// Over the triangle baked with material 1
|
||||
b3Capsule mover = { { 2.0f, 0.15f, 0.0f }, { 2.0f, 0.35f, 0.0f }, 0.2f };
|
||||
PlaneCapture capture = { 0 };
|
||||
b3World_CollideMover( worldId, b3Pos_zero, &mover, b3DefaultQueryFilter(), CapturePlaneFcn, &capture );
|
||||
|
||||
ENSURE( capture.count == 1 );
|
||||
ENSURE( capture.planes[0].materialIndex == shapeMaterialCount - 1 );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
b3DestroyMesh( mesh );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MoverTest( void )
|
||||
{
|
||||
RUN_SUBTEST( GamePlanes );
|
||||
|
|
@ -251,5 +709,16 @@ int MoverTest( void )
|
|||
RUN_SUBTEST( MoverHullTouching );
|
||||
RUN_SUBTEST( MoverHullDeepOverlap );
|
||||
|
||||
RUN_SUBTEST( MoverWorldMeshMaterials );
|
||||
RUN_SUBTEST( MoverWorldCompoundMeshMaterials );
|
||||
RUN_SUBTEST( MoverBodyMaterialIndices );
|
||||
RUN_SUBTEST( MoverBodySkipsMeshAndCompound );
|
||||
RUN_SUBTEST( MoverMeshBackside );
|
||||
RUN_SUBTEST( MoverMeshMirroredScale );
|
||||
RUN_SUBTEST( MoverHeightFieldBackside );
|
||||
RUN_SUBTEST( MoverHeightFieldReport );
|
||||
RUN_SUBTEST( MoverHeightFieldClockwise );
|
||||
RUN_SUBTEST( MoverWorldMeshMaterialClamp );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue