Follow cam (#107)

- follow cam in replay viewer
- world text with halo
- fixed sensor crash
- sensor visitors must now be convex
This commit is contained in:
Erin Catto 2026-07-29 22:14:26 -07:00 • committed by GitHub
commit 3fc20f5b45
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 7450 additions and 127 deletions

View file

@ -3,6 +3,7 @@
#include "gfx/debug_adapter.h"
#include "gfx/draw.h"
#include "gfx/keycodes.h"
#include "gfx/text.h"
#include "imgui.h"
#include "sample.h"
@ -240,6 +241,7 @@ public:
m_hoverQuery = -1;
m_revealSelection = false;
m_drawAllQueries = false;
m_followSelection = false;
m_queryIndexBuilt = false;
m_querySearch[0] = '\0';
m_querySearchKind = 0;
@ -510,6 +512,37 @@ public:
m_replayWorldId = b3RecPlayer_GetWorldId( m_player );
}
// Jump to a recorded frame. Rewinds through the nearest keyframe, so a backward hop costs the
// re-simulation from there.
void SeekTo( int frame )
{
b3RecPlayer_SeekFrame( m_player, frame );
m_replayWorldId = b3RecPlayer_GetWorldId( m_player );
m_frameAccumulator = 0.0f;
}
// , 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.
void Keyboard( int key, int action, int mods ) override
{
if ( m_generating )
{
return;
}
if ( m_player == nullptr || action != ACTION_PRESS || ( mods & ( MOD_CTRL | MOD_ALT ) ) != 0 )
{
return;
}
if ( key == KEY_COMMA )
{
int back = ( mods & MOD_SHIFT ) ? 5 : 1;
SeekTo( b3RecPlayer_GetFrame( m_player ) - back );
m_context->pause = true;
}
}
void Step() override
{
SetDrawOrigin( m_camera->DrawOrigin() );
@ -524,9 +557,11 @@ public:
if ( m_player != nullptr )
{
if ( m_context->pause && m_context->singleStep > 0 )
if ( m_context->singleStep > 0 )
{
// Stepping takes over from playback, like the transport buttons.
m_context->singleStep = b3MaxInt( 0, m_context->singleStep - 1 );
m_context->pause = true;
if ( b3RecPlayer_IsAtEnd( m_player ) == false )
{
AdvanceOne( m_subStepOnCreate );
@ -567,6 +602,13 @@ public:
return;
}
// Retarget after the frame advances so the follow reads the pose about to be drawn, and before
// the cull box below is taken, since both it and the draw origin hang off the eye.
if ( UpdateFollowCamera() )
{
SetDrawOrigin( m_camera->DrawOrigin() );
}
// Draw the replay world through the same adapter path the live samples use.
b3DebugDraw debugDraw;
MakeDebugDraw( &debugDraw );
@ -576,8 +618,13 @@ public:
// Drive the shared outline highlight from the selection. A shape selection outlines that
// shape alone, a body selection outlines the whole body. Set before the draw so the mask
// is filled by the draw callback.
if ( m_selKind == SelShape )
// is filled by the draw callback. Following already centers the selection, so the outline
// would only wrap and obscure the thing being watched.
if ( m_followSelection )
{
ClearSelection();
}
else if ( m_selKind == SelShape )
{
SetSelectedShape( SelectedShape() );
}
@ -592,8 +639,6 @@ public:
b3World_Draw( m_replayWorldId, &debugDraw, B3_DEFAULT_MASK_BITS );
DrawSelectionHighlight();
// Overlay query geometry and recorded hits on top of the world. The toggle draws every recorded
// query, otherwise just the selected one. Re-resolve the pinned query to this frame so a repeated
// query keeps drawing as the recording steps; it vanishes only on a frame that does not issue it.
@ -680,6 +725,58 @@ public:
FrameRecording();
}
// Where the follow cam should sit this frame. A body, shape or joint selection tracks the body's
// center of mass, the stable point to watch even while a shape spins about it. A query tracks the
// center of its recorded bounds. False when the selection resolves to nothing at this frame, which
// happens before a body spawns or on a frame that does not issue the pinned query.
bool FollowTarget( b3Pos* position ) const
{
if ( m_player == nullptr )
{
return false;
}
if ( m_selKind == SelQuery )
{
int sel = ResolveSelectedQuery();
if ( sel < 0 )
{
return false;
}
b3AABB aabb = b3RecPlayer_GetFrameQuery( m_player, sel ).aabb;
if ( b3IsValidAABB( aabb ) == false )
{
return false;
}
*position = b3ToPos( b3AABB_Center( aabb ) );
return true;
}
b3BodyId body = SelectedBody();
if ( m_selKind == SelNone || b3Body_IsValid( body ) == false )
{
return false;
}
*position = b3Body_GetWorldCenter( body );
return true;
}
// Ride the selection. Returns true when the eye moved, so the caller can relatch the draw origin.
bool UpdateFollowCamera()
{
b3Pos position;
if ( m_followSelection == false || FollowTarget( &position ) == false )
{
return false;
}
m_camera->SetTarget( position );
return true;
}
// Transport row shared by the right panel and the Timeline tab. Play is green, Pause red.
void DrawTransport()
{
@ -687,16 +784,12 @@ public:
if ( ImGui::Button( "|<" ) )
{
b3RecPlayer_SeekFrame( m_player, 0 );
m_replayWorldId = b3RecPlayer_GetWorldId( m_player );
m_frameAccumulator = 0.0f;
SeekTo( 0 );
}
ImGui::SameLine();
if ( ImGui::Button( "<" ) )
{
b3RecPlayer_SeekFrame( m_player, frame - 1 );
m_replayWorldId = b3RecPlayer_GetWorldId( m_player );
m_frameAccumulator = 0.0f;
SeekTo( frame - 1 );
m_context->pause = true;
}
ImGui::SameLine();
@ -730,21 +823,18 @@ public:
if ( m_subStepOnCreate )
{
AdvanceOne( true );
m_frameAccumulator = 0.0f;
}
else
{
b3RecPlayer_SeekFrame( m_player, frame + 1 );
m_replayWorldId = b3RecPlayer_GetWorldId( m_player );
SeekTo( frame + 1 );
}
m_frameAccumulator = 0.0f;
m_context->pause = true;
}
ImGui::SameLine();
if ( ImGui::Button( ">|" ) )
{
b3RecPlayer_SeekFrame( m_player, m_info.frameCount );
m_replayWorldId = b3RecPlayer_GetWorldId( m_player );
m_frameAccumulator = 0.0f;
SeekTo( m_info.frameCount );
}
}
@ -767,6 +857,11 @@ public:
// Overlay every recorded query, not just the one selected in the outline.
ImGui::Checkbox( "Draw All Queries", &m_drawAllQueries );
// Keep the selection centered as the recording plays. Orbit and zoom still aim the view, and the
// cursor is untouched, so picking and the scene tree keep working while it follows. The
// silhouette outline drops while following, since a centered body needs no pointing at.
ImGui::Checkbox( "Follow Selection", &m_followSelection );
// View-only stand-up for recordings authored with +Z as up. The simulation is untouched.
// Reframe on a toggle so the rotated bounds stay centered, matching the fit done on load.
if ( ImGui::Checkbox( "Z-up View", &m_context->viewZUp ) )
@ -953,53 +1048,6 @@ public:
}
}
// Selection overlays drawn on top of the outline highlight: body axes, center of mass, and
// contacts. Joints mark both connected body centers.
void DrawSelectionHighlight()
{
if ( m_selKind == SelShape )
{
b3ShapeId shape = SelectedShape();
if ( b3Shape_IsValid( shape ) == false )
{
return;
}
b3BodyId body = b3Shape_GetBody( shape );
DrawAxes( b3Body_GetTransform( body ), 0.5f );
DrawPoint( b3Body_GetWorldCenter( body ), 8.0f, MakeColor( b3_colorYellow ) );
DrawBodyContacts( body );
}
else if ( m_selKind == SelBody )
{
b3BodyId body = SelectedBody();
if ( b3Body_IsValid( body ) == false )
{
return;
}
DrawAxes( b3Body_GetTransform( body ), 0.5f );
DrawPoint( b3Body_GetWorldCenter( body ), 8.0f, MakeColor( b3_colorYellow ) );
DrawBodyContacts( body );
}
else if ( m_selKind == SelJoint )
{
b3JointId joint = SelectedJoint();
if ( b3Joint_IsValid( joint ) == false )
{
return;
}
b3BodyId a = b3Joint_GetBodyA( joint );
b3BodyId b = b3Joint_GetBodyB( joint );
if ( b3Body_IsValid( a ) )
{
DrawPoint( b3Body_GetWorldCenter( a ), 8.0f, MakeColor( b3_colorMagenta ) );
}
if ( b3Body_IsValid( b ) )
{
DrawPoint( b3Body_GetWorldCenter( b ), 8.0f, MakeColor( b3_colorMagenta ) );
}
}
}
// Left-edge Outline window plus the keyframe-policy popup. The selection detail lives in the right
// info panel (DrawControls), so the tree owns the whole column.
void DrawSampleWindows() override
@ -1157,10 +1205,8 @@ public:
ImGui::PopStyleColor();
if ( clicked )
{
b3RecPlayer_SeekFrame( m_player, row.frame );
m_replayWorldId = b3RecPlayer_GetWorldId( m_player );
SeekTo( row.frame );
m_context->pause = true;
m_frameAccumulator = 0.0f;
m_selKind = SelQuery;
m_selQueryKey = row.key;
FormatQueryLabel( m_selQueryLabel, sizeof( m_selQueryLabel ), row.name, row.id, row.type, row.kindOrdinal );
@ -1663,9 +1709,7 @@ public:
ImGui::PushItemWidth( -FLT_MIN );
if ( ImGui::SliderInt( "##frame", &scrub, 0, m_info.frameCount ) )
{
b3RecPlayer_SeekFrame( m_player, scrub );
m_replayWorldId = b3RecPlayer_GetWorldId( m_player );
m_frameAccumulator = 0.0f;
SeekTo( scrub );
m_context->pause = true;
}
ImGui::PopItemWidth();
@ -1758,6 +1802,11 @@ public:
bool m_drawAllQueries; // overlay every recorded query, not just the selected one
int m_hoverQuery; // outline query row under the cursor, drawn as a transient highlight
// Follow cam. Unlike the third person camera the character samples use, this drives the pivot only.
// The cursor stays free, so picking, the timeline and the scene tree keep working and orbit still
// aims the view.
bool m_followSelection;
// Re-usable buffer for getting all shapes from a body.
std::vector<b3ShapeId> m_shapeBuffer;