Optimizations (#168)
- Optimize contact prepare - Back to post restitution stage - Optimize solver math - Bug fixes
This commit is contained in:
parent
9e5a4cde86
commit
79fc55976a
55 changed files with 3248 additions and 1519 deletions
|
|
@ -177,6 +177,7 @@ set(SAMPLE_FILES
|
|||
sample_mesh.cpp
|
||||
sample_ragdoll.cpp
|
||||
sample_replay.cpp
|
||||
sample_restitution.cpp
|
||||
sample_robustness.cpp
|
||||
sample_shapes.cpp
|
||||
sample_stacking.cpp
|
||||
|
|
|
|||
|
|
@ -194,7 +194,10 @@ static void OnEvent( const sapp_event* e )
|
|||
}
|
||||
else
|
||||
{
|
||||
// The replay viewer redraws the highlight from its own selection every
|
||||
// frame, so it has to clear that too
|
||||
ClearSelection();
|
||||
s_context.sample->Keyboard( e->key_code, ACTION_PRESS, mods );
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -238,41 +241,20 @@ static void OnEvent( const sapp_event* e )
|
|||
break;
|
||||
|
||||
case SAPP_KEYCODE_LEFT_BRACKET:
|
||||
SelectSample( &s_context, b3MaxInt( 0, s_context.sampleIndex - 1 ), false );
|
||||
SelectSample( &s_context, ( s_context.sampleIndex + g_sampleCount - 1 ) % g_sampleCount, false );
|
||||
break;
|
||||
|
||||
case SAPP_KEYCODE_RIGHT_BRACKET:
|
||||
SelectSample( &s_context, b3MinInt( g_sampleCount - 1, s_context.sampleIndex + 1 ), false );
|
||||
SelectSample( &s_context, ( s_context.sampleIndex + 1 ) % g_sampleCount, false );
|
||||
break;
|
||||
|
||||
case SAPP_KEYCODE_F:
|
||||
{
|
||||
// Frame the selection, or let the sample frame its whole scene when nothing is
|
||||
// selected. A non-body selection such as a recorded query supplies its own bounds and
|
||||
// takes priority over the hovered body. The replay viewer's scene lives in a
|
||||
// player-owned world, not the base world, so the whole-scene case routes through
|
||||
// FocusHome.
|
||||
Camera& cam = s_context.camera;
|
||||
float aspect = cam.m_height > 0 ? (float)cam.m_width / (float)cam.m_height : 1.0f;
|
||||
b3AABB bounds;
|
||||
if ( s_context.sample->FocusBounds( &bounds ) )
|
||||
{
|
||||
cam.Frame( bounds, aspect, 1.5f );
|
||||
}
|
||||
else
|
||||
{
|
||||
b3BodyId bodyId = s_context.sample->FocusBody();
|
||||
if ( B3_IS_NON_NULL( bodyId ) )
|
||||
{
|
||||
cam.Frame( b3Body_ComputeAABB( bodyId ), aspect, 1.5f );
|
||||
}
|
||||
else
|
||||
{
|
||||
s_context.sample->FocusHome();
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
FrameSelection( &s_context );
|
||||
break;
|
||||
|
||||
case SAPP_KEYCODE_HOME:
|
||||
s_context.sample->FocusHome();
|
||||
break;
|
||||
|
||||
default:
|
||||
s_context.sample->Keyboard( e->key_code, ACTION_PRESS, mods );
|
||||
|
|
|
|||
|
|
@ -442,6 +442,8 @@ void Sample::Step()
|
|||
b3World_EnableSleeping( m_worldId, m_context->enableSleep );
|
||||
b3World_EnableWarmStarting( m_worldId, m_context->enableWarmStarting );
|
||||
b3World_EnableContinuous( m_worldId, m_context->enableContinuous );
|
||||
b3World_SetRestitutionIterations( m_worldId, m_context->restitutionIterations );
|
||||
b3World_EnableRestitutionPropagation( m_worldId, m_context->enableRestitutionPropagation );
|
||||
|
||||
if ( timeStep > 0.0f || m_stepWhilePaused )
|
||||
{
|
||||
|
|
@ -538,9 +540,33 @@ b3BodyId Sample::FocusBody() const
|
|||
|
||||
void Sample::FocusHome()
|
||||
{
|
||||
b3AABB aabb = b3World_GetBounds( m_worldId );
|
||||
float aspect = m_camera->m_height > 0 ? (float)m_camera->m_width / (float)m_camera->m_height : 1.0f;
|
||||
m_camera->Frame( aabb, aspect, 0.75f );
|
||||
m_camera->m_pivot = m_context->homePivot;
|
||||
m_camera->SetOrbit( m_context->homeYaw, m_context->homePitch, m_context->homeRadius );
|
||||
}
|
||||
|
||||
void FrameSelection( SampleContext* context )
|
||||
{
|
||||
// A non-body selection such as a recorded query supplies its own bounds and takes priority over
|
||||
// the hovered body. With nothing selected the sample decides what home is. The replay viewer fits
|
||||
// its recording, which lives in a player-owned world rather than the base world.
|
||||
Sample* sample = context->sample;
|
||||
Camera& cam = context->camera;
|
||||
float aspect = cam.m_height > 0 ? (float)cam.m_width / (float)cam.m_height : 1.0f;
|
||||
b3AABB bounds;
|
||||
if ( sample->FocusBounds( &bounds ) )
|
||||
{
|
||||
cam.Frame( bounds, aspect, 1.5f );
|
||||
return;
|
||||
}
|
||||
|
||||
b3BodyId bodyId = sample->FocusBody();
|
||||
if ( B3_IS_NON_NULL( bodyId ) )
|
||||
{
|
||||
cam.Frame( b3Body_ComputeAABB( bodyId ), aspect, 1.5f );
|
||||
return;
|
||||
}
|
||||
|
||||
sample->FocusHome();
|
||||
}
|
||||
|
||||
void Sample::ResetProfile()
|
||||
|
|
@ -621,7 +647,7 @@ void Sample::DrawMetrics()
|
|||
int count = m_profileWriteIndex - m_profileReadIndex;
|
||||
|
||||
// Unroll ring buffer into per-field histories.
|
||||
constexpr int kRowCount = 21;
|
||||
constexpr int kRowCount = 22;
|
||||
float histories[kRowCount][m_profileCapacity];
|
||||
float totals[kRowCount] = {};
|
||||
for ( int i = 0; i < count; ++i )
|
||||
|
|
@ -640,15 +666,16 @@ void Sample::DrawMetrics()
|
|||
histories[9][i] = p.solveImpulses;
|
||||
histories[10][i] = p.integratePositions;
|
||||
histories[11][i] = p.relaxImpulses;
|
||||
histories[12][i] = p.storeImpulses;
|
||||
histories[13][i] = p.splitIslands;
|
||||
histories[14][i] = p.transforms;
|
||||
histories[15][i] = p.jointEvents;
|
||||
histories[16][i] = p.hitEvents;
|
||||
histories[17][i] = p.refit;
|
||||
histories[18][i] = p.sleepIslands;
|
||||
histories[19][i] = p.bullets;
|
||||
histories[20][i] = p.sensors;
|
||||
histories[12][i] = p.restitution;
|
||||
histories[13][i] = p.storeImpulses;
|
||||
histories[14][i] = p.splitIslands;
|
||||
histories[15][i] = p.transforms;
|
||||
histories[16][i] = p.jointEvents;
|
||||
histories[17][i] = p.hitEvents;
|
||||
histories[18][i] = p.refit;
|
||||
histories[19][i] = p.sleepIslands;
|
||||
histories[20][i] = p.bullets;
|
||||
histories[21][i] = p.sensors;
|
||||
|
||||
totals[0] += p.step;
|
||||
totals[1] += p.pairs;
|
||||
|
|
@ -662,15 +689,16 @@ void Sample::DrawMetrics()
|
|||
totals[9] += p.solveImpulses;
|
||||
totals[10] += p.integratePositions;
|
||||
totals[11] += p.relaxImpulses;
|
||||
totals[12] += p.storeImpulses;
|
||||
totals[13] += p.splitIslands;
|
||||
totals[14] += p.transforms;
|
||||
totals[15] += p.jointEvents;
|
||||
totals[16] += p.hitEvents;
|
||||
totals[17] += p.refit;
|
||||
totals[18] += p.sleepIslands;
|
||||
totals[19] += p.bullets;
|
||||
totals[20] += p.sensors;
|
||||
totals[12] += p.restitution;
|
||||
totals[13] += p.storeImpulses;
|
||||
totals[14] += p.splitIslands;
|
||||
totals[15] += p.transforms;
|
||||
totals[16] += p.jointEvents;
|
||||
totals[17] += p.hitEvents;
|
||||
totals[18] += p.refit;
|
||||
totals[19] += p.sleepIslands;
|
||||
totals[20] += p.bullets;
|
||||
totals[21] += p.sensors;
|
||||
}
|
||||
|
||||
// Smoothed over the last few frames so bars don't jitter visibly.
|
||||
|
|
@ -730,7 +758,7 @@ void Sample::DrawMetrics()
|
|||
{ "solve", 0, colorSolve }, { "setup", 1, colorDefault }, { "constraints", 1, colorDefault },
|
||||
{ "prepare", 2, colorDefault }, { "velocities", 2, colorDefault }, { "warm start", 2, colorDefault },
|
||||
{ "bias", 2, colorDefault }, { "positions", 2, colorDefault }, { "relax", 2, colorDefault },
|
||||
{ "store", 2, colorDefault }, { "split islands", 2, colorDefault },
|
||||
{ "restitution", 2, colorDefault }, { "store", 2, colorDefault }, { "split islands", 2, colorDefault },
|
||||
{ "transforms", 1, colorDefault }, { "joint events", 1, colorDefault }, { "hit events", 1, colorDefault },
|
||||
{ "refit BVH", 1, colorDefault }, { "sleep", 1, colorDefault }, { "bullets", 1, colorDefault },
|
||||
{ "sensors", 0, colorSensors },
|
||||
|
|
@ -1354,6 +1382,15 @@ void SelectSample( SampleContext* context, int selection, bool restart )
|
|||
context->restart = restart;
|
||||
context->sample = g_sampleEntries[selection].CreateFcn( context );
|
||||
|
||||
// A restart keeps the camera where it was, so the original starting view stays home
|
||||
if ( restart == false )
|
||||
{
|
||||
context->homePivot = context->camera.m_pivot;
|
||||
context->homeYaw = context->camera.m_yaw;
|
||||
context->homePitch = context->camera.m_pitch;
|
||||
context->homeRadius = context->camera.m_radius;
|
||||
}
|
||||
|
||||
// A sample that knows where its content sits relative to its camera says
|
||||
// so, and is taken at its word. The ceiling below guards a guess, not a
|
||||
// measurement, so capping an explicit request would only hide it.
|
||||
|
|
@ -1678,11 +1715,11 @@ static void DrawMenuBar( SampleContext* context )
|
|||
ImGui::Separator();
|
||||
if ( ImGui::MenuItem( "Previous Sample", "[" ) )
|
||||
{
|
||||
SelectSample( context, b3MaxInt( 0, context->sampleIndex - 1 ), false );
|
||||
SelectSample( context, ( context->sampleIndex + g_sampleCount - 1 ) % g_sampleCount, false );
|
||||
}
|
||||
if ( ImGui::MenuItem( "Next Sample", "]" ) )
|
||||
{
|
||||
SelectSample( context, b3MinInt( g_sampleCount - 1, context->sampleIndex + 1 ), false );
|
||||
SelectSample( context, ( context->sampleIndex + 1 ) % g_sampleCount, false );
|
||||
}
|
||||
ImGui::Separator();
|
||||
if ( ImGui::MenuItem( "Reset Profile" ) )
|
||||
|
|
@ -1694,7 +1731,7 @@ static void DrawMenuBar( SampleContext* context )
|
|||
b3World_DumpMemoryStats( context->sample->m_worldId );
|
||||
}
|
||||
ImGui::Separator();
|
||||
if ( ImGui::MenuItem( "Quit", "Esc" ) )
|
||||
if ( ImGui::MenuItem( "Quit", "Ctrl+Q" ) )
|
||||
{
|
||||
sapp_request_quit();
|
||||
}
|
||||
|
|
@ -1707,12 +1744,13 @@ static void DrawMenuBar( SampleContext* context )
|
|||
{
|
||||
context->showUI = false;
|
||||
}
|
||||
if ( ImGui::MenuItem( "Frame Camera" ) )
|
||||
if ( ImGui::MenuItem( "Frame Selection", "F" ) )
|
||||
{
|
||||
b3AABB aabb = b3World_GetBounds( context->sample->m_worldId );
|
||||
Camera& cam = context->camera;
|
||||
float aspect = cam.m_height > 0 ? (float)cam.m_width / (float)cam.m_height : 1.0f;
|
||||
cam.Frame( aabb, aspect, 0.75f );
|
||||
FrameSelection( context );
|
||||
}
|
||||
if ( ImGui::MenuItem( "Reset Camera", "Home" ) )
|
||||
{
|
||||
context->sample->FocusHome();
|
||||
}
|
||||
ImGui::MenuItem( "Shapes", nullptr, &gd->drawShapes );
|
||||
if ( ImGui::BeginMenu( "Transparency" ) )
|
||||
|
|
@ -1844,7 +1882,8 @@ static void DrawMenuBar( SampleContext* context )
|
|||
DrawRow( "R", "Restart sample" );
|
||||
DrawRow( "[ ]", "Previous / next sample" );
|
||||
DrawRow( "Ctrl+O", "Open sample picker" );
|
||||
DrawRow( "F", "Frame selection / world" );
|
||||
DrawRow( "F", "Frame selection" );
|
||||
DrawRow( "Home", "Reset camera" );
|
||||
DrawRow( "?", "Show / hide controls" );
|
||||
DrawRow( "Esc", "Cancel / close" );
|
||||
DrawRow( "Ctrl+Q", "Quit" );
|
||||
|
|
@ -2046,6 +2085,7 @@ static void DrawInfoPanel( SampleContext* context )
|
|||
{
|
||||
ImGui::PushItemWidth( 6.0f * fontSize );
|
||||
ImGui::SliderInt( "Sub-steps##Solver", &context->subStepCount, 1, 50 );
|
||||
ImGui::SliderInt( "Rest Iters##Solver", &context->restitutionIterations, 0, 8 );
|
||||
ImGui::SliderFloat( "Hertz##Solver", &context->hertz, 5.0f, 240.0f, "%.0f hz" );
|
||||
|
||||
if ( ImGui::SliderInt( "Workers##Solver", &context->workerCount, 1, B3_MAX_WORKERS ) )
|
||||
|
|
@ -2065,6 +2105,7 @@ static void DrawInfoPanel( SampleContext* context )
|
|||
ImGui::Checkbox( "Sleep##Solver", &context->enableSleep );
|
||||
ImGui::Checkbox( "Warm Starting##Solver", &context->enableWarmStarting );
|
||||
ImGui::Checkbox( "Continuous##Solver", &context->enableContinuous );
|
||||
ImGui::Checkbox( "Rest Prop##Solver", &context->enableRestitutionPropagation );
|
||||
|
||||
if ( ImGui::Shortcut( ImGuiKey_R ) || ImGui::Button( "Restart" ) )
|
||||
{
|
||||
|
|
|
|||
|
|
@ -57,12 +57,21 @@ struct SampleContext
|
|||
float hertz = 60.0f;
|
||||
float recycleDistance = 0.05f;
|
||||
float drawDistance = 100.0f; // meters, view/cull box half extent, persisted
|
||||
|
||||
// Camera the sample set up on creation. Home returns to it. Fitting the world bounds instead
|
||||
// can put a scene with a large ground plate beyond the draw distance.
|
||||
b3Pos homePivot = {};
|
||||
float homeYaw = 0.0f;
|
||||
float homePitch = 0.0f;
|
||||
float homeRadius = 0.0f;
|
||||
int subStepCount = 4;
|
||||
int restitutionIterations = 2;
|
||||
int workerCount = 1;
|
||||
bool transparentDynamic = false;
|
||||
bool transparentKinematic = false;
|
||||
bool enableWarmStarting = true;
|
||||
bool enableContinuous = true;
|
||||
bool enableRestitutionPropagation = false;
|
||||
bool enableSleep = true;
|
||||
bool pause = false;
|
||||
int singleStep = 0;
|
||||
|
|
@ -150,9 +159,9 @@ public:
|
|||
// framing works regardless of where the cursor sits.
|
||||
virtual b3BodyId FocusBody() const;
|
||||
|
||||
// Frame shortcut with nothing selected: fit the whole scene. Defaults to the live world bounds.
|
||||
// The replay viewer overrides this to fit the recording, whose world is player-owned and separate
|
||||
// from the empty base world.
|
||||
// Home, and the frame shortcut with nothing selected. Defaults to the camera the sample set up on
|
||||
// creation. The replay viewer overrides this to fit the recording, whose world is player-owned and
|
||||
// separate from the empty base world.
|
||||
virtual void FocusHome();
|
||||
|
||||
// Arm recording on the live world, snapshotting it as the seed so capture can begin at any
|
||||
|
|
@ -271,6 +280,9 @@ int RegisterReplay( const char* category, const char* name, SampleCreateFcn* fcn
|
|||
// by leaving the restart flag set while the new sample constructs.
|
||||
void SelectSample( SampleContext* context, int selection, bool restart );
|
||||
|
||||
// Frame the selection, or go home when nothing is selected
|
||||
void FrameSelection( SampleContext* context );
|
||||
|
||||
// Run the native "open replay" file picker and, on success, hand the chosen
|
||||
// file to the Replay viewer. Must be called outside the frame (the dialog spins
|
||||
// a blocking nested run loop), driven by SampleContext::openReplayPicker.
|
||||
|
|
|
|||
|
|
@ -1033,7 +1033,7 @@ static int sampleStall = RegisterSample( "Continuous", "Stall", Stall::Create );
|
|||
// The best way to run this sample is:
|
||||
// 1. press pause (P)
|
||||
// 2. restart (R) or press the Drop button
|
||||
// 3. then single step (O)
|
||||
// 3. then single step (.)
|
||||
// Then look at the metrics and overlap. The shape is drawn orange while it is using
|
||||
// continuous collision detection.
|
||||
class SafetyFactor : public Sample
|
||||
|
|
|
|||
|
|
@ -395,6 +395,14 @@ public:
|
|||
m_camera->SetRenderTransform( b3GetLengthUnitsPerMeter(), m_context->viewZUp );
|
||||
float aspect = m_camera->m_height > 0 ? (float)m_camera->m_width / (float)m_camera->m_height : 1.0f;
|
||||
m_camera->Frame( m_info.bounds, aspect, 0.75f );
|
||||
|
||||
// The bounds include static geometry, and a large ground plate can push the fit past the
|
||||
// draw distance, which culls the whole scene. Pull in so the middle of the scene still draws.
|
||||
float maxRadius = 0.75f * m_context->drawDistance;
|
||||
if ( m_camera->m_radius > maxRadius )
|
||||
{
|
||||
m_camera->SetOrbit( m_camera->m_yaw, m_camera->m_pitch, maxRadius );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -523,7 +531,8 @@ public:
|
|||
}
|
||||
|
||||
// , steps backward. Forward is the global single step on . so it works in every sample, and the
|
||||
// step count lands in the context. Shift moves five frames, matching that key.
|
||||
// step count lands in the context. Shift moves five frames, matching that key. Esc drops the
|
||||
// selection.
|
||||
void Keyboard( int key, int action, int mods ) override
|
||||
{
|
||||
if ( m_generating )
|
||||
|
|
@ -536,7 +545,11 @@ public:
|
|||
return;
|
||||
}
|
||||
|
||||
if ( key == KEY_COMMA )
|
||||
if ( key == KEY_ESCAPE )
|
||||
{
|
||||
m_selKind = SelNone;
|
||||
}
|
||||
else if ( key == KEY_COMMA )
|
||||
{
|
||||
int back = ( mods & MOD_SHIFT ) ? 5 : 1;
|
||||
SeekTo( b3RecPlayer_GetFrame( m_player ) - back );
|
||||
|
|
|
|||
1219
samples/sample_restitution.cpp
Normal file
1219
samples/sample_restitution.cpp
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -239,626 +239,6 @@ public:
|
|||
|
||||
static int sampleSlideTwist = RegisterSample( "Shapes", "Slide Twist", SlideTwist::Create );
|
||||
|
||||
class Restitution : public Sample
|
||||
{
|
||||
public:
|
||||
enum ShapeType
|
||||
{
|
||||
e_sphereShape = 0,
|
||||
e_boxShape
|
||||
};
|
||||
|
||||
explicit Restitution( SampleContext* context )
|
||||
: Sample( context )
|
||||
{
|
||||
if ( context->restart == false )
|
||||
{
|
||||
m_camera->SetView( 0.0f, 25.0f, 85.0f, { 0.0f, 20.0f, 0.0f } );
|
||||
}
|
||||
|
||||
AddGroundBox( 50.0f );
|
||||
|
||||
m_shapeType = e_sphereShape;
|
||||
|
||||
CreateBodies();
|
||||
}
|
||||
|
||||
void CreateBodies()
|
||||
{
|
||||
for ( int i = 0; i < m_count; ++i )
|
||||
{
|
||||
if ( B3_IS_NON_NULL( m_bodyIds[i] ) )
|
||||
{
|
||||
b3DestroyBody( m_bodyIds[i] );
|
||||
m_bodyIds[i] = b3_nullBodyId;
|
||||
}
|
||||
}
|
||||
|
||||
b3Sphere sphere = { b3Vec3_zero, 0.5f };
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
|
||||
float dr = 1.0f / ( m_count > 1 ? m_count - 1 : 1 );
|
||||
float x = -1.0f * ( m_count - 1 );
|
||||
float dx = 2.0f;
|
||||
|
||||
for ( int i = 0; i < m_count; ++i )
|
||||
{
|
||||
bodyDef.position = { x, 40.0f, 0.0f };
|
||||
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
m_bodyIds[i] = bodyId;
|
||||
|
||||
if ( m_shapeType == e_sphereShape )
|
||||
{
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
}
|
||||
else
|
||||
{
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
}
|
||||
|
||||
shapeDef.baseMaterial.restitution += dr;
|
||||
x += dx;
|
||||
}
|
||||
}
|
||||
|
||||
bool DrawControls() override
|
||||
{
|
||||
if ( ImGui::RadioButton( "Sphere", m_shapeType == e_sphereShape ) )
|
||||
{
|
||||
m_shapeType = e_sphereShape;
|
||||
CreateBodies();
|
||||
}
|
||||
|
||||
if ( ImGui::RadioButton( "Box", m_shapeType == e_boxShape ) )
|
||||
{
|
||||
m_shapeType = e_boxShape;
|
||||
CreateBodies();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static Sample* Create( SampleContext* context )
|
||||
{
|
||||
return new Restitution( context );
|
||||
}
|
||||
|
||||
static constexpr int m_count = 40;
|
||||
|
||||
b3BodyId m_bodyIds[m_count] = {};
|
||||
ShapeType m_shapeType;
|
||||
};
|
||||
|
||||
static int sampleRestitution = RegisterSample( "Shapes", "Restitution", Restitution::Create );
|
||||
|
||||
|
||||
// Similar to the MeasureSupportedBounce unit test
|
||||
class SphereStackRestitution : public Sample
|
||||
{
|
||||
public:
|
||||
static constexpr float m_impactSpeed = 5.0f;
|
||||
static constexpr int m_maxStackCount = 3;
|
||||
|
||||
explicit SphereStackRestitution( SampleContext* context )
|
||||
: Sample( context )
|
||||
{
|
||||
if ( context->restart == false )
|
||||
{
|
||||
m_camera->SetView( 20.0f, 10.0f, 12.0f, { 0.0f, 2.5f, 0.0f } );
|
||||
}
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.position = { 0.0f, -1.0f, 0.0f };
|
||||
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.baseMaterial.friction = 0.0f;
|
||||
shapeDef.baseMaterial.restitution = 0.0f;
|
||||
b3BoxHull box = b3MakeBoxHull( 40.0f, 1.0f, 40.0f );
|
||||
b3ShapeId groundShapeId = b3CreateHullShape( groundId, &shapeDef, &box.base );
|
||||
SetGroundShape( groundShapeId );
|
||||
|
||||
CreateScene();
|
||||
}
|
||||
|
||||
b3BodyId CreateBall( float y, float velocityY, float restitution, bool hitEvents )
|
||||
{
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = { 0.0f, y, 0.0f };
|
||||
bodyDef.linearVelocity = { 0.0f, velocityY, 0.0f };
|
||||
bodyDef.enableSleep = false;
|
||||
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.baseMaterial.friction = 0.0f;
|
||||
shapeDef.baseMaterial.restitution = restitution;
|
||||
shapeDef.enableHitEvents = hitEvents;
|
||||
b3Sphere sphere = { b3Vec3_zero, 0.5f };
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
|
||||
return bodyId;
|
||||
}
|
||||
|
||||
void CreateScene()
|
||||
{
|
||||
for ( int i = 0; i < m_bodyCount; ++i )
|
||||
{
|
||||
b3DestroyBody( m_bodyIds[i] );
|
||||
}
|
||||
m_bodyCount = 0;
|
||||
|
||||
if ( m_gravity )
|
||||
{
|
||||
b3World_SetGravity( m_worldId, { 0.0f, -10.0f, 0.0f } );
|
||||
}
|
||||
else
|
||||
{
|
||||
b3World_SetGravity( m_worldId, b3Vec3_zero );
|
||||
}
|
||||
|
||||
for ( int i = 0; i < m_stackCount; ++i )
|
||||
{
|
||||
m_bodyIds[m_bodyCount] = CreateBall( 0.5f + 1.0f * i, 0.0f, 0.0f, false );
|
||||
m_bodyCount += 1;
|
||||
}
|
||||
|
||||
// Start half a step of travel above contact so the impact lands mid step, matching the test
|
||||
m_startHeight = 0.5f + 1.0f * m_stackCount + 0.5f * m_impactSpeed * ( 1.0f / 60.0f );
|
||||
|
||||
m_impactorId = CreateBall( m_startHeight, -m_impactSpeed, m_restitution, true );
|
||||
m_bodyIds[m_bodyCount] = m_impactorId;
|
||||
m_bodyCount += 1;
|
||||
|
||||
m_startEnergy = MeasureEnergy( m_worldId, m_bodyIds, m_bodyCount ).Total();
|
||||
m_peakEnergy = m_startEnergy;
|
||||
|
||||
m_coefficient = 0.0f;
|
||||
m_latched = false;
|
||||
m_hit = false;
|
||||
}
|
||||
|
||||
bool DrawControls() override
|
||||
{
|
||||
bool rebuild = false;
|
||||
|
||||
ImGui::PushItemWidth( 6.0f * ImGui::GetFontSize() );
|
||||
|
||||
if ( ImGui::SliderFloat( "Restitution", &m_restitution, 0.0f, 1.0f, "%.2f" ) )
|
||||
{
|
||||
rebuild = true;
|
||||
}
|
||||
|
||||
if ( ImGui::SliderInt( "Stack", &m_stackCount, 0, m_maxStackCount ) )
|
||||
{
|
||||
rebuild = true;
|
||||
}
|
||||
|
||||
ImGui::PopItemWidth();
|
||||
|
||||
if ( ImGui::Checkbox( "Gravity", &m_gravity ) )
|
||||
{
|
||||
rebuild = true;
|
||||
}
|
||||
|
||||
if ( ImGui::Button( "Reset" ) )
|
||||
{
|
||||
rebuild = true;
|
||||
}
|
||||
|
||||
if ( rebuild )
|
||||
{
|
||||
CreateScene();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Step() override
|
||||
{
|
||||
Sample::Step();
|
||||
|
||||
b3ContactEvents events = b3World_GetContactEvents( m_worldId );
|
||||
if ( events.hitCount > 0 )
|
||||
{
|
||||
m_hit = true;
|
||||
}
|
||||
|
||||
float vy = b3Body_GetLinearVelocity( m_impactorId ).y;
|
||||
|
||||
// The rebound only means something once the impact has happened and the impactor is leaving
|
||||
if ( m_latched == false && m_hit && vy > 0.0f )
|
||||
{
|
||||
m_coefficient = vy / m_impactSpeed;
|
||||
m_latched = true;
|
||||
}
|
||||
|
||||
float supportSpeed = 0.0f;
|
||||
for ( int i = 0; i < m_stackCount; ++i )
|
||||
{
|
||||
supportSpeed = b3MaxFloat( supportSpeed, b3Length( b3Body_GetLinearVelocity( m_bodyIds[i] ) ) );
|
||||
}
|
||||
|
||||
DrawLine( { -2.0f, m_startHeight, 0.0f }, { 2.0f, m_startHeight, 0.0f }, MakeColor( b3_colorRed ) );
|
||||
|
||||
MechanicalEnergy energy = MeasureEnergy( m_worldId, m_bodyIds, m_bodyCount );
|
||||
float total = energy.Total();
|
||||
m_peakEnergy = b3MaxFloat( m_peakEnergy, total );
|
||||
|
||||
DrawTextLine( "impactor vy = %.3f m/s", vy );
|
||||
|
||||
DrawTextLine( "coefficient = %.4f live (target %.2f)", vy / m_impactSpeed, m_restitution );
|
||||
|
||||
if ( m_latched )
|
||||
{
|
||||
DrawTextLine( "coefficient = %.4f at first rebound", m_coefficient );
|
||||
}
|
||||
|
||||
DrawTextLine( "support speed = %.4f m/s", supportSpeed );
|
||||
|
||||
float scale = m_startEnergy != 0.0f ? 100.0f / m_startEnergy : 0.0f;
|
||||
DrawTextLine( "kinetic = %.3f J linear + %.3f J angular", energy.linear, energy.angular );
|
||||
DrawTextLine( "potential = %.3f J", energy.potential );
|
||||
DrawTextLine( "total = %.3f J (%.2f%% of start, peak %.2f%%)", total, scale * total, scale * m_peakEnergy );
|
||||
}
|
||||
|
||||
static Sample* Create( SampleContext* context )
|
||||
{
|
||||
return new SphereStackRestitution( context );
|
||||
}
|
||||
|
||||
b3BodyId m_bodyIds[m_maxStackCount + 1] = {};
|
||||
b3BodyId m_impactorId = b3_nullBodyId;
|
||||
int m_bodyCount = 0;
|
||||
int m_stackCount = 2;
|
||||
float m_restitution = 1.0f;
|
||||
float m_startHeight = 0.0f;
|
||||
float m_coefficient = 0.0f;
|
||||
float m_startEnergy = 0.0f;
|
||||
float m_peakEnergy = 0.0f;
|
||||
bool m_gravity = false;
|
||||
bool m_latched = false;
|
||||
bool m_hit = false;
|
||||
};
|
||||
|
||||
static int sampleSphereStackRestitution = RegisterSample( "Shapes", "Sphere Stack Restitution", SphereStackRestitution::Create );
|
||||
|
||||
// Similar to the MeasureFlatBounce and SpinTest unit tests
|
||||
class BoxRestitution : public Sample
|
||||
{
|
||||
public:
|
||||
static constexpr float m_impactSpeed = 5.0f;
|
||||
|
||||
explicit BoxRestitution( SampleContext* context )
|
||||
: Sample( context )
|
||||
{
|
||||
if ( context->restart == false )
|
||||
{
|
||||
m_camera->SetView( 20.0f, 10.0f, 14.0f, { 0.0f, 3.0f, 0.0f } );
|
||||
}
|
||||
|
||||
// Gravity would bias the measured coefficient
|
||||
b3World_SetGravity( m_worldId, b3Vec3_zero );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.position = { 0.0f, -1.0f, 0.0f };
|
||||
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.baseMaterial.friction = 0.0f;
|
||||
shapeDef.baseMaterial.restitution = 0.0f;
|
||||
b3BoxHull box = b3MakeBoxHull( 40.0f, 1.0f, 40.0f );
|
||||
b3ShapeId groundShapeId = b3CreateHullShape( groundId, &shapeDef, &box.base );
|
||||
SetGroundShape( groundShapeId );
|
||||
|
||||
CreateScene();
|
||||
}
|
||||
|
||||
void CreateScene()
|
||||
{
|
||||
if ( B3_IS_NON_NULL( m_boxId ) )
|
||||
{
|
||||
b3DestroyBody( m_boxId );
|
||||
}
|
||||
|
||||
// Start half a step of travel above contact so the impact lands mid step, matching the test
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = { 0.0f, 0.25f + 0.5f * m_impactSpeed * ( 1.0f / 60.0f ), 0.0f };
|
||||
bodyDef.linearVelocity = { 0.0f, -m_impactSpeed, 0.0f };
|
||||
bodyDef.angularVelocity = { 0.0f, 0.0f, m_spin };
|
||||
bodyDef.enableSleep = false;
|
||||
m_boxId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.density = 1.0f;
|
||||
shapeDef.baseMaterial.friction = 0.0f;
|
||||
shapeDef.baseMaterial.restitution = m_restitution;
|
||||
shapeDef.enableHitEvents = true;
|
||||
b3BoxHull box = b3MakeBoxHull( 1.0f, 0.25f, 1.0f );
|
||||
b3CreateHullShape( m_boxId, &shapeDef, &box.base );
|
||||
|
||||
m_startEnergy = MeasureEnergy( m_worldId, &m_boxId, 1 ).Total();
|
||||
m_peakEnergy = m_startEnergy;
|
||||
|
||||
m_coefficient = 0.0f;
|
||||
m_latched = false;
|
||||
m_hit = false;
|
||||
}
|
||||
|
||||
bool DrawControls() override
|
||||
{
|
||||
bool rebuild = false;
|
||||
|
||||
ImGui::PushItemWidth( 6.0f * ImGui::GetFontSize() );
|
||||
|
||||
if ( ImGui::SliderFloat( "Restitution", &m_restitution, 0.0f, 1.0f, "%.2f" ) )
|
||||
{
|
||||
rebuild = true;
|
||||
}
|
||||
|
||||
if ( ImGui::SliderFloat( "Spin", &m_spin, 0.0f, 2.0f, "%.2f" ) )
|
||||
{
|
||||
rebuild = true;
|
||||
}
|
||||
|
||||
ImGui::PopItemWidth();
|
||||
|
||||
if ( ImGui::Button( "Reset" ) )
|
||||
{
|
||||
rebuild = true;
|
||||
}
|
||||
|
||||
if ( rebuild )
|
||||
{
|
||||
CreateScene();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Step() override
|
||||
{
|
||||
Sample::Step();
|
||||
|
||||
b3ContactEvents events = b3World_GetContactEvents( m_worldId );
|
||||
if ( events.hitCount > 0 )
|
||||
{
|
||||
m_hit = true;
|
||||
}
|
||||
|
||||
float vy = b3Body_GetLinearVelocity( m_boxId ).y;
|
||||
b3Vec3 w = b3Body_GetAngularVelocity( m_boxId );
|
||||
|
||||
if ( m_latched == false && m_hit && vy > 0.0f )
|
||||
{
|
||||
m_coefficient = vy / m_impactSpeed;
|
||||
m_latched = true;
|
||||
}
|
||||
|
||||
Vec4 yellow = MakeColor( b3_colorYellow );
|
||||
DrawPoint( b3Body_GetWorldPoint( m_boxId, { -1.0f, -0.25f, -1.0f } ), 8.0f, yellow );
|
||||
DrawPoint( b3Body_GetWorldPoint( m_boxId, { 1.0f, -0.25f, -1.0f } ), 8.0f, yellow );
|
||||
DrawPoint( b3Body_GetWorldPoint( m_boxId, { -1.0f, -0.25f, 1.0f } ), 8.0f, yellow );
|
||||
DrawPoint( b3Body_GetWorldPoint( m_boxId, { 1.0f, -0.25f, 1.0f } ), 8.0f, yellow );
|
||||
|
||||
MechanicalEnergy energy = MeasureEnergy( m_worldId, &m_boxId, 1 );
|
||||
float total = energy.Total();
|
||||
m_peakEnergy = b3MaxFloat( m_peakEnergy, total );
|
||||
|
||||
DrawTextLine( "vy = %.3f m/s", vy );
|
||||
|
||||
DrawTextLine( "coefficient = %.4f live (target %.2f)", vy / m_impactSpeed, m_restitution );
|
||||
|
||||
if ( m_latched )
|
||||
{
|
||||
DrawTextLine( "coefficient = %.4f at first rebound", m_coefficient );
|
||||
}
|
||||
|
||||
// The spin is about z, so any x or y component is residual from the sequential point solve
|
||||
DrawTextLine( "spin in = %.2f, spin out = %.4f (ideal %.2f at e = 1)", m_spin, w.z, -m_spin );
|
||||
DrawTextLine( "off axis spin = %.4f rad/s", sqrtf( w.x * w.x + w.y * w.y ) );
|
||||
|
||||
float scale = m_startEnergy != 0.0f ? 100.0f / m_startEnergy : 0.0f;
|
||||
DrawTextLine( "kinetic = %.3f J linear + %.3f J angular", energy.linear, energy.angular );
|
||||
DrawTextLine( "potential = %.3f J", energy.potential );
|
||||
DrawTextLine( "total = %.3f J (%.2f%% of start, peak %.2f%%)", total, scale * total, scale * m_peakEnergy );
|
||||
}
|
||||
|
||||
static Sample* Create( SampleContext* context )
|
||||
{
|
||||
return new BoxRestitution( context );
|
||||
}
|
||||
|
||||
b3BodyId m_boxId = b3_nullBodyId;
|
||||
float m_restitution = 0.9f;
|
||||
float m_spin = 0.0f;
|
||||
float m_coefficient = 0.0f;
|
||||
float m_startEnergy = 0.0f;
|
||||
float m_peakEnergy = 0.0f;
|
||||
bool m_latched = false;
|
||||
bool m_hit = false;
|
||||
};
|
||||
|
||||
static int sampleBoxRestitution = RegisterSample( "Shapes", "Box Restitution", BoxRestitution::Create );
|
||||
|
||||
// Similar to the ImpulseTest unit test. The hit event marks the bounce and carries the approach speed,
|
||||
// so the impulse the contact reports can be checked against reversing that speed at the coefficient.
|
||||
// The total runs a little over because the contact also carries the weight for the part of the step
|
||||
// it is active.
|
||||
class BounceImpulse : public Sample
|
||||
{
|
||||
public:
|
||||
explicit BounceImpulse( SampleContext* context )
|
||||
: Sample( context )
|
||||
{
|
||||
if ( context->restart == false )
|
||||
{
|
||||
m_camera->SetView( 20.0f, 10.0f, 36.0f, { 0.0f, 8.0f, 0.0f } );
|
||||
}
|
||||
|
||||
b3World_SetGravity( m_worldId, { 0.0f, -10.0f, 0.0f } );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.position = { 0.0f, -1.0f, 0.0f };
|
||||
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.baseMaterial.friction = 0.0f;
|
||||
shapeDef.baseMaterial.restitution = 0.0f;
|
||||
b3BoxHull box = b3MakeBoxHull( 40.0f, 1.0f, 40.0f );
|
||||
b3ShapeId groundShapeId = b3CreateHullShape( groundId, &shapeDef, &box.base );
|
||||
SetGroundShape( groundShapeId );
|
||||
|
||||
CreateScene();
|
||||
}
|
||||
|
||||
void CreateScene()
|
||||
{
|
||||
if ( B3_IS_NON_NULL( m_ballId ) )
|
||||
{
|
||||
b3DestroyBody( m_ballId );
|
||||
}
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = { 0.0f, 0.5f + m_dropHeight, 0.0f };
|
||||
bodyDef.enableSleep = false;
|
||||
m_ballId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.baseMaterial.friction = 0.0f;
|
||||
shapeDef.baseMaterial.restitution = m_restitution;
|
||||
shapeDef.enableHitEvents = true;
|
||||
b3Sphere sphere = { b3Vec3_zero, 0.5f };
|
||||
b3CreateSphereShape( m_ballId, &shapeDef, &sphere );
|
||||
|
||||
m_mass = b3Body_GetMass( m_ballId );
|
||||
|
||||
m_approachSpeed = 0.0f;
|
||||
m_totalImpulse = 0.0f;
|
||||
m_expectedImpulse = 0.0f;
|
||||
m_contactSteps = 0;
|
||||
m_hit = false;
|
||||
m_bouncing = false;
|
||||
}
|
||||
|
||||
bool DrawControls() override
|
||||
{
|
||||
bool rebuild = false;
|
||||
|
||||
ImGui::PushItemWidth( 6.0f * ImGui::GetFontSize() );
|
||||
|
||||
if ( ImGui::SliderFloat( "Restitution", &m_restitution, 0.0f, 1.0f, "%.2f" ) )
|
||||
{
|
||||
rebuild = true;
|
||||
}
|
||||
|
||||
if ( ImGui::SliderFloat( "Height", &m_dropHeight, 0.5f, 50.0f, "%.1f" ) )
|
||||
{
|
||||
rebuild = true;
|
||||
}
|
||||
|
||||
ImGui::PopItemWidth();
|
||||
|
||||
if ( ImGui::Button( "Reset" ) )
|
||||
{
|
||||
rebuild = true;
|
||||
}
|
||||
|
||||
if ( rebuild )
|
||||
{
|
||||
CreateScene();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Step() override
|
||||
{
|
||||
Sample::Step();
|
||||
|
||||
if ( m_didStep )
|
||||
{
|
||||
b3ContactEvents events = b3World_GetContactEvents( m_worldId );
|
||||
if ( m_hit == false && events.hitCount > 0 )
|
||||
{
|
||||
m_hit = true;
|
||||
m_bouncing = true;
|
||||
m_approachSpeed = events.hitEvents[0].approachSpeed;
|
||||
m_expectedImpulse = ( 1.0f + m_restitution ) * m_mass * m_approachSpeed;
|
||||
}
|
||||
|
||||
// Keep summing until the contact lets go, a slow bounce can take a few steps to leave
|
||||
if ( m_bouncing )
|
||||
{
|
||||
b3ContactData contactData[4];
|
||||
int contactCount = b3Body_GetContactData( m_ballId, contactData, 4 );
|
||||
for ( int c = 0; c < contactCount; ++c )
|
||||
{
|
||||
for ( int m = 0; m < contactData[c].manifoldCount; ++m )
|
||||
{
|
||||
const b3Manifold* manifold = contactData[c].manifolds + m;
|
||||
for ( int p = 0; p < manifold->pointCount; ++p )
|
||||
{
|
||||
m_totalImpulse += manifold->points[p].totalNormalImpulse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( contactCount > 0 )
|
||||
{
|
||||
m_contactSteps += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_bouncing = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float startHeight = 0.5f + m_dropHeight;
|
||||
DrawLine( { -2.0f, startHeight, 0.0f }, { 2.0f, startHeight, 0.0f }, MakeColor( b3_colorRed ) );
|
||||
|
||||
DrawTextLine( "vy = %.3f m/s", b3Body_GetLinearVelocity( m_ballId ).y );
|
||||
|
||||
if ( m_hit )
|
||||
{
|
||||
DrawTextLine( "approach speed = %.3f m/s", m_approachSpeed );
|
||||
DrawTextLine( "total impulse = %.2f N s over %d steps", m_totalImpulse, m_contactSteps );
|
||||
DrawTextLine( "expected impulse = %.2f N s", m_expectedImpulse );
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawTextLine( "waiting for the hit" );
|
||||
}
|
||||
}
|
||||
|
||||
static Sample* Create( SampleContext* context )
|
||||
{
|
||||
return new BounceImpulse( context );
|
||||
}
|
||||
|
||||
b3BodyId m_ballId = b3_nullBodyId;
|
||||
float m_restitution = 0.5f;
|
||||
float m_dropHeight = 20.0f;
|
||||
float m_mass = 0.0f;
|
||||
float m_approachSpeed = 0.0f;
|
||||
float m_totalImpulse = 0.0f;
|
||||
float m_expectedImpulse = 0.0f;
|
||||
int m_contactSteps = 0;
|
||||
bool m_hit = false;
|
||||
bool m_bouncing = false;
|
||||
};
|
||||
|
||||
static int sampleBounceImpulse = RegisterSample( "Shapes", "Bounce Impulse", BounceImpulse::Create );
|
||||
|
||||
// This shows an optimization when creating many static shapes you can skip having them invoke collision, assuming
|
||||
// dynamic bodies are added after the static bodies.
|
||||
class StaticInvoke : public Sample
|
||||
|
|
|
|||
Loading…
Reference in a new issue