Fixing issues (#94)

- option to draw kinematic shapes with transparency
- slight change to triangle vs hull to reduce ghost collisions
- changed return of b3DebugDraw.DrawShapeFcn to void
#66 
#48 
#69 
#75 
#76
This commit is contained in:
Erin Catto 2026-07-19 21:43:43 -07:00 • committed by GitHub
commit dfa5e6add0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 906 additions and 433 deletions

View file

@ -169,8 +169,11 @@ if (BOX3D_DOUBLE_PRECISION)
target_compile_definitions(box3d PUBLIC BOX3D_DOUBLE_PRECISION)
endif()
# Enable asserts in release with debug info
target_compile_definitions(box3d PUBLIC "$<$<CONFIG:RELWITHDEBINFO>:B3_ENABLE_ASSERT>")
if (MSVC)
message(STATUS "Box3D on MSVC")
message(STATUS "Box3D on MSVC")
if (BUILD_SHARED_LIBS)
# this is needed by DLL users to import Box3D symbols
target_compile_definitions(box3d INTERFACE BOX3D_DLL)
@ -179,9 +182,6 @@ if (MSVC)
# Visual Studio won't load the natvis unless it is in the project
target_sources(box3d PRIVATE box3d.natvis)
# Enable asserts in release with debug info
target_compile_definitions(box3d PUBLIC "$<$<CONFIG:RELWITHDEBINFO>:B3_ENABLE_ASSERT>")
# Warnings Wall is problematic in mixed C/C++, so using W4
target_compile_options(box3d PRIVATE /W4)
# target_compile_options(box3d PRIVATE /wd4820 /wd5045 /wd4061 /wd4711 /wd4514 /wd4365 /wd5219 /wd5039)

View file

@ -226,13 +226,19 @@ static bool b3CompareMeshes( const b3MeshData* mesh1, const b3MeshData* mesh2 )
#define FREE_FN b3Free
#include "verstable.h"
_Static_assert( sizeof( b3SurfaceMaterial ) == 40, "review padding" );
static inline uint64_t b3HashMaterial( const b3SurfaceMaterial* material )
{
B3_ASSERT( material->padding == 0 );
return vt_wyhash( material, sizeof( b3SurfaceMaterial ) );
}
static bool b3CompareMaterials( const b3SurfaceMaterial* mat1, const b3SurfaceMaterial* mat2 )
{
B3_ASSERT( mat1->padding == 0 && mat2->padding == 0);
if ( mat1 == mat2 )
{
return true;

View file

@ -745,6 +745,16 @@ bool b3UpdateContact( b3World* world, int workerIndex, b3Contact* contact, b3Sha
childShapeA.type = child.type;
// Handle child material for non-meshes.
if ( child.type != b3_meshShape )
{
B3_ASSERT( 0 <= child.materialIndices[0] && child.materialIndices[0] < shapeA->materialCount );
const b3SurfaceMaterial* parentMaterials = b3GetShapeMaterials( shapeA );
childShapeA.material = parentMaterials[child.materialIndices[0]];
childShapeA.materials = NULL;
childShapeA.materialCount = 1;
}
if ( child.type == b3_capsuleShape )
{
childShapeA.capsule = child.capsule;

View file

@ -964,6 +964,8 @@ typedef struct b3ContactConstraintWide
int indexA[B3_SIMD_WIDTH];
int indexB[B3_SIMD_WIDTH];
int pointCounts[B3_SIMD_WIDTH];
b3FloatW invMassA, invMassB;
b3SymMatrix3W invIA, invIB;
b3Vec3W normal;
@ -1403,6 +1405,8 @@ void b3PrepareContacts_Convex( b3SolverBlock block, b3StepContext* context )
( (float*)&constraint->impulseScale )[lane] = soft.impulseScale;
int pointCount = manifold->pointCount;
constraint->pointCounts[lane] = pointCount;
b3Vec3 centerA = b3Vec3_zero;
b3Vec3 centerB = b3Vec3_zero;
float totalFrictionWeight = 0.0f;
@ -1550,8 +1554,14 @@ void b3WarmStartContacts_Convex( b3SolverBlock block, b3StepContext* context )
b3BodyStateW bA = b3GatherBodies( states, c->indexA );
b3BodyStateW bB = b3GatherBodies( states, c->indexB );
_Static_assert( B3_SIMD_WIDTH == 4, "width" );
int pointCount1 = b3MaxInt( c->pointCounts[0], c->pointCounts[1] );
int pointCount2 = b3MaxInt( c->pointCounts[2], c->pointCounts[3] );
int pointCount = b3MaxInt( pointCount1, pointCount2 );
B3_VALIDATE( 0 < pointCount && pointCount <= B3_SIMD_WIDTH );
// Normal impulses
for ( int j = 0; j < B3_MAX_MANIFOLD_POINTS; ++j )
for ( int j = 0; j < pointCount; ++j )
{
b3ContactConstraintPointWide* cp = c->points + j;
@ -1618,6 +1628,12 @@ void b3SolveContacts_Convex( b3SolverBlock block, b3StepContext* context, bool u
{
b3ContactConstraintWide* c = constraints + wideIndex;
_Static_assert( B3_SIMD_WIDTH == 4, "width" );
int pointCount1 = b3MaxInt( c->pointCounts[0], c->pointCounts[1] );
int pointCount2 = b3MaxInt( c->pointCounts[2], c->pointCounts[3] );
int pointCount = b3MaxInt( pointCount1, pointCount2 );
B3_VALIDATE( 0 < pointCount && pointCount <= B3_SIMD_WIDTH );
b3BodyStateW bA = b3GatherBodies( states, c->indexA );
b3BodyStateW bB = b3GatherBodies( states, c->indexB );
@ -1640,8 +1656,7 @@ void b3SolveContacts_Convex( b3SolverBlock block, b3StepContext* context, bool u
b3FloatW totalNormalImpulse = b3ZeroW();
b3FloatW totalTwistLimit = b3ZeroW();
// todo_erin use the max point count of the four manifolds
for ( int pointIndex = 0; pointIndex < B3_MAX_MANIFOLD_POINTS; ++pointIndex )
for ( int pointIndex = 0; pointIndex < pointCount; ++pointIndex )
{
b3ContactConstraintPointWide* cp = c->points + pointIndex;
@ -1828,6 +1843,12 @@ void b3ApplyRestitution_Convex( b3SolverBlock block, b3StepContext* context )
continue;
}
_Static_assert( B3_SIMD_WIDTH == 4, "width" );
int pointCount1 = b3MaxInt( c->pointCounts[0], c->pointCounts[1] );
int pointCount2 = b3MaxInt( c->pointCounts[2], c->pointCounts[3] );
int pointCount = b3MaxInt( pointCount1, pointCount2 );
B3_VALIDATE( 0 < pointCount && pointCount <= B3_SIMD_WIDTH );
// Single gather for all manifolds
b3BodyStateW bA = b3GatherBodies( states, c->indexA );
b3BodyStateW bB = b3GatherBodies( states, c->indexB );
@ -1836,7 +1857,7 @@ void b3ApplyRestitution_Convex( b3SolverBlock block, b3StepContext* context )
// by the calculations below.
b3FloatW restitutionMask = b3EqualsW( c->restitution, zero );
for ( int pointIndex = 0; pointIndex < B3_MAX_MANIFOLD_POINTS; ++pointIndex )
for ( int pointIndex = 0; pointIndex < pointCount; ++pointIndex )
{
b3ContactConstraintPointWide* cp = c->points + pointIndex;

View file

@ -1523,7 +1523,7 @@ void b3World_Draw( b3WorldId worldId, b3DebugDraw* draw, uint64_t maskBits )
b3Vec3 normal = manifold->normal;
// Average the anchors not the world points so the friction center stays exact far from the origin
b3Pos contactCenter = draw->drawAnchorA == 1 ? bodySimA->center : bodySimB->center;
b3Pos contactCenter = draw->drawAnchorA ? bodySimA->center : bodySimB->center;
b3Vec3 frictionAnchor = b3Vec3_zero;
float totalWeight = 0.0f;
float invTau = 1.0f / B3_SPECULATIVE_DISTANCE;
@ -1535,7 +1535,7 @@ void b3World_Draw( b3WorldId worldId, b3DebugDraw* draw, uint64_t maskBits )
char buffer[32];
b3Vec3 anchor = draw->drawAnchorA == 1 ? mp->anchorA : mp->anchorB;
b3Vec3 anchor = draw->drawAnchorA ? mp->anchorA : mp->anchorB;
b3Pos p = b3OffsetPos( contactCenter, anchor );
// See similar friction anchor weights in b3PrepareContacts_Mesh.

View file

@ -1233,10 +1233,9 @@ void b3CollideHullAndTriangle( b3LocalManifold* manifold, int capacity, const b3
// Don't admit a hull face significantly opposed to the triangle face.
// Need a tolerance to avoid ghost collisions.
// todo hull query skips faces that point along the triangle normal
b3Vec3 hullNormal = hullPlanes[faceQueryB.faceIndex].normal;
bool pushingDown = b3Dot( hullNormal, trianglePlane.normal ) > 0.25f;
if ( faceQueryB.separation > faceQueryA.separation + linearSlop && pushingDown == false )
if ( faceQueryB.separation >= faceQueryA.separation && pushingDown == false )
{
clippedFaceSeparation = b3CollideHullFace( manifold, capacity, &triangle, hullA, faceQueryB, cache, enableSpeculative );
}

View file

@ -80,10 +80,9 @@ b3ShapeDef b3DefaultShapeDef( void )
return def;
}
static bool b3EmptyDrawShape( void* userShape, b3WorldTransform transform, b3HexColor color, void* context )
static void b3EmptyDrawShape( void* userShape, b3WorldTransform transform, b3HexColor color, void* context )
{
B3_UNUSED( userShape, transform, color, context );
return false;
}
static void b3EmptyDrawSegment( b3Pos p1, b3Pos p2, b3HexColor color, void* context )