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:
Erin Catto 2026-08-29 21:47:53 -07:00 • committed by GitHub
commit 47d7f7cc7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 2466 additions and 303 deletions

View file

@ -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;
}