- #86
- #88 
- #87 
- #89
This commit is contained in:
Erin Catto 2026-07-24 21:48:23 -07:00 • committed by GitHub
commit 781673be80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 705 additions and 152 deletions

View file

@ -33,6 +33,7 @@ set(BOX3D_SOURCE_FILES
dynamic_tree.c
height_field.c
hull.c
hull.h
id_pool.c
id_pool.h
island.c

View file

@ -3,7 +3,7 @@
#include "compound.h"
#include "hull_map.h"
#include "hull.h"
#include "math_internal.h"
#include "shape.h"

View file

@ -758,19 +758,6 @@ static bool b3BuildHullFaceAndCapsuleContact( b3LocalManifold* manifold, const b
return false;
}
static inline float b3DeepestPointSeparation( const b3LocalManifold* manifold )
{
// Deepest point
float minSeparation = FLT_MAX;
int pointCount = manifold->pointCount;
for ( int i = 0; i < pointCount; ++i )
{
minSeparation = b3MinFloat( minSeparation, manifold->points[i].separation );
}
return minSeparation;
}
static bool b3BuildHullAndCapsuleEdgeContact( b3LocalManifold* manifold, int capacity, const b3HullData* hullA,
const b3Capsule* capsuleB, b3Transform transformBtoA, b3SeparatingAxis query )
{
@ -934,14 +921,12 @@ void b3CollideHullAndCapsule( b3LocalManifold* manifold, int capacity, const b3H
// Create face contact
float faceSeparation = faceQuery.separation - capsuleB->radius;
b3BuildHullFaceAndCapsuleContact( manifold, hullA, capsuleB, transformBtoA, faceQuery );
if ( manifold->pointCount > 1 )
B3_VALIDATE( manifold->pointCount == 0 || manifold->pointCount == 2 );
if ( manifold->pointCount == 2 )
{
// If ( Out.PointCount <= 1 ) -> Compare with unclipped separation
// If ( Out.PointCount > 1 ) -> Be aggressive and compare with clipped separation
// Face contact can be empty if it does not realize the axis of minimum penetration
faceSeparation = b3DeepestPointSeparation( manifold );
// This becomes the clipped separation.
faceSeparation = b3MinFloat( manifold->points[0].separation, manifold->points[1].separation );
}
B3_VALIDATE( faceSeparation <= 0.0f );
// Is there a valid edge-edge axis?
if ( edgeQuery.indexA == B3_NULL_INDEX )
@ -951,10 +936,9 @@ void b3CollideHullAndCapsule( b3LocalManifold* manifold, int capacity, const b3H
// Face contact can be empty if it does not realize the axis of minimum penetration.
// Create edge contact if face contact fails or edge contact is significantly better!
const float kRelEdgeTolerance = 0.90f;
const float kAbsTolerance = 0.5f * B3_LINEAR_SLOP;
float linearSlop = B3_LINEAR_SLOP;
float edgeSeparation = edgeQuery.separation - capsuleB->radius;
if ( manifold->pointCount == 0 || edgeSeparation > kRelEdgeTolerance * faceSeparation + kAbsTolerance )
if ( manifold->pointCount == 0 || edgeSeparation > faceSeparation + linearSlop )
{
// Edge contact
b3BuildHullAndCapsuleEdgeContact( manifold, capacity, hullA, capsuleB, transformBtoA, edgeQuery );

View file

@ -137,6 +137,7 @@ void* b3AllocZeroed( size_t size );
void b3Free( void* mem, size_t size );
void* b3GrowAlloc( void* oldMem, int oldSize, int newSize );
B3_PRINTF_FORMAT( 1, 2 )
void b3Log( const char* format, ... );
// Geometry content hashes reserve zero to mean unhashed

View file

@ -4,7 +4,7 @@
// Dirk Gregorius contributed portions of this code
#include "algorithm.h"
#include "hull_map.h"
#include "hull.h"
#include "math_internal.h"
#include "shape.h"
@ -2303,6 +2303,7 @@ bool b3CompareHullData( const b3HullData* hull1, const b3HullData* hull2 )
_Static_assert( sizeof( b3HullData ) == 144, "unexpected hull data size" );
_Static_assert( sizeof( b3BoxHull ) == 648, "unexpected box hull size" );
// Implement b3HullMap.
#define NAME b3HullMap
#define KEY_TY const b3HullData*
#define VAL_TY int
@ -2772,6 +2773,8 @@ static const b3BoxHull s_boxHull = {
b3BoxHull b3MakeTransformedBoxHull( float hx, float hy, float hz, b3Transform transform )
{
B3_ASSERT( b3IsValidTransform( transform ) );
b3BoxHull boxHull = s_boxHull;
float minH = 0.2f * B3_LINEAR_SLOP;
@ -2934,6 +2937,8 @@ void b3ScaleBox( b3Vec3* halfWidths, b3Transform* transform, b3Vec3 postScale, f
// todo use new hull scaling technique
b3BoxHull b3MakeScaledBoxHull( b3Vec3 halfWidths, b3Transform transform, b3Vec3 postScale )
{
B3_ASSERT( b3IsValidTransform( transform ) );
b3Vec3 h = halfWidths;
b3Transform xf = transform;
b3ScaleBox( &h, &xf, postScale, 4.0f * B3_LINEAR_SLOP );

View file

@ -12,8 +12,8 @@
uint64_t b3HashHullData( const b3HullData* hull );
bool b3CompareHullData( const b3HullData* hull1, const b3HullData* hull2 );
// Map keyed by hull content. The world hull database stores a reference count,
// compound baking stores a byte offset. Implementation lives in hull.c.
// Map keyed by hull content. The world hull database uses the value as a reference count,
// while compound baking stores uses the value as a byte offset. Implementation is in hull.c.
#define NAME b3HullMap
#define KEY_TY const b3HullData*
#define VAL_TY int

View file

@ -11,7 +11,7 @@
#include "contact.h"
#include "core.h"
#include "ctz.h"
#include "hull_map.h"
#include "hull.h"
#include "island.h"
#include "joint.h"
#include "parallel_for.h"
@ -41,18 +41,21 @@ const b3HullData* b3AddHullToDatabase( b3World* world, const b3HullData* src )
{
b3HullMap* database = world->hullDatabase;
// Compare by content so an unowned query hull finds the shared copy.
// Compare by content to de-duplicate. Not trusting the hash.
b3HullMap_itr itr = b3HullMap_get( database, src );
if ( b3HullMap_is_end( itr ) == false )
{
// Bump reference count.
itr.data->val += 1;
return itr.data->key;
}
b3HullData* owned = b3CloneHull( src );
B3_ASSERT( owned != NULL );
b3HullMap_insert( database, owned, 1 );
return owned;
b3HullData* clone = b3CloneHull( src );
B3_ASSERT( clone != NULL );
// Start with reference count of 1.
b3HullMap_insert( database, clone, 1 );
return clone;
}
const b3HullData* b3AddOwnedHullToDatabase( b3World* world, b3HullData* owned )

View file

@ -178,7 +178,7 @@ typedef struct b3World
b3Array( b3Shape ) shapes;
// Reference counted store of shared hull data keyed by content. Shapes hold a
// pointer to the owned copy here. Opaque to avoid leaking the verstable map
// pointer to the hull stored in the db. Type erased to avoid leaking the verstable map
// type into this header.
void* hullDatabase;

View file

@ -799,23 +799,15 @@ uint64_t b3Hash64Blob( const uint8_t* bytes, int n )
static uint32_t b3RegistryPush( b3GeometryRegistry* reg, b3GeometryHashMap* map, b3GeometryHashMap_itr itr, bool hashPresent,
b3GeometryKind kind, uint64_t contentHash, uint8_t* bytes, int byteCount )
{
if ( reg->count >= reg->capacity )
{
int newCap = reg->capacity < 8 ? 8 : reg->capacity * 2;
reg->entries = (b3GeometryEntry*)b3GrowAlloc( reg->entries, reg->capacity * (int)sizeof( b3GeometryEntry ),
newCap * (int)sizeof( b3GeometryEntry ) );
reg->capacity = newCap;
}
uint32_t id = (uint32_t)reg->count;
b3GeometryEntry* entry = reg->entries + reg->count;
uint32_t id = (uint32_t)reg->entries.count;
b3GeometryEntry* entry = b3Array_Emplace( reg->entries );
entry->contentHash = contentHash;
entry->id = id;
entry->kind = kind;
entry->byteCount = byteCount;
entry->bytes = bytes; // take ownership
// Take ownership.
entry->bytes = bytes;
entry->hashNext = hashPresent ? (int)itr.data->val : B3_NULL_INDEX;
reg->count++;
if ( hashPresent )
{
@ -832,11 +824,11 @@ static b3GeometryHashMap* b3RegistryMap( b3GeometryRegistry* reg )
{
if ( reg->dedupMap == NULL )
{
b3GeometryHashMap* fresh = (b3GeometryHashMap*)b3Alloc( sizeof( b3GeometryHashMap ) );
b3GeometryHashMap* fresh = b3Alloc( sizeof( b3GeometryHashMap ) );
b3GeometryHashMap_init( fresh );
reg->dedupMap = fresh;
}
return (b3GeometryHashMap*)reg->dedupMap;
return reg->dedupMap;
}
uint32_t b3InternGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_t contentHash, uint8_t* bytes, int byteCount )
@ -848,12 +840,12 @@ uint32_t b3InternGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_
if ( hashPresent )
{
// Walk every entry sharing this hash so a collision still finds the identical blob.
for ( int idx = (int)itr.data->val; idx != B3_NULL_INDEX; idx = reg->entries[idx].hashNext )
for ( int index = (int)itr.data->val; index != B3_NULL_INDEX; index = reg->entries.data[index].hashNext )
{
b3GeometryEntry* e = reg->entries + idx;
b3GeometryEntry* e = reg->entries.data + index;
if ( e->byteCount == byteCount && memcmp( e->bytes, bytes, (size_t)byteCount ) == 0 )
{
// Duplicate: the caller transferred ownership; return existing id
// Duplicate. Free bytes because the caller transferred ownership. Return existing id.
b3Free( bytes, (size_t)byteCount );
return e->id;
}
@ -873,22 +865,18 @@ uint32_t b3AppendGeometry( b3GeometryRegistry* reg, b3GeometryKind kind, uint64_
void b3FreeRegistry( b3GeometryRegistry* reg )
{
for ( int i = 0; i < reg->count; ++i )
for ( int i = 0; i < reg->entries.count; ++i )
{
b3Free( reg->entries[i].bytes, (size_t)reg->entries[i].byteCount );
}
if ( reg->entries != NULL )
{
b3Free( reg->entries, (size_t)( reg->capacity * (int)sizeof( b3GeometryEntry ) ) );
b3Free( reg->entries.data[i].bytes, (size_t)reg->entries.data[i].byteCount );
}
b3Array_Destroy( reg->entries );
if ( reg->dedupMap != NULL )
{
b3GeometryHashMap_cleanup( (b3GeometryHashMap*)reg->dedupMap );
b3Free( reg->dedupMap, sizeof( b3GeometryHashMap ) );
}
reg->entries = NULL;
reg->count = 0;
reg->capacity = 0;
reg->dedupMap = NULL;
}
@ -955,10 +943,10 @@ void b3RecInternTag( b3Recording* rec, uint64_t key, uint64_t id, const char* na
// the tag table stops after the geometry entries and ignores the trailing tag bytes.
void b3RecWriteRegistry( b3Recording* rec )
{
b3RecW_U32( &rec->buffer, (uint32_t)rec->registry.count );
for ( int i = 0; i < rec->registry.count; ++i )
b3RecW_U32( &rec->buffer, (uint32_t)rec->registry.entries.count );
for ( int i = 0; i < rec->registry.entries.count; ++i )
{
b3GeometryEntry* e = rec->registry.entries + i;
b3GeometryEntry* e = rec->registry.entries.data + i;
b3RecW_U8( &rec->buffer, (uint8_t)e->kind );
b3RecW_U32( &rec->buffer, (uint32_t)e->byteCount );
b3RecBufAppend( &rec->buffer, e->bytes, e->byteCount );
@ -977,7 +965,7 @@ void b3RecWriteRegistry( b3Recording* rec )
b3Recording* b3CreateRecording( int byteCapacity )
{
b3Recording* rec = (b3Recording*)b3Alloc( sizeof( b3Recording ) );
b3Recording* rec = b3Alloc( sizeof( b3Recording ) );
*rec = (b3Recording){ 0 };
int initCap = byteCapacity > 0 ? byteCapacity : 65536;
@ -1196,7 +1184,7 @@ b3Recording* b3LoadRecordingFromFile( const char* path )
uint32_t b3RecInternHull( b3Recording* rec, const b3HullData* hull )
{
int byteCount = hull->byteCount;
uint8_t* bytes = (uint8_t*)b3Alloc( (size_t)byteCount );
uint8_t* bytes = b3Alloc( (size_t)byteCount );
memcpy( bytes, hull, (size_t)byteCount );
uint64_t h = b3Hash64Blob( bytes, byteCount );
return b3InternGeometry( &rec->registry, b3_geometryHull, h, bytes, byteCount );
@ -1205,7 +1193,7 @@ uint32_t b3RecInternHull( b3Recording* rec, const b3HullData* hull )
uint32_t b3RecInternMesh( b3Recording* rec, const b3MeshData* mesh )
{
int byteCount = mesh->byteCount;
uint8_t* bytes = (uint8_t*)b3Alloc( (size_t)byteCount );
uint8_t* bytes = b3Alloc( (size_t)byteCount );
memcpy( bytes, mesh, (size_t)byteCount );
uint64_t h = b3Hash64Blob( bytes, byteCount );
return b3InternGeometry( &rec->registry, b3_geometryMesh, h, bytes, byteCount );
@ -1214,7 +1202,7 @@ uint32_t b3RecInternMesh( b3Recording* rec, const b3MeshData* mesh )
uint32_t b3RecInternHeightField( b3Recording* rec, const b3HeightFieldData* hf )
{
int byteCount = hf->byteCount;
uint8_t* bytes = (uint8_t*)b3Alloc( (size_t)byteCount );
uint8_t* bytes = b3Alloc( (size_t)byteCount );
memcpy( bytes, hf, (size_t)byteCount );
uint64_t h = b3Hash64Blob( bytes, byteCount );
return b3InternGeometry( &rec->registry, b3_geometryHeightField, h, bytes, byteCount );
@ -1223,7 +1211,7 @@ uint32_t b3RecInternHeightField( b3Recording* rec, const b3HeightFieldData* hf )
uint32_t b3RecInternCompound( b3Recording* rec, const b3CompoundData* compound )
{
int byteCount = compound->byteCount;
uint8_t* bytes = (uint8_t*)b3Alloc( (size_t)byteCount );
uint8_t* bytes = b3Alloc( (size_t)byteCount );
memcpy( bytes, compound, (size_t)byteCount );
// Null the tree node pointer in the copy so the canonical bytes are pointer-free.
// b3ConvertBytesToCompound fixes it back on load via nodeOffset.

View file

@ -3,6 +3,7 @@
#pragma once
#include "container.h"
#include "core.h"
#include "box3d/id.h"
@ -50,10 +51,10 @@ typedef struct b3World b3World;
#define B3_REC_VERSION_MAJOR 4
// Minor tracks op-stream additions that keep the 48 byte header shape.
// Minor version 3 added name cache.
#define B3_REC_VERSION_MINOR 3
// Minor version 4 added b3Shape_SetMeshMaterial, b3Shape_SetHull, b3Shape_SetMesh
#define B3_REC_VERSION_MINOR 4
// File header, fixed 48 bytes, little-endian. Contains the registry locator so the player
// File header, fixed 48 bytes. Contains the registry locator so the player
// can load geometry before replaying any ops.
typedef struct b3RecHeader
{
@ -106,13 +107,13 @@ typedef struct b3GeometryEntry
int hashNext;
} b3GeometryEntry;
b3DeclareArray( b3GeometryEntry );
// Growable array of geometry entries. Ids are array indices, so the array is serialized in order.
// dedupMap maps content hash to entry id for O(1) dedup; it is opaque here and owned by recording.c.
typedef struct b3GeometryRegistry
{
b3GeometryEntry* entries;
int count;
int capacity;
b3Array( b3GeometryEntry ) entries;
void* dedupMap;
} b3GeometryRegistry;
@ -126,7 +127,7 @@ typedef struct b3GeometryRegistry
typedef struct b3RecTag
{
// hash of (id, queryName)
uint64_t key;
uint64_t key;
uint64_t id;
char queryName[B3_MAX_QUERY_NAME_LENGTH + 1];
} b3RecTag;

View file

@ -91,9 +91,11 @@ B3_REC_OP( 0x57, ShapeEnablePreSolveEvents, RET_NONE, ARG( SHAPEID, shape ) ARG(
B3_REC_OP( 0x58, ShapeEnableHitEvents, RET_NONE, ARG( SHAPEID, shape ) ARG( BOOL, flag ) )
B3_REC_OP( 0x59, ShapeSetSphere, RET_NONE, ARG( SHAPEID, shape ) ARG( SPHERE, sphere ) )
B3_REC_OP( 0x5A, ShapeSetCapsule, RET_NONE, ARG( SHAPEID, shape ) ARG( CAPSULE, capsule ) )
B3_REC_OP( 0x5B, ShapeApplyWind, RET_NONE,
ARG( SHAPEID, shape ) ARG( VEC3, wind ) ARG( F32, drag ) ARG( F32, lift ) ARG( F32, maxSpeed ) ARG( BOOL, wake ) )
B3_REC_OP( 0x5B, ShapeApplyWind, RET_NONE, ARG( SHAPEID, shape ) ARG( VEC3, wind ) ARG( F32, drag ) ARG( F32, lift ) ARG( F32, maxSpeed ) ARG( BOOL, wake ) )
B3_REC_OP( 0x5C, ShapeSetName, RET_NONE, ARG( SHAPEID, shape ) ARG( STR, name ) )
B3_REC_OP( 0x5D, ShapeSetMeshMaterial, RET_NONE, ARG( SHAPEID, shape ) ARG( MATERIAL, material ) ARG( I32, index ) )
B3_REC_OP( 0x5E, ShapeSetHull, RET_NONE, ARG( SHAPEID, shape ) ARG( GEOMID, geometryId ) )
B3_REC_OP( 0x5F, ShapeSetMesh, RET_NONE, ARG( SHAPEID, shape ) ARG( GEOMID, geometryId ) ARG( VEC3, scale ) )
// Joint create and destroy
B3_REC_OP( 0x90, CreateParallelJoint, RET_JOINTID, ARG( WORLDID, world ) ARG( PARALLELJOINTDEF, def ) )

View file

@ -1024,7 +1024,7 @@ static void b3RecDispatch_CreateMeshShape( const b3RecArgs_CreateMeshShape* a, b
return;
}
b3RegistrySlot* slot = rdr->slots + id;
const b3MeshData* mesh = (const b3MeshData*)b3RecGetLiveMesh( slot );
const b3MeshData* mesh = b3RecGetLiveMesh( slot );
b3BodyId bodyId = b3RecMakeBodyId( rdr, a->body );
b3ShapeId gotId = b3CreateMeshShape( bodyId, &a->def, mesh, a->scale );
b3RecCheckShapeId( rdr, gotId, recId );
@ -1110,6 +1110,11 @@ static void b3RecDispatch_ShapeSetSurfaceMaterial( const b3RecArgs_ShapeSetSurfa
b3Shape_SetSurfaceMaterial( b3RecMakeShapeId( rdr, a->shape ), a->material );
}
static void b3RecDispatch_ShapeSetMeshMaterial( const b3RecArgs_ShapeSetMeshMaterial* a, b3RecReader* rdr )
{
b3Shape_SetMeshMaterial( b3RecMakeShapeId( rdr, a->shape ), a->material, a->index );
}
static void b3RecDispatch_ShapeSetFilter( const b3RecArgs_ShapeSetFilter* a, b3RecReader* rdr )
{
b3Shape_SetFilter( b3RecMakeShapeId( rdr, a->shape ), a->filter, a->invokeContacts );
@ -1145,6 +1150,35 @@ static void b3RecDispatch_ShapeSetCapsule( const b3RecArgs_ShapeSetCapsule* a, b
b3Shape_SetCapsule( b3RecMakeShapeId( rdr, a->shape ), &a->capsule );
}
static void b3RecDispatch_ShapeSetHull( const b3RecArgs_ShapeSetHull* a, b3RecReader* rdr )
{
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
{
printf( "b3ReplayFile: hull geometryId %u out of range\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
b3ShapeId shapeId = b3RecMakeShapeId( rdr, a->shape );
b3Shape_SetHull( shapeId, (const b3HullData*)slot->bytes );
}
static void b3RecDispatch_ShapeSetMesh( const b3RecArgs_ShapeSetMesh* a, b3RecReader* rdr )
{
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
{
printf( "b3ReplayFile: mesh geometryId %u out of range\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
b3ShapeId shapeId = b3RecMakeShapeId( rdr, a->shape );
const b3MeshData* mesh = b3RecGetLiveMesh( slot );
b3Shape_SetMesh( shapeId, mesh, a->scale );
}
static void b3RecDispatch_ShapeApplyWind( const b3RecArgs_ShapeApplyWind* a, b3RecReader* rdr )
{
b3Shape_ApplyWind( b3RecMakeShapeId( rdr, a->shape ), a->wind, a->drag, a->lift, a->maxSpeed, a->wake );
@ -2581,12 +2615,12 @@ static void b3RecCaptureKeyframe( b3RecPlayer* player )
b3World* world = b3GetWorldFromId( player->rdr.replayWorldId );
b3RecBuffer buf = { 0 };
int regCountBefore = player->keyframeRec->registry.count;
int regCountBefore = player->keyframeRec->registry.entries.count;
B3_UNUSED( regCountBefore );
b3SerializeWorld( world, &buf, player->keyframeRec );
// Registry must not grow: all geometry was pre-seeded and the registry dedups exactly.
B3_ASSERT( player->keyframeRec->registry.count == regCountBefore );
B3_ASSERT( player->keyframeRec->registry.entries.count == regCountBefore );
size_t bodyBytes = (size_t)player->bodyIdCount * sizeof( b3BodyId );
size_t newBytes = (size_t)buf.capacity + bodyBytes;

View file

@ -1295,6 +1295,8 @@ void b3Shape_SetMeshMaterial( b3ShapeId shapeId, b3SurfaceMaterial surfaceMateri
B3_ASSERT( 0 <= index && index < shape->materialCount );
B3_ASSERT( shape->type != b3_compoundShape );
B3_REC( world, ShapeSetMeshMaterial, shapeId, surfaceMaterial, index );
b3GetShapeMaterials( shape )[index] = surfaceMaterial;
}
@ -1613,6 +1615,14 @@ void b3Shape_SetHull( b3ShapeId shapeId, const b3HullData* hull )
return;
}
if ( world->recording != NULL )
{
// Intern the shared hull.
uint32_t geometryId = b3RecInternHull( world->recording, data );
b3RecArgs_ShapeSetHull setArgs = { shapeId, geometryId };
b3RecWrite_ShapeSetHull( world->recording, &setArgs );
}
b3DestroyShapeAllocationForShapeChange( world, shape );
shape->hull = data;
@ -1640,6 +1650,13 @@ void b3Shape_SetMesh( b3ShapeId shapeId, const b3MeshData* meshData, b3Vec3 scal
world->locked = true;
if ( world->recording != NULL )
{
uint32_t geometryId = b3RecInternMesh( world->recording, meshData );
b3RecArgs_ShapeSetMesh setArgs = { shapeId, geometryId, scale };
b3RecWrite_ShapeSetMesh( world->recording, &setArgs );
}
b3Shape* shape = b3GetShape( world, shapeId );
b3DestroyShapeAllocationForShapeChange( world, shape );

View file

@ -390,9 +390,9 @@ void b3CollideTriangleAndCapsule( b3LocalManifold* manifold, int capacity, const
distanceInput.useRadii = false;
b3DistanceOutput distanceOutput = b3ShapeDistance( &distanceInput, cache, NULL, 0 );
float speculativeDistance = B3_SPECULATIVE_DISTANCE;
float radius = capsuleB->radius;
if ( distanceOutput.distance > radius + B3_SPECULATIVE_DISTANCE )
if ( distanceOutput.distance > radius + speculativeDistance )
{
// Shapes are separated, persist the cache
return;
@ -468,32 +468,32 @@ void b3CollideTriangleAndCapsule( b3LocalManifold* manifold, int capacity, const
b3SeparatingAxis faceQuery = b3QueryTriangleFaceAndCapsule( plane, capsuleB );
if ( faceQuery.separation > radius )
{
// Shapes are separated
// Shapes are separated. Should be impossible for a reasonable capsule radius.
return;
}
b3SeparatingAxis edgeQuery = b3QueryTriangleAndCapsuleEdges( triangleA, plane, capsuleB );
if ( edgeQuery.separation > radius )
{
// Shapes are separated
// Shapes are separated. Should be impossible for a reasonable capsule radius.
return;
}
// Create face contact
float faceSeparation = faceQuery.separation - radius;
b3BuildTriangleAndCapsuleFaceContact( manifold, triangleA, plane, capsuleB );
B3_VALIDATE( manifold->pointCount == 0 || manifold->pointCount == 2 );
if ( manifold->pointCount == 2 )
{
// This becomes the clipped separation.
faceSeparation = b3MinFloat( manifold->points[0].separation, manifold->points[1].separation );
}
B3_VALIDATE( faceSeparation <= 0.0f );
// Face contact can be empty if it does not realize the axis of minimum penetration.
// Create edge contact if face contact fails or edge contact is significantly better!
const float kRelEdgeTolerance = 0.50f;
const float kAbsTolerance = 1.0f * B3_LINEAR_SLOP;
float linearSlop = B3_LINEAR_SLOP;
float edgeSeparation = edgeQuery.separation - radius;
if ( manifold->pointCount == 0 || edgeSeparation > kRelEdgeTolerance * faceSeparation + kAbsTolerance )
if ( manifold->pointCount == 0 || edgeSeparation > faceSeparation + linearSlop )
{
// Edge contact
b3BuildTriangleAndCapsuleEdgeContact( manifold, triangleA, plane, capsuleB, edgeQuery );