Build polish (#18)
Improve build process and documentation. Improved ray versus capsule accuracy. UI improvements
This commit is contained in:
parent
0c8ef46931
commit
69037feaba
19 changed files with 1719 additions and 158 deletions
|
|
@ -6,6 +6,8 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace mapbox {
|
||||
|
||||
namespace util {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
// SPDX-FileCopyrightText: 2026 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// todo get rid of this file
|
||||
// These mappings exist to make porting samples from Box2D easier.
|
||||
|
||||
#pragma once
|
||||
|
||||
|
|
@ -19,6 +19,7 @@ enum
|
|||
KEY_M = SAPP_KEYCODE_M,
|
||||
KEY_O = SAPP_KEYCODE_O,
|
||||
KEY_P = SAPP_KEYCODE_P,
|
||||
KEY_Q = SAPP_KEYCODE_Q,
|
||||
KEY_R = SAPP_KEYCODE_R,
|
||||
KEY_S = SAPP_KEYCODE_S,
|
||||
KEY_T = SAPP_KEYCODE_T,
|
||||
|
|
@ -27,7 +28,6 @@ enum
|
|||
KEY_SPACE = SAPP_KEYCODE_SPACE,
|
||||
KEY_ESCAPE = SAPP_KEYCODE_ESCAPE,
|
||||
KEY_TAB = SAPP_KEYCODE_TAB,
|
||||
KEY_HOME = SAPP_KEYCODE_HOME,
|
||||
KEY_LEFT_SHIFT = SAPP_KEYCODE_LEFT_SHIFT,
|
||||
KEY_LEFT_BRACKET = SAPP_KEYCODE_LEFT_BRACKET,
|
||||
KEY_RIGHT_BRACKET = SAPP_KEYCODE_RIGHT_BRACKET,
|
||||
|
|
|
|||
|
|
@ -49,7 +49,21 @@ static int CompareSamples( const void* a, const void* b )
|
|||
|
||||
static void SortSamples()
|
||||
{
|
||||
// Sorting reorders the table, so recover the replay viewer's slot by identity.
|
||||
SampleCreateFcn* replayFcn = ( g_replayIndex >= 0 ) ? g_sampleEntries[g_replayIndex].CreateFcn : nullptr;
|
||||
qsort( g_sampleEntries, g_sampleCount, sizeof( SampleEntry ), CompareSamples );
|
||||
if ( replayFcn != nullptr )
|
||||
{
|
||||
g_replayIndex = -1;
|
||||
for ( int i = 0; i < g_sampleCount; ++i )
|
||||
{
|
||||
if ( g_sampleEntries[i].CreateFcn == replayFcn )
|
||||
{
|
||||
g_replayIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Single host UI callback fired from inside StartUIFrame: menu bar, panels, and
|
||||
|
|
@ -76,6 +90,9 @@ static void OnInit( void )
|
|||
|
||||
s_context.Load();
|
||||
|
||||
// First run with no settings opens with the controls window up.
|
||||
s_context.showControls = s_context.newUser;
|
||||
|
||||
int cores = (int)std::thread::hardware_concurrency();
|
||||
s_context.workerCount = b3ClampInt( cores / 2, 1, 8 );
|
||||
s_context.windowWidth = sapp_width();
|
||||
|
|
@ -83,9 +100,15 @@ static void OnInit( void )
|
|||
|
||||
SortSamples();
|
||||
|
||||
// A first run with no settings opens straight into the replay viewer.
|
||||
int index = s_context.sampleIndex;
|
||||
if ( s_context.newUser && g_replayIndex >= 0 )
|
||||
{
|
||||
index = g_replayIndex;
|
||||
}
|
||||
|
||||
// --sample N selects a registered sample by sorted index, overriding the
|
||||
// persisted one. Lets a headless --frames run target a specific sample.
|
||||
int index = s_context.sampleIndex;
|
||||
if ( s_sampleOverride >= 0 && s_sampleOverride < g_sampleCount )
|
||||
{
|
||||
index = s_sampleOverride;
|
||||
|
|
@ -96,12 +119,11 @@ static void OnInit( void )
|
|||
|
||||
static void OnEvent( const sapp_event* e )
|
||||
{
|
||||
// Esc quits even with ImGui focus so a text field can't trap the app.
|
||||
if ( e->type == SAPP_EVENTTYPE_KEY_DOWN && e->key_code == SAPP_KEYCODE_ESCAPE )
|
||||
{
|
||||
sapp_request_quit();
|
||||
return;
|
||||
}
|
||||
//if ( e->type == SAPP_EVENTTYPE_KEY_DOWN || e->type == SAPP_EVENTTYPE_CHAR )
|
||||
//{
|
||||
// volatile int dummy = 0;
|
||||
// dummy = 32;
|
||||
//}
|
||||
|
||||
bool uiCaptured = false;
|
||||
if (s_context.camera.m_thirdPerson == false)
|
||||
|
|
@ -142,6 +164,29 @@ static void OnEvent( const sapp_event* e )
|
|||
s_context.showUI = !s_context.showUI;
|
||||
break;
|
||||
|
||||
case KEY_ESCAPE:
|
||||
// Layered cancel. An open picker is an ImGui popup that
|
||||
// already swallowed this. So peel the controls window, then
|
||||
// the selection. Quit lives on Ctrl+Q now.
|
||||
if ( s_context.showControls )
|
||||
{
|
||||
s_context.showControls = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
ClearSelection();
|
||||
}
|
||||
break;
|
||||
|
||||
case KEY_Q:
|
||||
if ( mods & MOD_CTRL )
|
||||
{
|
||||
sapp_request_quit();
|
||||
break;
|
||||
}
|
||||
s_context.sample->Keyboard( e->key_code, ACTION_PRESS, mods );
|
||||
break;
|
||||
|
||||
case KEY_O:
|
||||
if ( mods & MOD_CTRL )
|
||||
{
|
||||
|
|
@ -156,6 +201,9 @@ static void OnEvent( const sapp_event* e )
|
|||
break;
|
||||
|
||||
case KEY_P:
|
||||
// Pause stays on P. Space is not bound here on purpose. Global
|
||||
// keys are dispatched before the sample sees them, so claiming
|
||||
// Space would steal jump from the Mover sample.
|
||||
s_context.pause = !s_context.pause;
|
||||
break;
|
||||
|
||||
|
|
@ -176,7 +224,6 @@ static void OnEvent( const sapp_event* e )
|
|||
break;
|
||||
|
||||
case KEY_F:
|
||||
case KEY_HOME:
|
||||
{
|
||||
// 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
|
||||
|
|
@ -216,6 +263,16 @@ static void OnEvent( const sapp_event* e )
|
|||
SetKeyDown( e->key_code, false );
|
||||
break;
|
||||
|
||||
case SAPP_EVENTTYPE_CHAR:
|
||||
// ? toggles the controls window. Read the typed character so it works
|
||||
// on any layout and a text field can still type a literal ?, since the
|
||||
// picker captures the event before it reaches here.
|
||||
if ( e->char_code == '?' && e->key_repeat == false )
|
||||
{
|
||||
s_context.showControls = !s_context.showControls;
|
||||
}
|
||||
break;
|
||||
|
||||
case SAPP_EVENTTYPE_MOUSE_DOWN:
|
||||
s_context.mouseX = e->mouse_x;
|
||||
s_context.mouseY = e->mouse_y;
|
||||
|
|
@ -294,6 +351,16 @@ static void OnFrame( void )
|
|||
return;
|
||||
}
|
||||
|
||||
// Handle a deferred Replay > Open request before any GPU work this frame.
|
||||
// The native picker blocks on a nested run loop. Running it here (no pass
|
||||
// open, no buffers updated, no ImGui frame begun) keeps it from re-entering
|
||||
// a half-built frame the way it did when invoked from inside the UI callback.
|
||||
if ( s_context.openReplayPicker )
|
||||
{
|
||||
s_context.openReplayPicker = false;
|
||||
OpenReplayFileDialog( &s_context );
|
||||
}
|
||||
|
||||
const float dt = (float)sapp_frame_duration();
|
||||
const int W = sapp_width();
|
||||
const int H = sapp_height();
|
||||
|
|
@ -321,13 +388,6 @@ static void OnFrame( void )
|
|||
SetTransparentDynamic( s_context.transparentDynamic );
|
||||
s_context.sample->ResetText();
|
||||
|
||||
// Pause banner only with the UI up, matching Box2D.
|
||||
if ( s_context.pause && s_context.showUI )
|
||||
{
|
||||
s_context.sample->DrawTextLine( "****PAUSED****" );
|
||||
s_context.sample->DrawTextLine( "" );
|
||||
}
|
||||
|
||||
s_context.sample->Step();
|
||||
s_context.sample->Render();
|
||||
|
||||
|
|
|
|||
|
|
@ -30,9 +30,8 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
// #include <thread>
|
||||
|
||||
#define INFO_PANEL_WIDTH 16.0f
|
||||
#define INFO_PANEL_WIDTH 20.0f
|
||||
|
||||
// Load a file. You must free the character array.
|
||||
static char* ReadFile( int& size, const char* filename )
|
||||
|
|
@ -95,7 +94,6 @@ void SampleContext::Save()
|
|||
fprintf( file, " \"drawShapes\": %s,\n", gd->drawShapes ? "true" : "false" );
|
||||
fprintf( file, " \"drawJoints\": %s,\n", gd->drawJoints ? "true" : "false" );
|
||||
fprintf( file, " \"drawContacts\": %s,\n", gd->drawContacts ? "true" : "false" );
|
||||
fprintf( file, " \"showDiagnostics\": %s,\n", showMetrics ? "true" : "false" );
|
||||
fprintf( file, " \"enableShadows\": %s,\n", enableShadows ? "true" : "false" );
|
||||
fprintf( file, " \"enableGtao\": %s,\n", enableGtao ? "true" : "false" );
|
||||
fprintf( file, " \"gtaoQuality\": %d,\n", gtaoQuality );
|
||||
|
|
@ -134,6 +132,9 @@ void SampleContext::Load()
|
|||
return;
|
||||
}
|
||||
|
||||
// Settings file exists, so it is not a new user.
|
||||
newUser = false;
|
||||
|
||||
jsmn_parser parser;
|
||||
jsmntok_t tokens[MAX_TOKENS];
|
||||
|
||||
|
|
@ -178,11 +179,6 @@ void SampleContext::Load()
|
|||
const char* s = data + tokens[i + 1].start;
|
||||
GetGuiDraw()->drawContacts = strncmp( s, "true", 4 ) == 0;
|
||||
}
|
||||
else if ( jsoneq( data, &tokens[i], "showDiagnostics" ) == 0 )
|
||||
{
|
||||
const char* s = data + tokens[i + 1].start;
|
||||
showMetrics = strncmp( s, "true", 4 ) == 0;
|
||||
}
|
||||
else if ( jsoneq( data, &tokens[i], "enableShadows" ) == 0 )
|
||||
{
|
||||
const char* s = data + tokens[i + 1].start;
|
||||
|
|
@ -1339,6 +1335,25 @@ void SelectSample( SampleContext* context, int selection, bool restart )
|
|||
context->restart = false;
|
||||
}
|
||||
|
||||
void OpenReplayFileDialog( SampleContext* context )
|
||||
{
|
||||
if ( g_replayIndex < 0 )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NFD_Init();
|
||||
nfdu8char_t* outPath = nullptr;
|
||||
nfdu8filteritem_t filter[1] = { { "Box3D recording", "b3rec" } };
|
||||
if ( NFD_OpenDialogU8( &outPath, filter, 1, nullptr ) == NFD_OKAY )
|
||||
{
|
||||
snprintf( context->replayFile, sizeof( context->replayFile ), "%s", outPath );
|
||||
NFD_FreePathU8( outPath );
|
||||
SelectSample( context, g_replayIndex, false );
|
||||
}
|
||||
NFD_Quit();
|
||||
}
|
||||
|
||||
// Subsequence fuzzy match. Returns a score (higher is better) or -1 if the
|
||||
// needle is not a subsequence of the haystack. Bonuses for a prefix match, a
|
||||
// word start, and a contiguous run, so "stack" ranks Pyramid > Stack sensibly.
|
||||
|
|
@ -1672,38 +1687,33 @@ static void DrawMenuBar( SampleContext* context )
|
|||
{
|
||||
if ( ImGui::MenuItem( "Open..." ) )
|
||||
{
|
||||
NFD_Init();
|
||||
nfdu8char_t* outPath = nullptr;
|
||||
nfdu8filteritem_t filter[1] = { { "Box3D recording", "b3rec" } };
|
||||
if ( NFD_OpenDialogU8( &outPath, filter, 1, nullptr ) == NFD_OKAY )
|
||||
{
|
||||
snprintf( context->replayFile, sizeof( context->replayFile ), "%s", outPath );
|
||||
NFD_FreePathU8( outPath );
|
||||
SelectSample( context, g_replayIndex, false );
|
||||
}
|
||||
NFD_Quit();
|
||||
// Defer the native picker to the top of the next frame. Running it
|
||||
// here would block on a nested run loop mid-frame (after the buffers
|
||||
// were uploaded and the ImGui frame began), re-entering the render
|
||||
// loop. See OpenReplayFileDialog and SampleContext::openReplayPicker.
|
||||
context->openReplayPicker = true;
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
static bool showHelp = false;
|
||||
static bool showAbout = false;
|
||||
if ( ImGui::BeginMenu( "Help" ) )
|
||||
{
|
||||
ImGui::MenuItem( "Controls", nullptr, &showHelp );
|
||||
ImGui::MenuItem( "Controls", "?", &context->showControls );
|
||||
ImGui::MenuItem( "About", nullptr, &showAbout );
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
ImGui::EndMainMenuBar();
|
||||
|
||||
if ( showHelp )
|
||||
if ( context->showControls )
|
||||
{
|
||||
ImGui::SetNextWindowPos( { context->camera.m_width * 0.5f, context->camera.m_height * 0.5f }, ImGuiCond_Appearing,
|
||||
ImGui::SetNextWindowPos( { context->camera.m_width * 0.5f, context->camera.m_height * 0.35f }, ImGuiCond_Appearing,
|
||||
{ 0.5f, 0.5f } );
|
||||
ImGui::SetNextWindowSize( { 26.0f * fontSize, 0.0f }, ImGuiCond_Appearing );
|
||||
|
||||
if ( ImGui::Begin( "Controls", &showHelp, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize ) )
|
||||
if ( ImGui::Begin( "Controls", &context->showControls,
|
||||
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize ) )
|
||||
{
|
||||
ImGui::SeparatorText( "Keyboard" );
|
||||
if ( ImGui::BeginTable( "keys", 2, ImGuiTableFlags_SizingFixedFit ) )
|
||||
|
|
@ -1715,17 +1725,21 @@ static void DrawMenuBar( SampleContext* context )
|
|||
DrawRow( "R", "Restart sample" );
|
||||
DrawRow( "[ ]", "Previous / next sample" );
|
||||
DrawRow( "Ctrl+O", "Open sample picker" );
|
||||
DrawRow( "F / Home", "Frame selection / world" );
|
||||
DrawRow( "Esc", "Quit" );
|
||||
DrawRow( "F", "Frame selection / world" );
|
||||
DrawRow( "?", "Show / hide controls" );
|
||||
DrawRow( "Esc", "Cancel / close" );
|
||||
DrawRow( "Ctrl+Q", "Quit" );
|
||||
ImGui::EndTable();
|
||||
}
|
||||
|
||||
ImGui::SeparatorText( "Mouse" );
|
||||
if ( ImGui::BeginTable( "mouse", 2, ImGuiTableFlags_SizingFixedFit ) )
|
||||
{
|
||||
DrawRow( "Left drag", "Move bodies (mouse joint)" );
|
||||
DrawRow( "Left click", "Select body/shape" );
|
||||
DrawRow( "Ctrl + left drag", "Move bodies (mouse joint)" );
|
||||
DrawRow( "Alt + left drag", "Orbit camera" );
|
||||
DrawRow( "Alt + middle drag", "Pan camera" );
|
||||
DrawRow( "Alt + right drag", "Zoom (dolly)" );
|
||||
DrawRow( "Right drag", "Fly look (WASD to move)" );
|
||||
DrawRow( "Scroll", "Zoom" );
|
||||
DrawRow( "Shift + left", "Shoot (Ctrl spin, Alt ragdoll)" );
|
||||
|
|
@ -1847,6 +1861,13 @@ static void DrawSamplePicker( SampleContext* context )
|
|||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
|
||||
// The active search field eats the first Esc, so dismiss here instead of
|
||||
// relying on the popup's default nav close.
|
||||
if ( ImGui::IsKeyPressed( ImGuiKey_Escape, false ) )
|
||||
{
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
}
|
||||
|
|
@ -1870,6 +1891,14 @@ static void DrawInfoPanel( SampleContext* context )
|
|||
ImGui::TextColored( HexColor( b3_colorLightGray ), "%s", entry.Category );
|
||||
ImGui::Separator();
|
||||
|
||||
if ( context->pause )
|
||||
{
|
||||
ImGui::TextColored( HexColor( b3_colorRed ), "PAUSED" );
|
||||
ImGui::SameLine();
|
||||
ImGui::TextDisabled( "(p)" );
|
||||
ImGui::Separator();
|
||||
}
|
||||
|
||||
const float frameMs = (float)( sapp_frame_duration() * 1000.0 );
|
||||
ImGui::TextColored( HexColor( b3_colorSeaGreen ), "%.1f ms", frameMs );
|
||||
ImGui::TextColored( HexColor( b3_colorSeaGreen ), "step %d", context->sample->m_stepCount );
|
||||
|
|
@ -1966,6 +1995,12 @@ static void DrawHud( SampleContext* context )
|
|||
float fontSize = ImGui::GetFontSize();
|
||||
|
||||
DrawScreenStringFormat( 5, (int)( 1.5f * fontSize ), MakeColor( b3_colorYellow ), "%s : %s", entry.Category, entry.Name );
|
||||
|
||||
if ( context->pause )
|
||||
{
|
||||
DrawScreenString( 5, (int)( 3.0f * fontSize ), MakeColor( b3_colorRed ), "****PAUSED****" );
|
||||
}
|
||||
|
||||
DrawScreenStringFormat( 5, context->camera.m_height - (int)( 1.5f * fontSize ), MakeColor( b3_colorSeaGreen ),
|
||||
"%.1f ms step %d", 1000.0f * (float)sapp_frame_duration(), context->sample->m_stepCount );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,6 +74,16 @@ struct SampleContext
|
|||
bool showMetrics = false;
|
||||
bool openSamplePicker = false;
|
||||
|
||||
// Controls help window (Help > Controls, toggled with ?). Promoted to the
|
||||
// context so the key handler can reach it. Seeded from newUser after Load.
|
||||
bool showControls = false;
|
||||
|
||||
// Deferred request from the Replay > Open menu. The native file dialog
|
||||
// blocks on a nested run loop, so it must run outside the frame (top of
|
||||
// OnFrame) rather than mid-frame inside the ImGui callback, which would
|
||||
// re-enter the render loop. See OpenReplayFileDialog.
|
||||
bool openReplayPicker = false;
|
||||
|
||||
// Render toggles fed into FrameInput each frame. They live here, not in
|
||||
// main.cpp, so the Render menu and the frame loop share one source.
|
||||
bool enableShadows = true;
|
||||
|
|
@ -87,6 +97,10 @@ struct SampleContext
|
|||
int debugView = 0;
|
||||
|
||||
int sampleIndex = 0;
|
||||
|
||||
// No settings file found yet. The first run opens the replay viewer with the
|
||||
// controls window up, so there is something to watch and a key to every action.
|
||||
bool newUser = true;
|
||||
};
|
||||
|
||||
class Sample
|
||||
|
|
@ -240,6 +254,11 @@ 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 );
|
||||
|
||||
// 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.
|
||||
void OpenReplayFileDialog( SampleContext* context );
|
||||
|
||||
// The single host UI callback: menu bar, sample picker, info panel, and the
|
||||
// bottom diagnostics drawer.
|
||||
void DrawUI( SampleContext* context );
|
||||
|
|
|
|||
|
|
@ -660,7 +660,7 @@ public:
|
|||
else if ( m_castType == e_sphereCast )
|
||||
{
|
||||
DrawLine( point, head, MakeColor( color1 ) );
|
||||
//DrawWireSphere( transform, &sphere, 32, MakeColorAlpha( colors[i], 0.5f ) );
|
||||
// DrawWireSphere( transform, &sphere, 32, MakeColorAlpha( colors[i], 0.5f ) );
|
||||
DrawSolidSphere( transform, sphere, MakeColorAlpha( colors[i], 0.5f ) );
|
||||
}
|
||||
else if ( m_castType == e_capsuleCast )
|
||||
|
|
@ -1328,6 +1328,250 @@ public:
|
|||
|
||||
static int sampleOverlapWorld = RegisterSample( "Collision", "Overlap World", OverlapWorld::Create );
|
||||
|
||||
// Casts very long rays at a row of shapes to exercise the far-origin accuracy of the ray cast
|
||||
// solvers. Each ray sweeps a small cone over time, so an accurate solver traces a smooth loop of
|
||||
// hit points and normals even when the origin is kilometers away.
|
||||
class LongRayCast : public Sample
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
SHAPE_COUNT = 5,
|
||||
TRAIL_COUNT = 180
|
||||
};
|
||||
|
||||
static Sample* Create( SampleContext* context )
|
||||
{
|
||||
return new LongRayCast( context );
|
||||
}
|
||||
|
||||
explicit LongRayCast( SampleContext* context )
|
||||
: Sample( context )
|
||||
{
|
||||
if ( context->restart == false )
|
||||
{
|
||||
m_camera->SetView( -35.0f, 22.0f, 34.0f, { 0.0f, 1.0f, 0.0f } );
|
||||
}
|
||||
|
||||
b3World_SetGravity( m_worldId, b3Vec3_zero );
|
||||
|
||||
m_hull = b3CreateRock( 1.0f );
|
||||
m_mesh = b3CreateWaveMesh( 8, 8, 0.5f, 0.25f, 0.2f, 0.2f );
|
||||
|
||||
int hfCount = 9;
|
||||
b3Vec3 hfScale = 0.5f * b3Vec3_one;
|
||||
m_heightField = b3CreateWave( hfCount, hfCount, hfScale, 0.08f, 0.16f, false );
|
||||
|
||||
// Aim each ray at a point above its shape so the cone sweeps the hit across the surface.
|
||||
float spacing = 5.0f;
|
||||
float aimHeight = 2.5f;
|
||||
for ( int i = 0; i < SHAPE_COUNT; ++i )
|
||||
{
|
||||
float x = ( i - 2 ) * spacing;
|
||||
m_targets[i] = { x, aimHeight, 0.0f };
|
||||
m_trailNext[i] = 0;
|
||||
m_trailCount[i] = 0;
|
||||
m_failRate[i] = 0.0f;
|
||||
}
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_staticBody;
|
||||
// bodyDef.position.y = 1.0f;
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
|
||||
// Sphere
|
||||
bodyDef.position.x = m_targets[0].x;
|
||||
{
|
||||
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
||||
b3Sphere sphere = { b3Vec3_zero, 1.0f };
|
||||
b3CreateSphereShape( body, &shapeDef, &sphere );
|
||||
}
|
||||
|
||||
// Capsule along the x-axis
|
||||
bodyDef.position.x = m_targets[1].x;
|
||||
{
|
||||
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
||||
b3Capsule capsule = { { -1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f }, 0.7f };
|
||||
b3CreateCapsuleShape( body, &shapeDef, &capsule );
|
||||
}
|
||||
|
||||
// Hull
|
||||
bodyDef.position.x = m_targets[2].x;
|
||||
{
|
||||
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
||||
b3CreateHullShape( body, &shapeDef, m_hull );
|
||||
}
|
||||
|
||||
// Wave mesh, centered on its origin and facing up
|
||||
bodyDef.position.x = m_targets[3].x;
|
||||
{
|
||||
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
||||
b3CreateMeshShape( body, &shapeDef, m_mesh, b3Vec3_one );
|
||||
}
|
||||
|
||||
// Height field grows from a corner, so offset the body to center the patch under the ray
|
||||
{
|
||||
float extentX = hfScale.x * ( hfCount - 1 );
|
||||
float extentZ = hfScale.z * ( hfCount - 1 );
|
||||
bodyDef.position = { m_targets[4].x - 0.5f * extentX, 0.0f, -0.5f * extentZ };
|
||||
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
||||
b3CreateHeightFieldShape( body, &shapeDef, m_heightField );
|
||||
}
|
||||
|
||||
m_rayLengthKilometers = 1.0f;
|
||||
m_coneAngle = 5.0f;
|
||||
m_phase = 0.0f;
|
||||
|
||||
memset( m_trail, 0, sizeof( m_trail ) );
|
||||
}
|
||||
|
||||
~LongRayCast() override
|
||||
{
|
||||
b3DestroyHull( m_hull );
|
||||
b3DestroyMesh( m_mesh );
|
||||
b3DestroyHeightField( m_heightField );
|
||||
}
|
||||
|
||||
bool DrawControls() override
|
||||
{
|
||||
ImGui::SliderFloat( "Ray Length", &m_rayLengthKilometers, 1.0f, 10000.0f, "%.0f km", ImGuiSliderFlags_Logarithmic );
|
||||
ImGui::SliderFloat( "Cone Angle", &m_coneAngle, 0.0f, 12.0f, "%.1f deg" );
|
||||
return true;
|
||||
}
|
||||
|
||||
void Step() override
|
||||
{
|
||||
Sample::Step();
|
||||
|
||||
// Advance the cone so it completes one loop per trail buffer.
|
||||
m_phase += 2.0f * B3_PI / float( TRAIL_COUNT );
|
||||
if ( m_phase > 2.0f * B3_PI )
|
||||
{
|
||||
m_phase -= 2.0f * B3_PI;
|
||||
}
|
||||
|
||||
// Unit direction precessing on a small cone about the up axis.
|
||||
float halfAngle = m_coneAngle * ( B3_PI / 180.0f );
|
||||
b3Vec3 tilted = b3RotateVector( b3MakeQuatFromAxisAngle( b3Vec3_axisX, halfAngle ), b3Vec3_axisY );
|
||||
b3Vec3 coneDir = b3RotateVector( b3MakeQuatFromAxisAngle( b3Vec3_axisY, m_phase ), tilted );
|
||||
|
||||
float reach = 5.0f;
|
||||
float farDistance = 1000.0f * m_rayLengthKilometers;
|
||||
b3QueryFilter filter = b3DefaultQueryFilter();
|
||||
|
||||
for ( int i = 0; i < SHAPE_COUNT; ++i )
|
||||
{
|
||||
// The long ray under test and a short ray on the same line. The short ray is near
|
||||
// enough to be accurate, so it is the ground truth: if it hits where the long ray
|
||||
// misses, the miss is an accuracy failure, not the cone tilting off the shape.
|
||||
CastHit truth = CastAlong( m_targets[i], coneDir, 50.0f, reach, filter );
|
||||
CastHit cast = CastAlong( m_targets[i], coneDir, farDistance, reach, filter );
|
||||
|
||||
float fail = 0.0f;
|
||||
|
||||
if ( cast.hit )
|
||||
{
|
||||
// Color the hit by how far it drifts from the ground truth.
|
||||
float error = truth.hit ? b3Length( cast.point - truth.point ) : 0.0f;
|
||||
b3HexColor color = error < 0.05f ? b3_colorGreen : b3_colorOrange;
|
||||
|
||||
m_trail[i][m_trailNext[i]] = cast.point;
|
||||
m_trailNext[i] = ( m_trailNext[i] + 1 ) % TRAIL_COUNT;
|
||||
if ( m_trailCount[i] < TRAIL_COUNT )
|
||||
{
|
||||
m_trailCount[i] += 1;
|
||||
}
|
||||
|
||||
DrawLine( b3OffsetPos( cast.point, b3MulSV( 3.0f, coneDir ) ), cast.point, MakeColor( b3_colorAqua ) );
|
||||
DrawLine( cast.point, b3OffsetPos( cast.point, b3MulSV( 1.5f, cast.normal ) ), MakeColor( b3_colorYellow ) );
|
||||
DrawPoint( cast.point, 8.0f, MakeColor( color ) );
|
||||
}
|
||||
else if ( truth.hit )
|
||||
{
|
||||
// Accuracy failure: the line does hit, but single precision lost it at distance.
|
||||
// Mark where the hit should have been and slash the empty ray red.
|
||||
fail = 1.0f;
|
||||
b3Pos expected = truth.point;
|
||||
DrawPoint( expected, 14.0f, MakeColor( b3_colorRed ) );
|
||||
DrawLine( b3OffsetPos( expected, b3MulSV( 2.0f, coneDir ) ), b3OffsetPos( expected, b3MulSV( -2.0f, coneDir ) ),
|
||||
MakeColor( b3_colorRed ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Geometric miss: the cone tilted the ray off the shape. Not an accuracy problem.
|
||||
b3Pos aim = m_targets[i];
|
||||
DrawLine( b3OffsetPos( aim, b3MulSV( 2.0f, coneDir ) ), b3OffsetPos( aim, b3MulSV( -4.0f, coneDir ) ),
|
||||
MakeColorAlpha( b3_colorGray, 0.4f ) );
|
||||
}
|
||||
|
||||
m_failRate[i] = 0.95f * m_failRate[i] + 0.05f * fail;
|
||||
|
||||
// Fade the trail from oldest to newest so the loop reads as a path.
|
||||
int start = ( m_trailNext[i] - m_trailCount[i] + TRAIL_COUNT ) % TRAIL_COUNT;
|
||||
for ( int j = 0; j < m_trailCount[i]; ++j )
|
||||
{
|
||||
int index = ( start + j ) % TRAIL_COUNT;
|
||||
float alpha = float( j + 1 ) / float( m_trailCount[i] );
|
||||
DrawPoint( m_trail[i][index], 4.0f, MakeColorAlpha( b3_colorGreen, alpha ) );
|
||||
}
|
||||
}
|
||||
|
||||
DrawTextLine( "Long ray casts" );
|
||||
DrawTextLine( "Origin %.0f km. Green: accurate, Orange: drifting, Red: miss.",
|
||||
m_rayLengthKilometers );
|
||||
DrawTextLine( "Failures: sphere %.0f%% capsule %.0f%% hull %.0f%% mesh %.0f%% hf %.0f%%",
|
||||
100.0f * m_failRate[0], 100.0f * m_failRate[1], 100.0f * m_failRate[2], 100.0f * m_failRate[3],
|
||||
100.0f * m_failRate[4] );
|
||||
}
|
||||
|
||||
struct CastHit
|
||||
{
|
||||
b3Pos point;
|
||||
b3Vec3 normal;
|
||||
bool hit;
|
||||
};
|
||||
|
||||
// Cast a ray that passes through the aim point along a cone direction, starting at the given
|
||||
// distance above it. Returns the closest hit.
|
||||
CastHit CastAlong( b3Pos aim, b3Vec3 coneDir, float distance, float reach, b3QueryFilter filter ) const
|
||||
{
|
||||
b3Pos origin = b3OffsetPos( aim, b3MulSV( distance, coneDir ) );
|
||||
b3Vec3 translation = b3MulSV( -( distance + reach ), coneDir );
|
||||
|
||||
CastContext context = {};
|
||||
context.fractions[0] = FLT_MAX;
|
||||
context.fractions[1] = FLT_MAX;
|
||||
context.fractions[2] = FLT_MAX;
|
||||
b3World_CastRay( m_worldId, origin, translation, filter, RayCastClosestCallback, &context );
|
||||
|
||||
CastHit output = {};
|
||||
output.hit = context.count > 0;
|
||||
if ( output.hit )
|
||||
{
|
||||
output.point = context.points[0];
|
||||
output.normal = context.normals[0];
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
b3HullData* m_hull;
|
||||
b3MeshData* m_mesh;
|
||||
b3HeightFieldData* m_heightField;
|
||||
|
||||
b3Pos m_targets[SHAPE_COUNT];
|
||||
b3Pos m_trail[SHAPE_COUNT][TRAIL_COUNT];
|
||||
int m_trailNext[SHAPE_COUNT];
|
||||
int m_trailCount[SHAPE_COUNT];
|
||||
float m_failRate[SHAPE_COUNT];
|
||||
|
||||
float m_rayLengthKilometers;
|
||||
float m_coneAngle;
|
||||
float m_phase;
|
||||
};
|
||||
|
||||
static int sampleLongRayCast = RegisterSample( "Collision", "Long Ray Cast", LongRayCast::Create );
|
||||
|
||||
class InitialOverlap : public Sample
|
||||
{
|
||||
public:
|
||||
|
|
|
|||
|
|
@ -253,10 +253,8 @@ public:
|
|||
m_camera->SetView( 30.0f, 22.0f, 14.0f, { 0.0f, 2.0f, 0.0f } );
|
||||
}
|
||||
|
||||
// The timeline scrubber lives in the diagnostics drawer, so open it and start paused.
|
||||
m_prevShowMetrics = m_context->showMetrics;
|
||||
m_context->showMetrics = true;
|
||||
m_context->pause = true;
|
||||
m_selectTimelineTab = true;
|
||||
|
||||
// Z-up is a replay-only view choice and has no toggle elsewhere, so restore it on exit
|
||||
|
|
|
|||
Loading…
Reference in a new issue