mover joint circular bound

This commit is contained in:
Erin Catto 2026-10-24 15:31:39 -07:00
commit 9ab0f0bdc5
21 changed files with 540 additions and 277 deletions

View file

@ -13,6 +13,8 @@
#include <imgui.h>
#include <vector>
class SimpleCompound : public Sample
{
public:
@ -484,6 +486,39 @@ static bool OverlapResultFcn( b3ShapeId shapeId, void* context )
return false;
}
struct VillageBoxQuery
{
b3AABB aabb;
int shapeCount;
int childCount;
};
static bool CountCompoundChildFcn( const b3CompoundData* compound, int childIndex, void* context )
{
(void)compound;
(void)childIndex;
int* count = (int*)context;
*count += 1;
return true;
}
// The world query only reaches the compound as a whole, so descend into its children like a game would
static bool VillageBoxQueryFcn( b3ShapeId shapeId, void* context )
{
VillageBoxQuery* query = (VillageBoxQuery*)context;
query->shapeCount += 1;
if ( b3Shape_GetType( shapeId ) == b3_compoundShape )
{
b3WorldTransform bodyTransform = b3Body_GetTransform( b3Shape_GetBody( shapeId ) );
b3Transform worldToLocal = b3InvMulWorldTransforms( bodyTransform, b3WorldTransform_identity );
b3AABB localBox = b3AABB_Transform( worldToLocal, query->aabb );
b3QueryCompound( b3Shape_GetCompound( shapeId ), localBox, CountCompoundChildFcn, &query->childCount );
}
return true;
}
class Village : public Sample
{
public:
@ -691,6 +726,80 @@ public:
}
}
// Fixed seed so timings compare across runs and builds
void CreateStressQueries()
{
uint32_t savedSeed = g_randomSeed;
g_randomSeed = 271828;
float h = 0.45f * m_worldWidth;
m_stressRays.resize( m_stressRayCount );
for ( StressRay& ray : m_stressRays )
{
ray.origin = { RandomFloatRange( -h, h ), 20.0f, RandomFloatRange( -h, h ) };
ray.translation = { RandomFloatRange( -10.0f, 10.0f ), -40.0f, RandomFloatRange( -10.0f, 10.0f ) };
}
m_stressBoxes.resize( m_stressBoxCount );
for ( b3AABB& box : m_stressBoxes )
{
b3Vec3 center = { RandomFloatRange( -h, h ), RandomFloatRange( 0.0f, 8.0f ), RandomFloatRange( -h, h ) };
b3Vec3 extents = RandomVec3( { 0.5f, 0.5f, 0.5f }, { 4.0f, 4.0f, 4.0f } );
box = { center - extents, center + extents };
}
g_randomSeed = savedSeed;
}
void RunStressQueries()
{
b3QueryFilter filter = b3DefaultQueryFilter();
uint64_t ticks = b3GetTicks();
int rayHitCount = 0;
for ( const StressRay& ray : m_stressRays )
{
CastClosestContext context = {};
(void)b3World_CastRay( m_worldId, ray.origin, ray.translation, filter, CastClosestCallback, &context );
rayHitCount += context.hit ? 1 : 0;
}
float rayTime = b3GetMillisecondsAndReset( &ticks );
VillageBoxQuery query = {};
for ( b3AABB box : m_stressBoxes )
{
query.aabb = box;
(void)b3World_OverlapAABB( m_worldId, box, filter, VillageBoxQueryFcn, &query );
}
float boxTime = b3GetMilliseconds( ticks );
int rayCount = (int)m_stressRays.size();
int boxCount = (int)m_stressBoxes.size();
DrawTextLine( "rays = %d, hits = %d", rayCount, rayHitCount );
DrawTextLine( "ray time = %.3f ms, ave = %.3f us", rayTime, 1000.0f * rayTime / rayCount );
DrawTextLine( "aabb queries = %d, shapes = %d, children = %d", boxCount, query.shapeCount, query.childCount );
DrawTextLine( "aabb time = %.3f ms, ave = %.3f us", boxTime, 1000.0f * boxTime / boxCount );
}
bool DrawControls() override
{
bool changed = ImGui::Checkbox( "Query Stress", &m_stressQueries );
changed = ImGui::SliderInt( "Rays", &m_stressRayCount, 1, 100000 ) || changed;
changed = ImGui::SliderInt( "AABB Queries", &m_stressBoxCount, 1, 100000 ) || changed;
if ( changed && m_stressQueries )
{
CreateStressQueries();
}
return true;
}
void Render() override
{
Sample::Render();
@ -742,6 +851,11 @@ public:
DrawTextLine( "third person (T) = %d", m_camera->m_thirdPerson );
if ( m_stressQueries )
{
RunStressQueries();
}
b3Vec3 translation = { 10.0f, -40.0f, -5.0f };
b3QueryFilter filter = b3DefaultQueryFilter();
@ -828,10 +942,22 @@ public:
return new Village( context );
}
struct StressRay
{
b3Pos origin;
b3Vec3 translation;
};
b3CompoundData* m_compound;
GeometricMover m_mover;
float m_worldWidth;
b3Pos m_rayOrigin;
std::vector<StressRay> m_stressRays;
std::vector<b3AABB> m_stressBoxes;
int m_stressRayCount = 1000;
int m_stressBoxCount = 1000;
bool m_stressQueries = false;
};
static int sampleVillage = RegisterSample( "Compound", "Village", Village::Create );