box3d/shared/stability.c
Erin Catto c37cfe4cd7
SIMD hull collision (#93)
SIMD based hull collision inspired by example code from @cairnc in #54 

<img width="1676" height="1332" alt="image"
src="https://github.com/user-attachments/assets/58b137bd-bf7b-4c99-863c-64341b973a3d"
/>

- Fixed #78
- Fixed #85 and fixed CI to error on UB.
- Updated benchmarks
- Added AMD scalar benchmarks
- Added Neon benchmarks
- More determinism unit tests
- Sample leak detection
- Added b3Body_AllowFastRotation
- Added b3Body_IsFastRotationAllowed
- Bumped b3HullData version
- Bumped b3CompoundData version
2026-07-18 22:43:03 -07:00

98 lines
2.4 KiB
C

// SPDX-FileCopyrightText: 2026 Erin Catto
// SPDX-License-Identifier: MIT
#include "stability.h"
#include "utils.h"
#include "box3d/box3d.h"
#include <assert.h>
#include <stdlib.h>
MeshDropData CreateMeshDrop( b3WorldId worldId, b3Pos origin )
{
MeshDropData data = { 0 };
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.position = origin;
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
int gridCount = 40;
float cellWidth = 1.0f;
float rowHz = 0.1f;
float columnHz = 0.2f;
float groundAmplitude = 0.5f;
data.mesh = b3CreateWaveMesh( gridCount, gridCount, cellWidth, groundAmplitude, rowHz, columnHz );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.filter.categoryBits = 1;
b3CreateMeshShape( groundId, &shapeDef, data.mesh, b3Vec3_one );
}
{
b3BoxHull box = b3MakeBoxHull( 0.02f, 0.2f, 0.04f );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.baseMaterial.rollingResistance = 0.1f;
// Don't allow shapes to collide with each other.
shapeDef.filter.categoryBits = 2;
shapeDef.filter.maskBits = 1;
g_randomSeed = 3963634789;
int gridCount = MESH_DROP_GRID_COUNT;
for ( int i = 0; i < gridCount; ++i )
{
for ( int j = 0; j < gridCount; ++j )
{
b3Vec3 linearVelocity = RandomVec3Uniform( -1.0f, 1.0f );
b3Vec3 angularVelocity = RandomVec3Uniform( -5.0f, 5.0f );
bodyDef.position =
b3OffsetPos( origin, (b3Vec3){ 0.5f * ( i - 0.5f * gridCount ), 5.0f, 0.5f * ( j - 0.5f * gridCount ) } );
bodyDef.linearVelocity = linearVelocity;
bodyDef.angularVelocity = angularVelocity;
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
data.bodies[i * gridCount + j] = bodyId;
b3CreateHullShape( bodyId, &shapeDef, &box.base );
}
}
}
return data;
}
bool UpdateMeshDrop( b3WorldId worldId, MeshDropData* data )
{
if ( data->hash == 0 )
{
if ( b3World_GetAwakeBodyCount( worldId ) == 0 )
{
data->hash = B3_HASH_INIT;
int bodyCount = MESH_DROP_GRID_COUNT * MESH_DROP_GRID_COUNT;
for ( int i = 0; i < bodyCount; ++i )
{
b3WorldTransform xf = b3Body_GetTransform( data->bodies[i] );
data->hash = b3Hash( data->hash, (uint8_t*)&xf, sizeof( b3WorldTransform ) );
}
data->sleepStep = data->stepCount;
}
}
data->stepCount += 1;
return data->hash != 0;
}
void DestroyMeshDrop( MeshDropData* data )
{
b3DestroyMesh( data->mesh );
}