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

@ -192,6 +192,27 @@ void Camera::SetOrbit( float yawRadians, float pitchRadians, float radius )
RebuildBasisAndView( *this );
}
void Camera::SetTarget( b3Pos target )
{
// The sim->display map is a uniform scale plus an optional quarter turn, so apply it directly
// rather than through the float matrix. In large world mode that keeps the range the pivot is
// double for; a matrix multiply would round it away.
const double s = 1.0 / m_lengthUnitsPerMeter;
m_pivot.x = s * target.x;
if ( m_zUp )
{
m_pivot.y = s * target.z;
m_pivot.z = -s * target.y;
}
else
{
m_pivot.y = s * target.y;
m_pivot.z = s * target.z;
}
RebuildBasisAndView( *this );
}
void Camera::SetView( float yawDegrees, float pitchDegrees, float radius, b3Pos pivot )
{
SetPivot( pivot );
@ -322,6 +343,12 @@ void Camera::OnEvent( const sapp_event* e )
case SAPP_KEYCODE_D:
m_dDown = true;
break;
case SAPP_KEYCODE_Q:
m_qDown = true;
break;
case SAPP_KEYCODE_E:
m_eDown = true;
break;
case SAPP_KEYCODE_LEFT_ALT:
case SAPP_KEYCODE_RIGHT_ALT:
m_altDown = true;
@ -345,6 +372,12 @@ void Camera::OnEvent( const sapp_event* e )
case SAPP_KEYCODE_D:
m_dDown = false;
break;
case SAPP_KEYCODE_Q:
m_qDown = false;
break;
case SAPP_KEYCODE_E:
m_eDown = false;
break;
case SAPP_KEYCODE_LEFT_ALT:
case SAPP_KEYCODE_RIGHT_ALT:
m_altDown = false;
@ -363,6 +396,8 @@ void Camera::OnEvent( const sapp_event* e )
m_aDown = false;
m_sDown = false;
m_dDown = false;
m_qDown = false;
m_eDown = false;
m_altDown = false;
break;
default:
@ -425,6 +460,7 @@ void Camera::Update( float dt, int width, int height )
float wasdF = 0.0f; // +forward = backwards, since forward = pivot->eye
float wasdR = 0.0f;
float wasdU = 0.0f; // Q/E rise and fall, along world up rather than the view basis
if ( m_wDown )
wasdF -= 1.0f;
if ( m_sDown )
@ -433,9 +469,13 @@ void Camera::Update( float dt, int width, int height )
wasdR += 1.0f;
if ( m_aDown )
wasdR -= 1.0f;
if ( m_eDown )
wasdU += 1.0f;
if ( m_qDown )
wasdU -= 1.0f;
b3Pos eye = eyeBefore;
if ( wasdF != 0.0f || wasdR != 0.0f )
if ( wasdF != 0.0f || wasdR != 0.0f || wasdU != 0.0f )
{
// Right = normalize(worldUp x forward), matching Box3D's
// UpdateTransform. worldUp = (0,1,0).
@ -449,9 +489,9 @@ void Camera::Update( float dt, int width, int height )
right.z /= rlen;
}
const float step = m_speed * dt;
eye.x += forward.x * wasdF * step + right.x * wasdR * step;
eye.y += forward.y * wasdF * step + right.y * wasdR * step;
eye.z += forward.z * wasdF * step + right.z * wasdR * step;
eye.x += forward.x * wasdF * step + right.x * wasdR * step + worldUp.x * wasdU * step;
eye.y += forward.y * wasdF * step + right.y * wasdR * step + worldUp.y * wasdU * step;
eye.z += forward.z * wasdF * step + right.z * wasdR * step + worldUp.z * wasdU * step;
}
// Back-derive the pivot so Position() stays consistent and a return

View file

@ -11,6 +11,7 @@
// FLY (right mouse held, no Alt)
// right-drag : FPS look (yaw/pitch the view direction)
// WASD : translate eye along view forward/right at m_speed
// QE : translate eye along world down/up at m_speed
// scroll : tune m_speed
//
// Always
@ -112,11 +113,12 @@ public:
{
m_pivot = pivot;
}
void SetTarget( b3Pos target )
{
m_pivot = target;
}
// Aim at a point given in simulation space: length units and the simulation up axis, mapped
// into the display frame the pivot lives in, then the transform refreshed. Follow cams read
// positions straight from the world, so they land here. Counterpart to DrawOrigin, which maps
// the eye back the other way. SetPivot takes a point already in display space.
void SetTarget( b3Pos target );
// Frame an AABB: keep current yaw/pitch, move pivot to the AABB center,
// and refit radius so the AABB fits in view at the current FOV+aspect.
@ -252,4 +254,6 @@ public:
bool m_aDown = false;
bool m_sDown = false;
bool m_dDown = false;
bool m_qDown = false;
bool m_eDown = false;
};

View file

@ -13,9 +13,11 @@
#include "gui.h"
#include "gfx/draw.h"
#include "gfx/fonts.h"
#include "gfx/projection.h"
#include "gfx/renderer.h"
#include "gfx/text.h"
#include "gfx/world_text.h"
#include "imgui.h"
#include "implot.h"
#include "sokol_app.h"
@ -135,7 +137,6 @@ void InitUI( const sg_environment* env, DrawUiFcn* drawGuiFcn )
desc.sample_count = env->defaults.sample_count;
desc.ini_filename = "imgui.ini";
desc.logger.func = slog_func;
// Skip ImGui's auto-pick so we always get the vector font we add below.
desc.no_default_font = true;
simgui_setup( &desc );
@ -168,9 +169,9 @@ void InitUI( const sg_environment* env, DrawUiFcn* drawGuiFcn )
style.ScaleAllSizes( s_uiScale );
}
// 13px base for the proportional UI font, scaled to a whole physical
// pixel. Rasterizer density is 1.0 (DisplayFramebufferScale 1.0), so the
// atlas bakes at exactly this - floorf keeps it on a whole-pixel size.
// 13px base, scaled to a whole physical pixel. Rasterizer density is 1.0
// (DisplayFramebufferScale 1.0), so the atlas bakes at exactly this -
// floorf keeps it on a whole-pixel size.
style.FontSizeBase = floorf( 13.0f * s_uiScale );
s_uiInitialized = true;
@ -288,12 +289,11 @@ void EndPanel( void )
}
// Text overlay. Drain the per-frame label arena (filled by DrawString /
// DrawScreenString and the Box3D adapter's DrawString callback). World
// entries project to screen pixels with the last rendered camera, screen
// entries pass through their pixel position, and both emit into ImGui's
// background draw list - labels sit on the scene but under any ImGui
// windows. The drain runs in StartUIFrame because the calling app
// guarantees RenderFrame already ran this frame, so GetCameraState
// DrawScreenString and the Box3D adapter's DrawString callback). Screen
// entries pass through their pixel position into ImGui's background draw
// list, so labels sit on the scene but under any ImGui windows. World
// entries belong to the SDF pass. The drain runs in StartUIFrame because the calling
// app guarantees RenderFrame already ran this frame, so GetCameraState
// returns the matrices the scene was actually rasterized with.
static void RenderText()
{
@ -321,6 +321,11 @@ static void RenderText()
for ( int i = 0; i < n; ++i )
{
const TextEntry* e = GetTextAt( i );
if ( e->space == TEXT_SPACE_WORLD )
{
continue;
}
float sx, sy;
if ( !ResolveTextScreenPos( e, cam.view, cam.proj, vpW, vpH, &sx, &sy ) )
{
@ -336,8 +341,12 @@ static void RenderText()
v = 1.0f;
return (uint32_t)( v * 255.0f + 0.5f );
};
const uint32_t col = IM_COL32( byte( e->color.x ), byte( e->color.y ), byte( e->color.z ), byte( e->color.w ) );
dl->AddText( nullptr, 0.0f, ImVec2( origin.x + sx, origin.y + sy ), col, e->text );
const uint32_t alpha = byte( e->color.w );
const uint32_t col = IM_COL32( byte( e->color.x ), byte( e->color.y ), byte( e->color.z ), alpha );
const float x = origin.x + sx;
const float y = origin.y + sy;
dl->AddText( nullptr, 0.0f, ImVec2( x, y ), col, e->text );
}
}