Replay fixes (#17)

Fixes to work with our game.
Replay fixes and improvements.
Removed shape names (save memory).
B3_DEFAULT_CATEGORY_BITS  is now UINT64_MAX to make selection easier.
This commit is contained in:
Erin Catto 2026-06-26 21:10:10 -07:00 • committed by GitHub
commit 0c8ef46931
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
51 changed files with 5653 additions and 3853 deletions

View file

@ -24,9 +24,8 @@
#include "box3d/box3d.h"
#include <nfd.h>
#include <ctype.h>
#include <nfd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@ -105,7 +104,9 @@ void SampleContext::Save()
fprintf( file, " \"sunStrength\": %g,\n", GetSun().strength );
fprintf( file, " \"debugView\": %d,\n", debugView );
fprintf( file, " \"showHullEdges\": %s,\n", GetEdgeOverlayParams().showHulls ? "true" : "false" );
fprintf( file, " \"showEdgeConvexity\": %s\n", GetEdgeOverlayParams().showEdgeConvexity ? "true" : "false" );
fprintf( file, " \"showEdgeConvexity\": %s,\n", GetEdgeOverlayParams().showEdgeConvexity ? "true" : "false" );
fprintf( file, " \"replayKeyframeBudgetMB\": %d,\n", replayKeyframeBudgetMB );
fprintf( file, " \"replayKeyframeMinInterval\": %d\n", replayKeyframeMinInterval );
fprintf( file, "}\n" );
fclose( file );
}
@ -253,6 +254,30 @@ void SampleContext::Load()
p.showEdgeConvexity = strncmp( s, "true", 4 ) == 0;
SetEdgeOverlayParams( &p );
}
else if ( jsoneq( data, &tokens[i], "replayKeyframeBudgetMB" ) == 0 )
{
int count = tokens[i + 1].end - tokens[i + 1].start;
if ( count > (int)sizeof( buffer ) - 1 )
{
count = (int)sizeof( buffer ) - 1;
}
const char* s = data + tokens[i + 1].start;
memcpy( buffer, s, count );
buffer[count] = 0;
replayKeyframeBudgetMB = b3ClampInt( (int)strtol( buffer, nullptr, 10 ), 64, 4096 );
}
else if ( jsoneq( data, &tokens[i], "replayKeyframeMinInterval" ) == 0 )
{
int count = tokens[i + 1].end - tokens[i + 1].start;
if ( count > (int)sizeof( buffer ) - 1 )
{
count = (int)sizeof( buffer ) - 1;
}
const char* s = data + tokens[i + 1].start;
memcpy( buffer, s, count );
buffer[count] = 0;
replayKeyframeMinInterval = b3ClampInt( (int)strtol( buffer, nullptr, 10 ), 1, 1024 );
}
}
free( data );
@ -436,53 +461,66 @@ void Sample::Step()
m_triangleIndex = -1;
m_userMaterialId = 0;
// Cursor pick feeds the surface type readout only. Highlighting is selection driven.
if ( m_camera->m_thirdPerson == false )
{
PickRay pickRay = m_camera->BuildPickRay( m_context->mouseX, m_context->mouseY );
b3RayResult result = b3World_CastRayClosest( m_worldId, pickRay.origin, pickRay.translation, b3DefaultQueryFilter() );
b3QueryFilter filter = b3DefaultQueryFilter();
filter.name = "pick";
b3RayResult result = b3World_CastRayClosest( m_worldId, pickRay.origin, pickRay.translation, filter );
b3BodyId hovered = b3_nullBodyId;
if ( result.hit )
{
b3ShapeType type = b3Shape_GetType( result.shapeId );
if ( type == b3_meshShape )
if ( b3Shape_GetType( result.shapeId ) == b3_meshShape )
{
m_triangleIndex = result.triangleIndex;
}
m_userMaterialId = result.userMaterialId;
// Only dynamic bodies highlight, so the static ground never lights up.
b3BodyId bodyId = b3Shape_GetBody( result.shapeId );
if ( b3Body_GetType( bodyId ) == b3_dynamicBody )
{
hovered = bodyId;
}
}
SetHoveredBody( hovered );
}
// The frame latched the origin before Step, but a third person follow moves the eye while
// stepping, so refresh it here. Shapes come back demoted to float against this origin, the same
// relative frame the translation free view renders, so everything stays precise far from the origin.
SetDrawOrigin( m_camera->m_worldEye );
SetDrawOrigin( m_camera->DrawOrigin() );
b3DebugDraw debugDraw;
MakeDebugDraw( &debugDraw );
// Generous visible volume around the eye. Box3D uses this to decide which shapes enter the
// draw set and lazily fire createDebugShape. The cull bounds live in absolute world space to
// match the broad-phase tree; the draw origin is the world eye.
b3Vec3 r = { 1000.0f, 1000.0f, 1000.0f };
b3AABB bounds = b3OffsetAABB( { b3Neg( r ), r }, m_camera->m_worldEye );
debugDraw.drawingBounds = bounds;
// Box3D uses this to decide which shapes enter the draw set and lazily fire
// createDebugShape. The camera derives it from the view distance, in length units
// around the simulation eye, matching the broad-phase tree and the far plane.
debugDraw.drawingBounds = m_camera->DrawBounds();
ApplyGuiFlags( &debugDraw );
b3World_Draw( m_worldId, &debugDraw, B3_DEFAULT_MASK_BITS );
}
bool Sample::FocusBounds( b3AABB* )
{
return false;
}
float Sample::InfoPanelWidthEm() const
{
return INFO_PANEL_WIDTH;
}
b3BodyId Sample::FocusBody() const
{
return GetSelectedBody();
}
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 );
}
void Sample::ResetProfile()
{
m_stepCount = 0;
@ -539,7 +577,7 @@ void Sample::DrawMetrics()
}
float fontSize = ImGui::GetFontSize();
float menuWidth = INFO_PANEL_WIDTH * fontSize;
float menuWidth = InfoPanelWidthEm() * fontSize;
float drawerHeight = 16.0f * fontSize;
float drawerWidth = m_camera->m_width - menuWidth - 1.5f * fontSize;
@ -1089,12 +1127,33 @@ void Sample::MouseDown( b3Vec2 p, int button, int modifiers )
{
if ( button == 0 && modifiers == 0 )
{
// Plain click selects the body under the cursor, any type. Empty space clears.
PickRay pickRay = m_camera->BuildPickRay( p.x, p.y );
b3RayResult result = b3World_CastRayClosest( m_worldId, pickRay.origin, pickRay.translation,
b3DefaultQueryFilter() );
b3QueryFilter filter = b3DefaultQueryFilter();
filter.name = "select";
b3RayResult result = b3World_CastRayClosest( m_worldId, pickRay.origin, pickRay.translation, filter );
if ( result.hit )
{
SetSelectedBody( b3Shape_GetBody( result.shapeId ) );
}
else
{
ClearSelection();
}
}
else if ( button == 0 && modifiers == MOD_CTRL )
{
// Ctrl click grabs a dynamic body with a kinematic mouse body and a motor joint.
PickRay pickRay = m_camera->BuildPickRay( p.x, p.y );
b3QueryFilter filter = b3DefaultQueryFilter();
filter.name = "grab";
b3RayResult result = b3World_CastRayClosest( m_worldId, pickRay.origin, pickRay.translation, filter );
b3BodyId bodyId = result.hit ? b3Shape_GetBody( result.shapeId ) : b3_nullBodyId;
if ( result.hit && b3Body_GetType( bodyId ) == b3_dynamicBody )
{
m_mousePoint = result.point;
@ -1104,17 +1163,8 @@ void Sample::MouseDown( b3Vec2 p, int button, int modifiers )
bodyDef.enableSleep = false;
m_mouseBodyId = b3CreateBody( m_worldId, &bodyDef );
b3BodyId bodyId = b3Shape_GetBody( result.shapeId );
// A plain click also sets the selection. Dynamic only, matching hover.
if ( b3Body_GetType( bodyId ) == b3_dynamicBody )
{
SetSelectedBody( bodyId );
}
else
{
ClearSelection();
}
// Grabbing also selects so the body stays highlighted while dragged.
SetSelectedBody( bodyId );
b3MotorJointDef jointDef = b3DefaultMotorJointDef();
jointDef.base.bodyIdA = m_mouseBodyId;
@ -1142,11 +1192,6 @@ void Sample::MouseDown( b3Vec2 p, int button, int modifiers )
m_mouseFraction = result.fraction;
}
else
{
// Click into empty space clears the selection.
ClearSelection();
}
}
else if ( modifiers & MOD_SHIFT )
{
@ -1206,8 +1251,6 @@ void Sample::MouseUp( b3Vec2 p, int button )
m_mouseJointId = b3_nullJointId;
m_mouseBodyId = b3_nullBodyId;
m_mouseFraction = 0.0f;
ClearSelection();
}
void Sample::MouseMove( b3Vec2 p )
@ -1546,7 +1589,6 @@ static void DrawMenuBar( SampleContext* context )
float aspect = cam.m_height > 0 ? (float)cam.m_width / (float)cam.m_height : 1.0f;
cam.Frame( aabb, aspect, 0.75f );
}
ImGui::Separator();
ImGui::MenuItem( "Shapes", nullptr, &gd->drawShapes );
ImGui::MenuItem( "Transparent", nullptr, &context->transparentDynamic );
ImGui::MenuItem( "Joints", nullptr, &gd->drawJoints );
@ -1581,7 +1623,7 @@ static void DrawMenuBar( SampleContext* context )
{
ImGui::PushItemWidth( 6.0f * fontSize );
ImGui::InputFloat( "Joint", &gd->jointScale );
ImGui::InputFloat( "Force", &gd->forceScale );
ImGui::InputFloat( "Force", &gd->forceScale, 0, 0, "%.6f", ImGuiInputTextFlags_CharsScientific );
ImGui::PopItemWidth();
ImGui::EndMenu();
}
@ -1813,7 +1855,7 @@ static void DrawInfoPanel( SampleContext* context )
{
const SampleEntry& entry = g_sampleEntries[context->sampleIndex];
float fontSize = ImGui::GetFontSize();
float menuWidth = INFO_PANEL_WIDTH * fontSize;
float menuWidth = context->sample->InfoPanelWidthEm() * fontSize;
float menuBarHeight = ImGui::GetFrameHeight();
// Full-height panel pinned under the menu bar at the right edge, matching Box2D.
@ -1833,12 +1875,15 @@ static void DrawInfoPanel( SampleContext* context )
ImGui::TextColored( HexColor( b3_colorSeaGreen ), "step %d", context->sample->m_stepCount );
ImGui::Separator();
// The camera lives in display space, so pivot, radius and speed are in meters
// regardless of the simulation's length units.
b3Pos p = context->camera.m_pivot;
ImGui::TextColored( HexColor( b3_colorSeaGreen ), "pivot (%.1f, %.1f, %.1f)", p.x, p.y, p.z );
ImGui::TextColored( HexColor( b3_colorSeaGreen ), "pivot m (%.1f, %.1f, %.1f)", p.x, p.y, p.z );
float yawDeg = B3_RAD_TO_DEG * context->camera.m_yaw;
float pitchDeg = B3_RAD_TO_DEG * context->camera.m_pitch;
ImGui::TextColored( HexColor( b3_colorSeaGreen ), "yaw/pitch (%.1f, %.1f)", yawDeg, pitchDeg );
ImGui::TextColored( HexColor( b3_colorSeaGreen ), "radius %.1f", context->camera.m_radius );
ImGui::TextColored( HexColor( b3_colorSeaGreen ), "radius m %.1f, speed m/s %.1f", context->camera.m_radius,
context->camera.m_speed );
ImGui::Separator();
@ -2103,6 +2148,7 @@ void CharacterMover::SolveMove( float timeStep, b3Vec3 forward, b3Vec3 right, b3
b3Pos rayOrigin = b3TransformWorldPoint( m_transform, m_capsule.center1 );
b3Vec3 rayTranslation = -rayLength * b3Vec3_axisY;
b3QueryFilter skipTeamFilter = { 1, ~2u };
skipTeamFilter.name = "pogo";
b3RayResult rayResult = b3World_CastRayClosest( worldId, rayOrigin, rayTranslation, skipTeamFilter );
if ( rayResult.hit == false )
@ -2131,10 +2177,10 @@ void CharacterMover::SolveMove( float timeStep, b3Vec3 forward, b3Vec3 right, b3
b3Pos target = m_transform.p + timeStep * m_velocity + timeStep * m_pogoVelocity * b3Vec3_axisY;
// Want the mover to collide with allies
b3QueryFilter moverFilter = { .categoryBits = 1, .maskBits = ~0u };
b3QueryFilter moverFilter = { .categoryBits = 1, .maskBits = ~0u, .id = 1, .name = "mover_collide" };
// The cast should ignore allies
b3QueryFilter castFilter = { .categoryBits = 1, .maskBits = ~2u };
b3QueryFilter castFilter = { .categoryBits = 1, .maskBits = ~2u, .id = 1, .name = "mover_cast" };
m_totalIterations = 0;
float tolerance = 0.01f;
@ -2281,7 +2327,7 @@ void CharacterMover::Step( b3ShapeId* ignoreShapes, int ignoreCount, bool clipVe
m_sample->m_camera->UpdateTransform();
}
SetDrawOrigin( m_sample->m_camera->m_worldEye );
SetDrawOrigin( m_sample->m_camera->DrawOrigin() );
int count = m_planeCount;
for ( int i = 0; i < count; ++i )