Dynamic Mover (#202)
Port the dynamic character mover from Box2D including the mover joint and pogo joint. Match the mover and sample structure of Box2D.
This commit is contained in:
parent
f36d061ba2
commit
3362633204
41 changed files with 4580 additions and 885 deletions
|
|
@ -1,11 +1,11 @@
|
|||
// SPDX-FileCopyrightText: 2025 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "geometric_mover.h"
|
||||
#include "gfx/debug_adapter.h"
|
||||
#include "gfx/draw.h"
|
||||
#include "human.h"
|
||||
#include "mesh_loader.h"
|
||||
#include "mover.h"
|
||||
#include "sample.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
|
@ -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:
|
||||
|
|
@ -501,7 +536,9 @@ public:
|
|||
}
|
||||
|
||||
m_launchSpeedScale = 2.0f;
|
||||
m_mover.Initialize( this, position );
|
||||
GeometricMoverDef moverDef;
|
||||
moverDef.position = position;
|
||||
m_mover.Create( m_worldId, &moverDef );
|
||||
|
||||
{
|
||||
constexpr int capsuleCapacity = gridCount * gridCount / 8 + 1;
|
||||
|
|
@ -689,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();
|
||||
|
|
@ -711,9 +822,40 @@ public:
|
|||
|
||||
void Step() override
|
||||
{
|
||||
m_mover.Step( nullptr, 0, true );
|
||||
MoverInput input = GetMoverInput();
|
||||
if ( input.jump )
|
||||
{
|
||||
m_mover.Jump();
|
||||
}
|
||||
|
||||
float speedScale = 1.0f;
|
||||
if ( input.walk )
|
||||
{
|
||||
speedScale = 0.25f;
|
||||
}
|
||||
else if ( m_mover.m_onGround && input.sprint )
|
||||
{
|
||||
speedScale = 1.5f;
|
||||
}
|
||||
float moverTimeStep = m_context->hertz > 0.0f ? 1.0f / m_context->hertz : 0.0f;
|
||||
m_mover.Update( moverTimeStep, input.forward, input.right, input.throttle, speedScale );
|
||||
|
||||
if ( m_camera->m_thirdPerson )
|
||||
{
|
||||
m_camera->m_pivot = m_mover.m_position;
|
||||
m_camera->UpdateTransform();
|
||||
}
|
||||
|
||||
SetDrawOrigin( m_camera->DrawOrigin() );
|
||||
m_mover.Draw();
|
||||
|
||||
DrawTextLine( "third person (T) = %d", m_camera->m_thirdPerson );
|
||||
|
||||
if ( m_stressQueries )
|
||||
{
|
||||
RunStressQueries();
|
||||
}
|
||||
|
||||
b3Vec3 translation = { 10.0f, -40.0f, -5.0f };
|
||||
b3QueryFilter filter = b3DefaultQueryFilter();
|
||||
|
||||
|
|
@ -800,10 +942,22 @@ public:
|
|||
return new Village( context );
|
||||
}
|
||||
|
||||
struct StressRay
|
||||
{
|
||||
b3Pos origin;
|
||||
b3Vec3 translation;
|
||||
};
|
||||
|
||||
b3CompoundData* m_compound;
|
||||
CharacterMover m_mover;
|
||||
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 );
|
||||
|
|
|
|||
Loading…
Reference in a new issue