Fixes (#157)
- Added b3BodyDef::safetyFactor for CCD tuning - #155 - #153 - #148 - #149
This commit is contained in:
parent
47d7f7cc7e
commit
954cf879e7
46 changed files with 1944 additions and 318 deletions
51
src/body.c
51
src/body.c
|
|
@ -161,6 +161,7 @@ b3BodyId b3CreateBody( b3WorldId worldId, const b3BodyDef* def )
|
|||
B3_ASSERT( b3IsValidFloat( def->linearDamping ) && def->linearDamping >= 0.0f );
|
||||
B3_ASSERT( b3IsValidFloat( def->angularDamping ) && def->angularDamping >= 0.0f );
|
||||
B3_ASSERT( b3IsValidFloat( def->sleepThreshold ) && def->sleepThreshold >= 0.0f );
|
||||
B3_ASSERT( b3IsValidFloat( def->safetyFactor ) && def->safetyFactor >= 0.0f );
|
||||
B3_ASSERT( b3IsValidFloat( def->gravityScale ) );
|
||||
|
||||
b3World* world = b3GetUnlockedWorldFromId( worldId );
|
||||
|
|
@ -283,6 +284,7 @@ b3BodyId b3CreateBody( b3WorldId worldId, const b3BodyDef* def )
|
|||
body->sleepThreshold = def->sleepThreshold;
|
||||
body->sleepTime = 0.0f;
|
||||
body->sleepVelocity = 0.0f;
|
||||
body->safetyFactor = def->safetyFactor;
|
||||
body->mass = 0.0f;
|
||||
body->inertia = b3Mat3_zero;
|
||||
body->nameId = b3AddName( &world->names, def->name );
|
||||
|
|
@ -1000,6 +1002,19 @@ void b3UpdateBodyMassData( b3World* world, b3Body* body )
|
|||
shapeId = s->nextShapeId;
|
||||
}
|
||||
|
||||
// When the center of mass changes, any cached contact manifold becomes invalid.
|
||||
int edgeKey = body->headContactKey;
|
||||
while ( edgeKey != B3_NULL_INDEX )
|
||||
{
|
||||
int contactId = edgeKey >> 1;
|
||||
int edgeIndex = edgeKey & 1;
|
||||
|
||||
b3Contact* contact = b3Array_Get( world->contacts, contactId );
|
||||
contact->flags &= ~b3_relativeTransformValid;
|
||||
|
||||
edgeKey = contact->edges[edgeIndex].nextKey;
|
||||
}
|
||||
|
||||
// Apply fixed rotation
|
||||
if ( ( bodySim->flags & b3_fixedRotation ) == b3_fixedRotation )
|
||||
{
|
||||
|
|
@ -1890,6 +1905,19 @@ void b3Body_SetMassData( b3BodyId bodyId, b3MassData massData )
|
|||
bodySim->maxExtent = b3Max( bodySim->maxExtent, extent.maxExtent );
|
||||
shapeId = s->nextShapeId;
|
||||
}
|
||||
|
||||
// When the center of mass changes, any cached contact manifold becomes invalid.
|
||||
int edgeKey = body->headContactKey;
|
||||
while ( edgeKey != B3_NULL_INDEX )
|
||||
{
|
||||
int contactId = edgeKey >> 1;
|
||||
int edgeIndex = edgeKey & 1;
|
||||
|
||||
b3Contact* contact = b3Array_Get( world->contacts, contactId );
|
||||
contact->flags &= ~b3_relativeTransformValid;
|
||||
|
||||
edgeKey = contact->edges[edgeIndex].nextKey;
|
||||
}
|
||||
}
|
||||
|
||||
b3MassData b3Body_GetMassData( b3BodyId bodyId )
|
||||
|
|
@ -2063,6 +2091,29 @@ float b3Body_GetSleepThreshold( b3BodyId bodyId )
|
|||
return body->sleepThreshold;
|
||||
}
|
||||
|
||||
void b3Body_SetSafetyFactor( b3BodyId bodyId, float safetyFactor )
|
||||
{
|
||||
B3_ASSERT( b3IsValidFloat( safetyFactor ) && safetyFactor >= 0.0f );
|
||||
|
||||
b3World* world = b3GetUnlockedWorld( bodyId.world0 );
|
||||
if ( world == NULL )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
B3_REC( world, BodySetSafetyFactor, bodyId, safetyFactor );
|
||||
|
||||
b3Body* body = b3GetBodyFullId( world, bodyId );
|
||||
body->safetyFactor = safetyFactor;
|
||||
}
|
||||
|
||||
float b3Body_GetSafetyFactor( b3BodyId bodyId )
|
||||
{
|
||||
b3World* world = b3GetWorld( bodyId.world0 );
|
||||
b3Body* body = b3GetBodyFullId( world, bodyId );
|
||||
return body->safetyFactor;
|
||||
}
|
||||
|
||||
void b3Body_EnableSleep( b3BodyId bodyId, bool enableSleep )
|
||||
{
|
||||
b3World* world = b3GetUnlockedWorld( bodyId.world0 );
|
||||
|
|
|
|||
|
|
@ -108,6 +108,7 @@ typedef struct b3Body
|
|||
float sleepThreshold;
|
||||
float sleepTime;
|
||||
float sleepVelocity;
|
||||
float safetyFactor;
|
||||
float mass;
|
||||
|
||||
// local space inertia
|
||||
|
|
|
|||
|
|
@ -395,6 +395,23 @@ static void b3UpdateTreesTask( void* context )
|
|||
b3TracyCZoneEnd( tree_task );
|
||||
}
|
||||
|
||||
// Task that can be done in parallel with the narrow-phase
|
||||
// - rebuild the collision tree for dynamic and kinematic bodies to keep their query performance good
|
||||
static void b3EnqueueTreeUpdate( b3World* world )
|
||||
{
|
||||
if ( world->taskCount < B3_MAX_TASKS )
|
||||
{
|
||||
world->userTreeTask = world->enqueueTaskFcn( &b3UpdateTreesTask, world, world->userTaskContext, "rebuild tree" );
|
||||
world->taskCount += 1;
|
||||
world->activeTaskCount += world->userTreeTask == NULL ? 0 : 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
world->userTreeTask = NULL;
|
||||
b3UpdateTreesTask( world );
|
||||
}
|
||||
}
|
||||
|
||||
void b3UpdateBroadPhasePairs( b3World* world )
|
||||
{
|
||||
b3BroadPhase* bp = &world->broadPhase;
|
||||
|
|
@ -403,6 +420,8 @@ void b3UpdateBroadPhasePairs( b3World* world )
|
|||
|
||||
if ( moveCount == 0 )
|
||||
{
|
||||
// A destroyed shape may lead to no moves, but the tree could still be enlarged.
|
||||
b3EnqueueTreeUpdate( world );
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -427,19 +446,8 @@ void b3UpdateBroadPhasePairs( b3World* world )
|
|||
|
||||
b3TracyCZoneNC( create_contacts, "Create Contacts", b3_colorCoral, true );
|
||||
|
||||
// Task that can be done in parallel with the narrow-phase
|
||||
// - rebuild the collision tree for dynamic and kinematic bodies to keep their query performance good
|
||||
if ( world->taskCount < B3_MAX_TASKS )
|
||||
{
|
||||
world->userTreeTask = world->enqueueTaskFcn( &b3UpdateTreesTask, world, world->userTaskContext, "rebuild tree" );
|
||||
world->taskCount += 1;
|
||||
world->activeTaskCount += world->userTreeTask == NULL ? 0 : 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
world->userTreeTask = NULL;
|
||||
b3UpdateTreesTask( world );
|
||||
}
|
||||
// Update stale trees.
|
||||
b3EnqueueTreeUpdate( world );
|
||||
|
||||
// Single-threaded work
|
||||
// - Clear move flags
|
||||
|
|
|
|||
|
|
@ -237,7 +237,7 @@ static inline uint64_t b3HashMaterial( const b3SurfaceMaterial* material )
|
|||
|
||||
static bool b3CompareMaterials( const b3SurfaceMaterial* mat1, const b3SurfaceMaterial* mat2 )
|
||||
{
|
||||
B3_ASSERT( mat1->padding == 0 && mat2->padding == 0);
|
||||
B3_ASSERT( mat1->padding == 0 && mat2->padding == 0 );
|
||||
|
||||
if ( mat1 == mat2 )
|
||||
{
|
||||
|
|
@ -684,8 +684,7 @@ b3AABB b3ComputeCompoundAABB( const b3CompoundData* shape, b3Transform transform
|
|||
struct b3CompoundOverlapContext
|
||||
{
|
||||
const b3CompoundData* compound;
|
||||
// transform of the compound
|
||||
b3Transform transform;
|
||||
// proxy in the compound frame
|
||||
b3ShapeProxy proxy;
|
||||
bool overlap;
|
||||
};
|
||||
|
|
@ -698,25 +697,23 @@ static bool b3CompoundOverlapCallback( int proxyId, uint64_t userData, void* con
|
|||
struct b3CompoundOverlapContext* overlapContext = context;
|
||||
b3ChildShape child = b3GetCompoundChild( overlapContext->compound, childIndex );
|
||||
|
||||
b3Transform transform = b3MulTransforms( overlapContext->transform, child.transform );
|
||||
|
||||
bool overlap = false;
|
||||
switch ( child.type )
|
||||
{
|
||||
case b3_capsuleShape:
|
||||
overlap = b3OverlapCapsule( &child.capsule, transform, &overlapContext->proxy );
|
||||
overlap = b3OverlapCapsule( &child.capsule, child.transform, &overlapContext->proxy );
|
||||
break;
|
||||
|
||||
case b3_hullShape:
|
||||
overlap = b3OverlapHull( child.hull, transform, &overlapContext->proxy );
|
||||
overlap = b3OverlapHull( child.hull, child.transform, &overlapContext->proxy );
|
||||
break;
|
||||
|
||||
case b3_meshShape:
|
||||
overlap = b3OverlapMesh( &child.mesh, transform, &overlapContext->proxy );
|
||||
overlap = b3OverlapMesh( &child.mesh, child.transform, &overlapContext->proxy );
|
||||
break;
|
||||
|
||||
case b3_sphereShape:
|
||||
overlap = b3OverlapSphere( &child.sphere, transform, &overlapContext->proxy );
|
||||
overlap = b3OverlapSphere( &child.sphere, child.transform, &overlapContext->proxy );
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
@ -735,26 +732,24 @@ static bool b3CompoundOverlapCallback( int proxyId, uint64_t userData, void* con
|
|||
return true;
|
||||
}
|
||||
|
||||
// This is dealing with multiple transforms:
|
||||
// - the compound shape transform
|
||||
// - the compound child shape transforms
|
||||
bool b3OverlapCompound( const b3CompoundData* shape, b3Transform shapeTransform, const b3ShapeProxy* proxy )
|
||||
{
|
||||
B3_ASSERT( 0 < proxy->count && proxy->count <= B3_MAX_SHAPE_CAST_POINTS );
|
||||
|
||||
// Use local proxy.
|
||||
b3Vec3 buffer[B3_MAX_SHAPE_CAST_POINTS];
|
||||
struct b3CompoundOverlapContext context = {
|
||||
.compound = shape,
|
||||
.transform = shapeTransform,
|
||||
.proxy = *proxy,
|
||||
.proxy = b3MakeLocalProxy( proxy, shapeTransform, buffer ),
|
||||
.overlap = false,
|
||||
};
|
||||
|
||||
b3AABB aabb = { proxy->points[0], proxy->points[0] };
|
||||
for ( int i = 1; i < proxy->count; ++i )
|
||||
{
|
||||
aabb.lowerBound = b3Min( aabb.lowerBound, proxy->points[i] );
|
||||
aabb.upperBound = b3Max( aabb.upperBound, proxy->points[i] );
|
||||
}
|
||||
|
||||
b3Vec3 r = { proxy->radius, proxy->radius, proxy->radius };
|
||||
aabb.lowerBound = b3Sub( aabb.lowerBound, r );
|
||||
aabb.upperBound = b3Add( aabb.upperBound, r );
|
||||
b3AABB aabb = b3ComputeProxyAABB( &context.proxy );
|
||||
|
||||
// This query must be in the compound frame.
|
||||
(void)b3DynamicTree_Query( &shape->tree, aabb, ~0ull, false, b3CompoundOverlapCallback, &context );
|
||||
|
||||
return context.overlap;
|
||||
|
|
@ -779,6 +774,7 @@ static float b3CompoundRayCastCallback( const b3RayCastInput* input, int proxyId
|
|||
|
||||
b3ChildShape child = b3GetCompoundChild( compound, childIndex );
|
||||
|
||||
// Get the input in child local space.
|
||||
b3RayCastInput localInput = *input;
|
||||
localInput.origin = b3InvTransformPoint( child.transform, input->origin );
|
||||
localInput.translation = b3InvRotateVector( child.transform.q, input->translation );
|
||||
|
|
@ -852,23 +848,13 @@ static float b3CompoundShapeCastCallback( const b3BoxCastInput* input, int proxy
|
|||
|
||||
b3ChildShape child = b3GetCompoundChild( compound, childIndex );
|
||||
|
||||
// Rebuild from the carried shape cast input, taking only the advancing fraction from the tree
|
||||
b3ShapeCastInput localInput = *shapeInput;
|
||||
localInput.maxFraction = input->maxFraction;
|
||||
// Get the input in the child local space.
|
||||
b3ShapeCastInput localInput = { 0 };
|
||||
b3Vec3 localPoints[B3_MAX_SHAPE_CAST_POINTS];
|
||||
|
||||
localInput.proxy.count = b3MinInt( shapeInput->proxy.count, B3_MAX_SHAPE_CAST_POINTS );
|
||||
|
||||
b3Transform invTransform = b3InvertTransform( child.transform );
|
||||
b3Matrix3 R = b3MakeMatrixFromQuat( invTransform.q );
|
||||
|
||||
for ( int i = 0; i < localInput.proxy.count; ++i )
|
||||
{
|
||||
localPoints[i] = b3Add( b3MulMV( R, shapeInput->proxy.points[i] ), invTransform.p );
|
||||
}
|
||||
|
||||
localInput.proxy.points = localPoints;
|
||||
localInput.translation = b3MulMV( R, shapeInput->translation );
|
||||
localInput.proxy = b3MakeLocalProxy( &shapeInput->proxy, child.transform, localPoints );
|
||||
localInput.translation = b3InvRotateVector( child.transform.q, shapeInput->translation );
|
||||
localInput.maxFraction = input->maxFraction;
|
||||
localInput.canEncroach = shapeInput->canEncroach;
|
||||
|
||||
b3CastOutput output = { 0 };
|
||||
|
||||
|
|
|
|||
|
|
@ -485,7 +485,7 @@ static bool b3ComputeConvexManifold( b3World* world, int workerIndex, b3Contact*
|
|||
|
||||
b3ContactCache* cache = &contact->convexContact.cache;
|
||||
|
||||
int pointCapacity = 32;
|
||||
int pointCapacity = B3_MAX_MANIFOLD_POINTS;
|
||||
b3LocalManifoldPoint* pointBuffer = (b3LocalManifoldPoint*)b3Bump( &arena, pointCapacity * sizeof( b3LocalManifoldPoint ) );
|
||||
|
||||
b3LocalManifold geomManifold = { 0 };
|
||||
|
|
@ -560,6 +560,7 @@ static bool b3ComputeConvexManifold( b3World* world, int workerIndex, b3Contact*
|
|||
}
|
||||
|
||||
b3Manifold* manifold = contact->manifolds;
|
||||
B3_ASSERT( 0 < geomManifold.pointCount && geomManifold.pointCount <= B3_MAX_MANIFOLD_POINTS );
|
||||
manifold->pointCount = geomManifold.pointCount;
|
||||
|
||||
b3Matrix3 matrixA = b3MakeMatrixFromQuat( xfA.q );
|
||||
|
|
|
|||
|
|
@ -133,6 +133,7 @@ typedef struct b3Contact
|
|||
// b3ContactFlags
|
||||
uint32_t flags;
|
||||
|
||||
// todo embed single manifold with ability to allocate overfloat, use union.
|
||||
b3Manifold* manifolds;
|
||||
int manifoldCount;
|
||||
|
||||
|
|
|
|||
|
|
@ -630,7 +630,7 @@ void b3ApplyRestitution_Mesh( b3SolverBlock block, b3StepContext* context )
|
|||
|
||||
b3Vec3 normal = cm->normal;
|
||||
int pointCount = cm->pointCount;
|
||||
B3_ASSERT( 0 < pointCount && pointCount <= B3_MAX_MANIFOLD_POINTS );
|
||||
B3_VALIDATE( 0 < pointCount && pointCount <= B3_MAX_MANIFOLD_POINTS );
|
||||
|
||||
for ( int pointIndex = 0; pointIndex < pointCount; ++pointIndex )
|
||||
{
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ typedef struct b3ManifoldConstraintPoint
|
|||
|
||||
typedef struct b3ManifoldConstraint
|
||||
{
|
||||
b3ManifoldConstraintPoint points[4];
|
||||
b3ManifoldConstraintPoint points[B3_MAX_MANIFOLD_POINTS];
|
||||
int pointCount;
|
||||
b3Vec3 normal;
|
||||
b3Vec3 tangent1;
|
||||
|
|
|
|||
|
|
@ -203,6 +203,10 @@ static b3SeparatingAxis b3QueryEdgeDirectionHullAndCapsule( const b3HullData* hu
|
|||
};
|
||||
}
|
||||
|
||||
_Static_assert( B3_MAX_MANIFOLD_POINTS >= 4, "must be 4 or more" );
|
||||
|
||||
#if B3_MAX_MANIFOLD_POINTS == 4
|
||||
|
||||
// Reduce the manifold points to a maximum of 4 points.
|
||||
// Note: this modifies the input point array to improve performance
|
||||
static void b3ReduceManifoldPoints( b3LocalManifold* manifold, int capacity, b3LocalManifoldPoint* points, int count )
|
||||
|
|
@ -367,6 +371,61 @@ static void b3ReduceManifoldPoints( b3LocalManifold* manifold, int capacity, b3L
|
|||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static void b3ReduceManifoldPoints( b3LocalManifold* manifold, int capacity, b3LocalManifoldPoint* points, int count )
|
||||
{
|
||||
if ( capacity < 4 )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
B3_ASSERT( count <= B3_MAX_CLIP_POINTS );
|
||||
|
||||
int target = b3MinInt( capacity, B3_MAX_MANIFOLD_POINTS );
|
||||
|
||||
if ( count <= target )
|
||||
{
|
||||
for ( int i = 0; i < count; ++i )
|
||||
{
|
||||
manifold->points[i] = points[i];
|
||||
}
|
||||
|
||||
manifold->pointCount = count;
|
||||
return;
|
||||
}
|
||||
|
||||
b3Vec3 normal = manifold->normal;
|
||||
b3Vec3 u = b3Perp( normal );
|
||||
b3Vec3 v = b3Cross( normal, u );
|
||||
b3Vec3 origin = points[0].point;
|
||||
|
||||
b3Point2D pts[B3_MAX_CLIP_POINTS];
|
||||
for ( int i = 0; i < count; ++i )
|
||||
{
|
||||
b3Vec3 d = b3Sub( points[i].point, origin );
|
||||
pts[i].p = (b3Vec2){ b3Dot( d, u ), b3Dot( d, v ) };
|
||||
pts[i].separation = points[i].separation;
|
||||
pts[i].originalIndex = i;
|
||||
}
|
||||
|
||||
b3Point2D hull[2 * B3_MAX_CLIP_POINTS];
|
||||
int hullCount = b3Hull2D( pts, count, hull );
|
||||
int finalCount = b3SimplifyHull2D( hull, hullCount, target );
|
||||
B3_ASSERT( 0 < finalCount && finalCount <= target );
|
||||
|
||||
for ( int i = 0; i < finalCount; ++i )
|
||||
{
|
||||
int index = hull[i].originalIndex;
|
||||
B3_ASSERT( 0 <= index && index < count );
|
||||
manifold->points[i] = points[index];
|
||||
}
|
||||
|
||||
manifold->pointCount = finalCount;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void b3CollideSpheres( b3LocalManifold* manifold, int capacity, const b3Sphere* sphereA, const b3Sphere* sphereB,
|
||||
b3Transform transformBtoA )
|
||||
{
|
||||
|
|
@ -984,6 +1043,7 @@ static int b3BuildPolygon( b3ClipVertex* out, b3Transform transform, const b3Hul
|
|||
static bool b3BuildFaceAContact( b3LocalManifold* manifold, int capacity, const b3HullData* hullA, const b3HullData* hullB,
|
||||
b3Transform transformBtoA, b3SeparatingAxis query, b3SATCache* cache )
|
||||
{
|
||||
B3_ASSERT( capacity > 0 );
|
||||
B3_VALIDATE( query.type == b3_faceAxisA );
|
||||
B3_VALIDATE( 0 <= query.indexA && query.indexA < hullA->faceCount );
|
||||
B3_VALIDATE( 0 <= query.indexB && query.indexB < hullB->vertexCount );
|
||||
|
|
|
|||
|
|
@ -36,6 +36,8 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
_Static_assert( B3_MAX_MANIFOLD_POINTS >= 4, "B3_MAX_MANIFOLD_POINTS must be at least 4" );
|
||||
|
||||
// This allows the user to change the length units at runtime
|
||||
static float b3_lengthUnitsPerMeter = 1.0f;
|
||||
|
||||
|
|
@ -128,6 +130,11 @@ bool b3IsDoublePrecision( void )
|
|||
#endif
|
||||
}
|
||||
|
||||
int b3GetMaxManifoldPoints( void )
|
||||
{
|
||||
return B3_MAX_MANIFOLD_POINTS;
|
||||
}
|
||||
|
||||
static b3AllocFcn* b3_allocFcn = NULL;
|
||||
static b3FreeFcn* b3_freeFcn = NULL;
|
||||
|
||||
|
|
|
|||
183
src/hull.c
183
src/hull.c
|
|
@ -2945,3 +2945,186 @@ b3BoxHull b3MakeScaledBoxHull( b3Vec3 halfWidths, b3Transform transform, b3Vec3
|
|||
b3ScaleBox( &h, &xf, postScale, 4.0f * B3_LINEAR_SLOP );
|
||||
return b3MakeTransformedBoxHull( h.x, h.y, h.z, xf );
|
||||
}
|
||||
|
||||
// 2D hull (Andrew's monotone chain)
|
||||
|
||||
// cross(b - a, c - a)
|
||||
static inline float b3Cross2D( b3Vec2 a, b3Vec2 b, b3Vec2 c )
|
||||
{
|
||||
return ( b.x - a.x ) * ( c.y - a.y ) - ( b.y - a.y ) * ( c.x - a.x );
|
||||
}
|
||||
|
||||
// Iteratively remove the vertices contributing the least area.
|
||||
int b3SimplifyHull2D( b3Point2D* hull, int count1, int target )
|
||||
{
|
||||
B3_ASSERT( target >= 3 );
|
||||
|
||||
if ( count1 <= 3 )
|
||||
{
|
||||
return count1;
|
||||
}
|
||||
|
||||
float linearSlop = B3_LINEAR_SLOP;
|
||||
float areaTol = 0.25f * linearSlop * linearSlop;
|
||||
int count2 = count1;
|
||||
|
||||
for ( ;; )
|
||||
{
|
||||
float minArea = FLT_MAX;
|
||||
int minIndex = 0;
|
||||
|
||||
for ( int i = 0; i < count2; i++ )
|
||||
{
|
||||
int prev = ( i + count2 - 1 ) % count2;
|
||||
int next = ( i + 1 ) % count2;
|
||||
float area = b3Cross2D( hull[prev].p, hull[i].p, hull[next].p );
|
||||
B3_VALIDATE( area >= -FLT_EPSILON );
|
||||
|
||||
if ( area + areaTol < minArea )
|
||||
{
|
||||
minArea = area;
|
||||
minIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
if ( count2 <= 3 || ( minArea > areaTol && count2 <= target ) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
if ( minIndex < count2 )
|
||||
{
|
||||
memmove( hull + minIndex, hull + ( minIndex + 1 ), ( count2 - minIndex ) * sizeof( b3Point2D ) );
|
||||
}
|
||||
}
|
||||
|
||||
return count2;
|
||||
}
|
||||
|
||||
// Sort comparator: by x, then by y. Insertion since n is typically 20 or less.
|
||||
static void b3Sort2D( b3Point2D* pts, int count )
|
||||
{
|
||||
for ( int i = 1; i < count; ++i )
|
||||
{
|
||||
b3Point2D base = pts[i];
|
||||
int j = i - 1;
|
||||
while ( j >= 0 && ( pts[j].p.x > base.p.x || ( pts[j].p.x == base.p.x && pts[j].p.y > base.p.y ) ) )
|
||||
{
|
||||
pts[j + 1] = pts[j];
|
||||
j -= 1;
|
||||
}
|
||||
pts[j + 1] = base;
|
||||
}
|
||||
}
|
||||
|
||||
// Welds points that have been sorted O(n) time
|
||||
static int b3Weld2D( b3Point2D* pts, int count1 )
|
||||
{
|
||||
if ( count1 <= 1 )
|
||||
{
|
||||
return count1;
|
||||
}
|
||||
|
||||
float linearSlop = 0.25f * B3_LINEAR_SLOP;
|
||||
float tolSqr = linearSlop * linearSlop;
|
||||
|
||||
int count2 = 1;
|
||||
int baseIndex = 0;
|
||||
for ( int i = 1; i < count1; i++ )
|
||||
{
|
||||
float distanceSqr = b3DistanceSquared2( pts[i].p, pts[baseIndex].p );
|
||||
if ( distanceSqr < tolSqr )
|
||||
{
|
||||
if ( pts[i].originalIndex < pts[baseIndex].originalIndex )
|
||||
{
|
||||
pts[baseIndex] = pts[i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pts[count2] = pts[i];
|
||||
baseIndex = count2;
|
||||
count2 += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return count2;
|
||||
}
|
||||
|
||||
// Andrew's monotone chain convex hull algorithm
|
||||
// hull must have space for 2 * count1 points
|
||||
int b3Hull2D( b3Point2D* pts, int count, b3Point2D* hull )
|
||||
{
|
||||
int count1 = count;
|
||||
|
||||
if ( count1 <= 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( count1 == 1 )
|
||||
{
|
||||
hull[0] = pts[0];
|
||||
return 1;
|
||||
}
|
||||
|
||||
b3Sort2D( pts, count1 );
|
||||
count1 = b3Weld2D( pts, count1 );
|
||||
|
||||
if ( count1 == 1 )
|
||||
{
|
||||
hull[0] = pts[0];
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ( count1 == 2 )
|
||||
{
|
||||
hull[0] = pts[0];
|
||||
hull[1] = pts[1];
|
||||
return 2;
|
||||
}
|
||||
|
||||
int count2 = 0;
|
||||
|
||||
for ( int i = 0; i < count1; i++ )
|
||||
{
|
||||
while ( count2 >= 2 )
|
||||
{
|
||||
float area = b3Cross2D( hull[count2 - 2].p, hull[count2 - 1].p, pts[i].p );
|
||||
if ( area > 0.0f )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( count2 < 2 * count1 );
|
||||
hull[count2] = pts[i];
|
||||
count2 += 1;
|
||||
}
|
||||
|
||||
int lowerCount = count2 + 1;
|
||||
|
||||
for ( int i = count1 - 2; i >= 0; i-- )
|
||||
{
|
||||
while ( count2 >= lowerCount )
|
||||
{
|
||||
float area = b3Cross2D( hull[count2 - 2].p, hull[count2 - 1].p, pts[i].p );
|
||||
if ( area > 0.0f )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( count2 < 2 * count1 );
|
||||
hull[count2] = pts[i];
|
||||
count2 += 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( hull[0].originalIndex == hull[count2 - 1].originalIndex );
|
||||
return count2 - 1;
|
||||
}
|
||||
|
|
|
|||
90
src/joint.c
90
src/joint.c
|
|
@ -149,6 +149,46 @@ b3JointSim* b3GetJointSimCheckType( b3JointId jointId, b3JointType type )
|
|||
return jointSim;
|
||||
}
|
||||
|
||||
static void b3DestroyContactsBetweenBodies( b3World* world, b3Body* bodyA, b3Body* bodyB )
|
||||
{
|
||||
int contactKey;
|
||||
int otherBodyId;
|
||||
|
||||
// use the smaller of the two contact lists
|
||||
if ( bodyA->contactCount < bodyB->contactCount )
|
||||
{
|
||||
contactKey = bodyA->headContactKey;
|
||||
otherBodyId = bodyB->id;
|
||||
}
|
||||
else
|
||||
{
|
||||
contactKey = bodyB->headContactKey;
|
||||
otherBodyId = bodyA->id;
|
||||
}
|
||||
|
||||
// no need to wake bodies when a joint removes collision between them
|
||||
bool wakeBodies = false;
|
||||
|
||||
// destroy the contacts
|
||||
while ( contactKey != B3_NULL_INDEX )
|
||||
{
|
||||
int contactId = contactKey >> 1;
|
||||
int edgeIndex = contactKey & 1;
|
||||
|
||||
b3Contact* contact = b3Array_Get( world->contacts, contactId );
|
||||
contactKey = contact->edges[edgeIndex].nextKey;
|
||||
|
||||
int otherEdgeIndex = edgeIndex ^ 1;
|
||||
if ( contact->edges[otherEdgeIndex].bodyId == otherBodyId )
|
||||
{
|
||||
// Careful, this removes the contact from the current doubly linked list
|
||||
b3DestroyContact( world, contact, wakeBodies );
|
||||
}
|
||||
}
|
||||
|
||||
b3ValidateSolverSets( world );
|
||||
}
|
||||
|
||||
typedef struct b3JointPair
|
||||
{
|
||||
b3Joint* joint;
|
||||
|
|
@ -159,10 +199,20 @@ static b3JointPair b3CreateJoint( b3World* world, const b3JointDef* def, b3Joint
|
|||
{
|
||||
B3_ASSERT( b3IsValidTransform( def->localFrameA ) );
|
||||
B3_ASSERT( b3IsValidTransform( def->localFrameB ) );
|
||||
B3_ASSERT( world->worldId == def->bodyIdA.world0 );
|
||||
B3_ASSERT( world->worldId == def->bodyIdB.world0 );
|
||||
B3_ASSERT( B3_ID_EQUALS( def->bodyIdA, def->bodyIdB ) == false );
|
||||
|
||||
b3Body* bodyA = b3GetBodyFullId( world, def->bodyIdA );
|
||||
b3Body* bodyB = b3GetBodyFullId( world, def->bodyIdB );
|
||||
|
||||
// If the joint prevents collisions, then destroy all contacts between attached bodies.
|
||||
// Do this first so world arrays are not disturbed in the following code.
|
||||
if ( def->collideConnected == false )
|
||||
{
|
||||
b3DestroyContactsBetweenBodies( world, bodyA, bodyB );
|
||||
}
|
||||
|
||||
int bodyIdA = bodyA->id;
|
||||
int bodyIdB = bodyB->id;
|
||||
int maxSetIndex = b3MaxInt( bodyA->setIndex, bodyB->setIndex );
|
||||
|
|
@ -333,46 +383,6 @@ static b3JointPair b3CreateJoint( b3World* world, const b3JointDef* def, b3Joint
|
|||
return (b3JointPair){ joint, jointSim };
|
||||
}
|
||||
|
||||
static void b3DestroyContactsBetweenBodies( b3World* world, b3Body* bodyA, b3Body* bodyB )
|
||||
{
|
||||
int contactKey;
|
||||
int otherBodyId;
|
||||
|
||||
// use the smaller of the two contact lists
|
||||
if ( bodyA->contactCount < bodyB->contactCount )
|
||||
{
|
||||
contactKey = bodyA->headContactKey;
|
||||
otherBodyId = bodyB->id;
|
||||
}
|
||||
else
|
||||
{
|
||||
contactKey = bodyB->headContactKey;
|
||||
otherBodyId = bodyA->id;
|
||||
}
|
||||
|
||||
// no need to wake bodies when a joint removes collision between them
|
||||
bool wakeBodies = false;
|
||||
|
||||
// destroy the contacts
|
||||
while ( contactKey != B3_NULL_INDEX )
|
||||
{
|
||||
int contactId = contactKey >> 1;
|
||||
int edgeIndex = contactKey & 1;
|
||||
|
||||
b3Contact* contact = b3Array_Get( world->contacts, contactId );
|
||||
contactKey = contact->edges[edgeIndex].nextKey;
|
||||
|
||||
int otherEdgeIndex = edgeIndex ^ 1;
|
||||
if ( contact->edges[otherEdgeIndex].bodyId == otherBodyId )
|
||||
{
|
||||
// Careful, this removes the contact from the current doubly linked list
|
||||
b3DestroyContact( world, contact, wakeBodies );
|
||||
}
|
||||
}
|
||||
|
||||
b3ValidateSolverSets( world );
|
||||
}
|
||||
|
||||
void b3Joint_SetConstraintTuning( b3JointId jointId, float hertz, float dampingRatio )
|
||||
{
|
||||
B3_ASSERT( b3IsValidFloat( hertz ) && hertz >= 0.0f );
|
||||
|
|
|
|||
|
|
@ -271,6 +271,8 @@ static inline bool b3AddVertex( b3FoundVertices* vertices, int vertex )
|
|||
return true;
|
||||
}
|
||||
|
||||
#if B3_MAX_MANIFOLD_POINTS == 4
|
||||
|
||||
// Returns true if (score, separation) should replace (bestScore, bestSeparation).
|
||||
static inline bool b3IsBetterCullCandidate( float score, float separation, float bestScore, float bestSeparation, float scoreTol,
|
||||
float separationTol )
|
||||
|
|
@ -288,13 +290,6 @@ static inline bool b3IsBetterCullCandidate( float score, float separation, float
|
|||
return separation < bestSeparation - separationTol;
|
||||
}
|
||||
|
||||
typedef struct b3Point2D
|
||||
{
|
||||
b3Vec2 p;
|
||||
float separation;
|
||||
int originalIndex;
|
||||
} b3Point2D;
|
||||
|
||||
static int b3CullPoints( b3Point2D* points, int count )
|
||||
{
|
||||
if ( count <= 1 )
|
||||
|
|
@ -475,6 +470,8 @@ static int b3CullPoints( b3Point2D* points, int count )
|
|||
return 4;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static int b3ReduceCluster( b3LocalManifoldPoint* points, int count1, b3Vec3 normal, b3Arena arena )
|
||||
{
|
||||
int targetCount = 1;
|
||||
|
|
@ -496,13 +493,20 @@ static int b3ReduceCluster( b3LocalManifoldPoint* points, int count1, b3Vec3 nor
|
|||
pts[i].originalIndex = i;
|
||||
}
|
||||
|
||||
#if B3_MAX_MANIFOLD_POINTS == 4
|
||||
b3Point2D* hull = pts;
|
||||
int count2 = b3CullPoints( pts, count1 );
|
||||
#else
|
||||
b3Point2D* hull = b3Bump( &arena, 2 * count1 * sizeof( b3Point2D ) );
|
||||
int hullCount = b3Hull2D( pts, count1, hull );
|
||||
int count2 = b3SimplifyHull2D( hull, hullCount, B3_MAX_MANIFOLD_POINTS );
|
||||
#endif
|
||||
B3_ASSERT( count2 <= B3_MAX_MANIFOLD_POINTS );
|
||||
|
||||
b3LocalManifoldPoint finalPoints[B3_MAX_MANIFOLD_POINTS];
|
||||
for ( int i = 0; i < count2; ++i )
|
||||
{
|
||||
int index = pts[i].originalIndex;
|
||||
int index = hull[i].originalIndex;
|
||||
B3_ASSERT( 0 <= index && index < count1 );
|
||||
finalPoints[i] = points[index];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -615,7 +615,6 @@ static void b3CollideTask( int startIndex, int endIndex, int workerIndex, void*
|
|||
bool isStaticA = bodyA->type == b3_staticBody;
|
||||
bool isStaticB = bodyB->type == b3_staticBody;
|
||||
bool wasTouching = ( contact->flags & b3_simTouchingFlag );
|
||||
bool isMeshContact = ( contact->flags & b3_simMeshContact );
|
||||
b3BodySim* bodySimA;
|
||||
b3BodySim* bodySimB;
|
||||
if ( wasTouching )
|
||||
|
|
@ -653,7 +652,7 @@ static void b3CollideTask( int startIndex, int endIndex, int workerIndex, void*
|
|||
// Contact recycling optimization. Please cite this library if you use this optimization.
|
||||
// This is inspired by persistent contact manifolds used in some physics engines, such as PhysX.
|
||||
// However, this allows larger relative motion and has fewer tuning parameters (just one).
|
||||
if ( ( isFast == false || isMeshContact == false ) && recycleDistance > 0.0f &&
|
||||
if ( isFast == false && recycleDistance > 0.0f &&
|
||||
( contact->flags & b3_relativeTransformValid ) && ( contact->flags & b3_contactRecycleFlag ) )
|
||||
{
|
||||
// The scalar part of b3InvMulQuat is just the quaternion dot product.
|
||||
|
|
@ -1149,6 +1148,7 @@ void b3World_Step( b3WorldId worldId, float timeStep, int subStepCount )
|
|||
world->finishTaskFcn( world->userTreeTask, world->userTaskContext );
|
||||
world->userTreeTask = NULL;
|
||||
world->activeTaskCount -= 1;
|
||||
b3ValidateNoEnlarged( &world->broadPhase );
|
||||
}
|
||||
|
||||
// Update sensors
|
||||
|
|
|
|||
|
|
@ -8,17 +8,19 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Software prefetch hint. T0 brings the line into all cache levels.
|
||||
// On x86 MSVC exposes _mm_prefetch, ARM MSVC uses __prefetch instead.
|
||||
// clang/gcc provide __builtin_prefetch on every target.
|
||||
#if defined( B3_COMPILER_MSVC )
|
||||
#if defined( _MSC_VER ) && ( defined( _M_ARM ) || defined( _M_ARM64 ) || defined( _M_ARM64EC ) )
|
||||
#include <intrin.h>
|
||||
#if defined( B3_CPU_X86_X64 )
|
||||
#elif defined( _MSC_VER )
|
||||
#include <intrin0.h>
|
||||
#endif
|
||||
|
||||
#if defined( _MSC_VER )
|
||||
#if defined( _M_X64 ) || defined( __x86_64__ ) || defined( _M_IX86 ) || defined( __i386__ )
|
||||
#define b3Prefetch( addr ) _mm_prefetch( (const char*)( addr ), _MM_HINT_T0 )
|
||||
#else
|
||||
#define b3Prefetch( addr ) __prefetch( (const void*)( addr ) )
|
||||
#endif
|
||||
#elif defined( B3_COMPILER_CLANG ) || defined( B3_COMPILER_GCC )
|
||||
#elif defined( __GNUC__ ) || defined( __clang__ )
|
||||
#define b3Prefetch( addr ) __builtin_prefetch( (const void*)( addr ), 0, 3 )
|
||||
#else
|
||||
#define b3Prefetch( addr ) ( (void)( addr ) )
|
||||
|
|
|
|||
|
|
@ -320,7 +320,7 @@ static void b3RecW_STR( b3RecBuffer* buf, const char* s )
|
|||
// single-precision and double-precision sizes (equal for most), so either build configuration passes.
|
||||
_Static_assert( sizeof( void* ) != 8 || sizeof( b3ExplosionDef ) == 32 || sizeof( b3ExplosionDef ) == 48,
|
||||
"b3ExplosionDef changed: update b3RecW_EXPLOSIONDEF and b3RecR_EXPLOSIONDEF together" );
|
||||
_Static_assert( sizeof( void* ) != 8 || sizeof( b3BodyDef ) == 104 || sizeof( b3BodyDef ) == 120,
|
||||
_Static_assert( sizeof( void* ) != 8 || sizeof( b3BodyDef ) == 112 || sizeof( b3BodyDef ) == 128,
|
||||
"b3BodyDef changed: update b3RecW_BODYDEF and b3RecR_BODYDEF together" );
|
||||
_Static_assert( sizeof( void* ) != 8 || sizeof( b3ShapeDef ) == 120,
|
||||
"b3ShapeDef changed: update b3RecW_SHAPEDEF and b3RecR_SHAPEDEF together" );
|
||||
|
|
@ -363,6 +363,7 @@ void b3RecW_BODYDEF( b3RecBuffer* buf, b3BodyDef v )
|
|||
b3RecW_F32( buf, v.angularDamping );
|
||||
b3RecW_F32( buf, v.gravityScale );
|
||||
b3RecW_F32( buf, v.sleepThreshold );
|
||||
b3RecW_F32( buf, v.safetyFactor );
|
||||
b3RecW_STR( buf, v.name );
|
||||
// userData: not preserved
|
||||
b3RecW_U64( buf, 0u );
|
||||
|
|
|
|||
|
|
@ -47,8 +47,8 @@ typedef struct b3World b3World;
|
|||
#define B3_REC_MAGIC 0x43523342u
|
||||
|
||||
// Major recording version is bumped when writers change.
|
||||
// Major version 5 added b3PlaneResult fields.
|
||||
#define B3_REC_VERSION_MAJOR 5
|
||||
// Major version 6 widened the body def with the safety factor.
|
||||
#define B3_REC_VERSION_MAJOR 6
|
||||
|
||||
// Minor tracks op-stream additions that keep the 48 byte header shape.
|
||||
// Minor version 4 added b3Shape_SetMeshMaterial, b3Shape_SetHull, b3Shape_SetMesh
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ B3_REC_OP( 0x37, BodySetBullet, RET_NONE, ARG( BODYID, body ) ARG( BOOL, flag )
|
|||
B3_REC_OP( 0x38, BodyEnableContactRecycling, RET_NONE, ARG( BODYID, body ) ARG( BOOL, flag ) )
|
||||
B3_REC_OP( 0x39, BodyEnableHitEvents, RET_NONE, ARG( BODYID, body ) ARG( BOOL, flag ) )
|
||||
B3_REC_OP( 0x3A, BodyAllowFastRotation, RET_NONE, ARG( BODYID, body ) ARG( BOOL, flag ) )
|
||||
B3_REC_OP( 0x3B, BodySetSafetyFactor, RET_NONE, ARG( BODYID, body ) ARG( F32, value ) )
|
||||
|
||||
// Shape create/destroy
|
||||
B3_REC_OP( 0x40, CreateSphereShape, RET_SHAPEID, ARG( BODYID, body ) ARG( SHAPEDEF, def ) ARG( SPHERE, sphere ) )
|
||||
|
|
|
|||
|
|
@ -428,6 +428,7 @@ b3BodyDef b3RecR_BODYDEF( b3RecReader* rdr )
|
|||
def.angularDamping = b3RecR_F32( rdr );
|
||||
def.gravityScale = b3RecR_F32( rdr );
|
||||
def.sleepThreshold = b3RecR_F32( rdr );
|
||||
def.safetyFactor = b3RecR_F32( rdr );
|
||||
def.name = b3RecR_STR( rdr );
|
||||
(void)b3RecR_U64( rdr ); // userData placeholder
|
||||
def.motionLocks = b3RecR_LOCKS( rdr );
|
||||
|
|
@ -939,6 +940,11 @@ static void b3RecDispatch_BodySetSleepThreshold( const b3RecArgs_BodySetSleepThr
|
|||
b3Body_SetSleepThreshold( b3RecMakeBodyId( rdr, a->body ), a->threshold );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_BodySetSafetyFactor( const b3RecArgs_BodySetSafetyFactor* a, b3RecReader* rdr )
|
||||
{
|
||||
b3Body_SetSafetyFactor( b3RecMakeBodyId( rdr, a->body ), a->value );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_BodyDisable( const b3RecArgs_BodyDisable* a, b3RecReader* rdr )
|
||||
{
|
||||
b3Body_Disable( b3RecMakeBodyId( rdr, a->body ) );
|
||||
|
|
|
|||
|
|
@ -756,11 +756,11 @@ static void b3FinalizeBodiesTask( int startIndex, int endIndex, int workerIndex,
|
|||
// Body is not sleepy
|
||||
body->sleepTime = 0.0f;
|
||||
|
||||
const float safetyFactor = 0.5f;
|
||||
float safetyFactor = body->safetyFactor;
|
||||
float maxMotion = b3MaxFloat( maxDeltaPosition, maxVelocity * timeStep );
|
||||
if ( body->type == b3_dynamicBody && enableContinuous && maxMotion > safetyFactor * sim->minExtent )
|
||||
{
|
||||
// This flag is only retained for debug draw
|
||||
// This flag is used for debug draw and contact recycling.
|
||||
sim->flags |= b3_isFast;
|
||||
|
||||
// Store in fast array for the continuous collision stage
|
||||
|
|
@ -1444,7 +1444,6 @@ void b3Solve( b3World* world, b3StepContext* stepContext )
|
|||
int awakeBodyCount = awakeSet->bodySims.count;
|
||||
if ( awakeBodyCount == 0 )
|
||||
{
|
||||
b3ValidateNoEnlarged( &world->broadPhase );
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ b3BodyDef b3DefaultBodyDef( void )
|
|||
def.type = b3_staticBody;
|
||||
def.rotation = b3Quat_identity;
|
||||
def.sleepThreshold = 0.05f * b3GetLengthUnitsPerMeter();
|
||||
def.safetyFactor = 0.5f;
|
||||
def.gravityScale = 1.0f;
|
||||
def.enableSleep = true;
|
||||
def.isAwake = true;
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@
|
|||
|
||||
// Snapshot image magic 'BNS3' and version
|
||||
#define B3_SNAP_MAGIC 0x33534E42u
|
||||
#define B3_SNAP_VERSION 2u
|
||||
#define B3_SNAP_VERSION 3u // added b3Body::safetyFactor
|
||||
|
||||
#define B3_SNAP_FLAG_VALIDATION 0x1u
|
||||
#define B3_SNAP_FLAG_DOUBLE_PRECISION 0x2u
|
||||
|
|
|
|||
Loading…
Reference in a new issue