3095 lines
101 KiB
C++
3095 lines
101 KiB
C++
// SPDX-FileCopyrightText: 2026 Erin Catto
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include "gfx/draw.h"
|
|
#include "gfx/keycodes.h"
|
|
#include "imgui.h"
|
|
#include "mesh_loader.h"
|
|
#include "meshoptimizer.h"
|
|
#include "sample.h"
|
|
|
|
#include "box3d/box3d.h"
|
|
|
|
#include <ctype.h>
|
|
#include <float.h>
|
|
#include <iterator>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <vector>
|
|
|
|
class Crash : public Sample
|
|
{
|
|
public:
|
|
explicit Crash( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( 45.0f, 30.0f, 15.0f, { 0.0f, 2.0f, 0.0f } );
|
|
}
|
|
|
|
b3BodyId groundId;
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.position = { 0.0f, -1.0f, 0.0f };
|
|
groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
m_gridMesh = b3CreateGridMesh( 20, 20, 2, 0, true );
|
|
b3CreateMeshShape( groundId, &shapeDef, m_gridMesh, b3Vec3_one );
|
|
}
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.position = { 2.0f, 4.0f, 0.0f };
|
|
m_bodyId1 = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
|
b3CreateHullShape( m_bodyId1, &shapeDef, &box.base );
|
|
|
|
bodyDef.position = { -2.0f, 4.0f, 0.0f };
|
|
m_bodyId2 = b3CreateBody( m_worldId, &bodyDef );
|
|
b3CreateHullShape( m_bodyId2, &shapeDef, &box.base );
|
|
}
|
|
|
|
~Crash() override
|
|
{
|
|
b3DestroyMesh( m_gridMesh );
|
|
}
|
|
|
|
bool DrawControls() override
|
|
{
|
|
if ( ImGui::Button( "Add Joint" ) )
|
|
{
|
|
b3WeldJointDef jointDef = b3DefaultWeldJointDef();
|
|
jointDef.base.bodyIdA = m_bodyId1;
|
|
jointDef.base.bodyIdB = m_bodyId2;
|
|
b3CreateWeldJoint( m_worldId, &jointDef );
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new Crash( context );
|
|
}
|
|
|
|
b3BodyId m_bodyId1;
|
|
b3BodyId m_bodyId2;
|
|
b3MeshData* m_gridMesh;
|
|
};
|
|
|
|
static int sampleCrash = RegisterSample( "Issues", "Crash", Crash::Create );
|
|
|
|
class MultiplePrismatic : public Sample
|
|
{
|
|
public:
|
|
explicit MultiplePrismatic( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( 0.0f, 0.0f, 25.0f, { 0.0f, 5.0f, 0.0f } );
|
|
}
|
|
|
|
b3BodyId groundId;
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
}
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
|
b3PrismaticJointDef jointDef = b3DefaultPrismaticJointDef();
|
|
jointDef.base.bodyIdA = groundId;
|
|
jointDef.base.localFrameA.p = { 0.0f, 0.0f, 0.0f };
|
|
jointDef.base.localFrameB.p = { 0.0f, -0.6f, 0.0f };
|
|
jointDef.base.drawScale = 2.0f;
|
|
jointDef.base.constraintHertz = 240.0f;
|
|
jointDef.lowerTranslation = -6.0f;
|
|
jointDef.upperTranslation = 6.0f;
|
|
jointDef.enableLimit = true;
|
|
|
|
for ( int i = 0; i < 6; ++i )
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.position = { 0.0f, 0.6f + 1.2f * i, 0.0f };
|
|
bodyDef.type = b3_dynamicBody;
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
|
|
|
jointDef.base.bodyIdB = bodyId;
|
|
b3CreatePrismaticJoint( m_worldId, &jointDef );
|
|
|
|
jointDef.base.bodyIdA = bodyId;
|
|
jointDef.base.localFrameA.p = { 0.0f, 0.6f, 0.0f };
|
|
}
|
|
|
|
// huge mouse force
|
|
m_mouseForceScale = 1000000.0f;
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new MultiplePrismatic( context );
|
|
}
|
|
};
|
|
|
|
static int sampleMultiplePrismatic =
|
|
RegisterSample( "Issues", "Multiple Prismatic", MultiplePrismatic::Create );
|
|
|
|
// bad hull SM_Waterfall_MED_Wide_01
|
|
static const b3Vec3 waterfallPoints[] = {
|
|
{ 0.100183107, -498.925385, -1275.39966 }, { 0.100183107, -498.925415, 0.125000000 },
|
|
{ 0.100183107, 486.343750, 0.125000000 }, { 0.100183107, 486.343719, -1275.39966 },
|
|
{ -395.117462, 486.343781, -1462.43750 }, { -395.117462, 486.343750, -96.7426758 },
|
|
{ -395.117462, -498.925415, -96.7424469 }, { -395.117462, -498.925446, -1462.52612 },
|
|
{ -186.979691, 486.294891, -1462.47949 }, { -298.250000, 486.294891, 0.125000000 },
|
|
{ -395.121216, 486.294891, -1462.52612 }, { -186.984360, -498.913361, -1462.48413 },
|
|
{ -298.250000, -498.913361, 0.125000000 },
|
|
};
|
|
|
|
static const b3Vec3 quadPoints[] = {
|
|
{ 100.000000, -142.292389, 130.826111 }, { 99.5354385, -71.3011093, 130.826111 },
|
|
{ 99.5930862, -80.1112213, -100.000000 }, { 100.000000, -142.292389, -100.000000 },
|
|
{ 99.5930862, -80.1112213, 130.826111 },
|
|
};
|
|
|
|
static const b3Vec3 thinBoxPoints[] = {
|
|
{ -11.3861933, -24.2451687, -12.0037909 }, { -11.3889809, -24.2466526, -11.9013014 },
|
|
{ -11.3804407, -24.3151531, -12.0046492 }, { -11.3832273, -24.3166409, -11.9021587 },
|
|
{ -14.4396200, -24.3636723, -12.1324549 }, { -14.4432650, -24.3655701, -12.0299988 },
|
|
{ -14.4356947, -24.4337788, -12.1336164 }, { -14.4393377, -24.4356804, -12.0311594 },
|
|
};
|
|
|
|
// Inline point sets are scaled from centimeters. A null point array is a file in data/hulls.
|
|
struct HullSource
|
|
{
|
|
const char* name;
|
|
const b3Vec3* points;
|
|
int count;
|
|
float scale;
|
|
};
|
|
|
|
static const HullSource hullSources[] = {
|
|
{ "waterfall", waterfallPoints, (int)std::size( waterfallPoints ), 0.01f },
|
|
{ "quad", quadPoints, (int)std::size( quadPoints ), 0.01f },
|
|
{ "thin box", thinBoxPoints, (int)std::size( thinBoxPoints ), 0.01f },
|
|
{ "axe.txt", nullptr, 0, 1.0f },
|
|
};
|
|
|
|
static constexpr int hullSourceCount = (int)std::size( hullSources );
|
|
|
|
// Reads x, y, z triples separated by commas or whitespace. A C source repro is
|
|
// also accepted: only the first initializer list is parsed, so counts and other
|
|
// numbers in the surrounding code are ignored.
|
|
static bool LoadHullPoints( const char* path, std::vector<b3Vec3>* points )
|
|
{
|
|
points->clear();
|
|
|
|
FILE* file = fopen( path, "rb" );
|
|
if ( file == nullptr )
|
|
{
|
|
fprintf( stderr, "Failed to open '%s'\n", path );
|
|
return false;
|
|
}
|
|
|
|
fseek( file, 0, SEEK_END );
|
|
long size = ftell( file );
|
|
fseek( file, 0, SEEK_SET );
|
|
if ( size < 0 )
|
|
{
|
|
fprintf( stderr, "Failed to read '%s'\n", path );
|
|
fclose( file );
|
|
return false;
|
|
}
|
|
|
|
std::vector<char> text( size + 1 );
|
|
size_t readCount = fread( text.data(), 1, size, file );
|
|
fclose( file );
|
|
text[readCount] = '\0';
|
|
|
|
char* begin = text.data();
|
|
char* end = begin + readCount;
|
|
for ( char* c = begin; *c != '\0'; ++c )
|
|
{
|
|
if ( *c != '{' )
|
|
{
|
|
continue;
|
|
}
|
|
|
|
char* prev = c - 1;
|
|
while ( prev >= begin && isspace( (unsigned char)*prev ) )
|
|
{
|
|
--prev;
|
|
}
|
|
|
|
if ( prev < begin || *prev != '=' )
|
|
{
|
|
continue;
|
|
}
|
|
|
|
begin = c + 1;
|
|
int depth = 1;
|
|
for ( end = begin; *end != '\0'; ++end )
|
|
{
|
|
if ( *end == '{' )
|
|
{
|
|
depth += 1;
|
|
}
|
|
else if ( *end == '}' && --depth == 0 )
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
*end = '\0';
|
|
|
|
std::vector<float> values;
|
|
char* cursor = begin;
|
|
while ( *cursor != '\0' )
|
|
{
|
|
char* stop;
|
|
float value = strtof( cursor, &stop );
|
|
if ( stop == cursor )
|
|
{
|
|
cursor += 1;
|
|
continue;
|
|
}
|
|
|
|
values.push_back( value );
|
|
cursor = stop;
|
|
}
|
|
|
|
if ( values.size() % 3 != 0 )
|
|
{
|
|
fprintf( stderr, "'%s' has %d values, not a multiple of 3\n", path, (int)values.size() );
|
|
}
|
|
|
|
int count = (int)values.size() / 3;
|
|
points->resize( count );
|
|
for ( int i = 0; i < count; ++i )
|
|
{
|
|
( *points )[i] = { values[3 * i], values[3 * i + 1], values[3 * i + 2] };
|
|
}
|
|
|
|
return count > 0;
|
|
}
|
|
|
|
class HullCrash : public Sample
|
|
{
|
|
public:
|
|
explicit HullCrash( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
m_hull = nullptr;
|
|
m_sourceIndex = 3;
|
|
m_maxVertexCount = B3_MAX_HULL_VERTICES;
|
|
|
|
LoadPoints();
|
|
CreateHull();
|
|
|
|
if ( m_context->restart == false )
|
|
{
|
|
FrameView();
|
|
}
|
|
}
|
|
|
|
~HullCrash() override
|
|
{
|
|
if ( m_hull != nullptr )
|
|
{
|
|
b3DestroyHull( m_hull );
|
|
}
|
|
}
|
|
|
|
void LoadPoints()
|
|
{
|
|
const HullSource& source = hullSources[m_sourceIndex];
|
|
if ( source.points != nullptr )
|
|
{
|
|
m_points.assign( source.points, source.points + source.count );
|
|
}
|
|
else
|
|
{
|
|
char path[64];
|
|
snprintf( path, sizeof( path ), "data/hulls/%s", source.name );
|
|
LoadHullPoints( path, &m_points );
|
|
}
|
|
|
|
for ( b3Vec3& point : m_points )
|
|
{
|
|
point = source.scale * point;
|
|
}
|
|
|
|
// The reported failure used the point count as the limit
|
|
m_maxVertexCount = b3ClampInt( (int)m_points.size(), 4, B3_MAX_HULL_VERTICES );
|
|
}
|
|
|
|
void CreateHull()
|
|
{
|
|
if ( m_hull != nullptr )
|
|
{
|
|
b3DestroyHull( m_hull );
|
|
m_hull = nullptr;
|
|
}
|
|
|
|
int count = (int)m_points.size();
|
|
if ( count >= 4 )
|
|
{
|
|
m_hull = b3CreateHull( m_points.data(), count, m_maxVertexCount );
|
|
}
|
|
}
|
|
|
|
void FrameView()
|
|
{
|
|
if ( m_points.empty() )
|
|
{
|
|
return;
|
|
}
|
|
|
|
b3AABB bounds = b3MakeAABB( m_points.data(), (int)m_points.size(), 0.0f );
|
|
float radius = b3Length( b3AABB_Extents( bounds ) );
|
|
m_camera->SetView( 0.0f, 15.0f, b3MaxFloat( 3.0f * radius, 1.0f ),
|
|
b3ToPos( b3AABB_Center( bounds ) ) );
|
|
}
|
|
|
|
bool DrawControls() override
|
|
{
|
|
DrawTextLine( "points = %d", (int)m_points.size() );
|
|
|
|
if ( m_hull != nullptr )
|
|
{
|
|
DrawTextLine( "hull: vertices = %d, edges = %d, faces = %d", m_hull->vertexCount,
|
|
m_hull->edgeCount / 2, m_hull->faceCount );
|
|
}
|
|
else
|
|
{
|
|
DrawTextLine( "hull creation failed" );
|
|
}
|
|
|
|
if ( ImGui::BeginCombo( "Source", hullSources[m_sourceIndex].name ) )
|
|
{
|
|
for ( int i = 0; i < hullSourceCount; ++i )
|
|
{
|
|
if ( ImGui::Selectable( hullSources[i].name, i == m_sourceIndex ) )
|
|
{
|
|
m_sourceIndex = i;
|
|
LoadPoints();
|
|
CreateHull();
|
|
FrameView();
|
|
}
|
|
}
|
|
|
|
ImGui::EndCombo();
|
|
}
|
|
|
|
if ( ImGui::SliderInt( "Max Vertices", &m_maxVertexCount, 4, B3_MAX_HULL_VERTICES ) )
|
|
{
|
|
CreateHull();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void Render() override
|
|
{
|
|
if ( m_hull != nullptr )
|
|
{
|
|
DrawHull( b3WorldTransform_identity, m_hull, MakeColor( b3_colorYellow ) );
|
|
}
|
|
else
|
|
{
|
|
for ( const b3Vec3& point : m_points )
|
|
{
|
|
DrawPoint( b3ToPos( point ), 5.0f, MakeColor( b3_colorWhite ) );
|
|
}
|
|
}
|
|
|
|
DrawAxes( b3WorldTransform_identity, 1.0f );
|
|
|
|
Sample::Render();
|
|
}
|
|
|
|
static Sample* Create( SampleContext* sampleContext )
|
|
{
|
|
return new HullCrash( sampleContext );
|
|
}
|
|
|
|
std::vector<b3Vec3> m_points;
|
|
b3HullData* m_hull;
|
|
int m_sourceIndex;
|
|
int m_maxVertexCount;
|
|
};
|
|
|
|
static int sampleHullCrash = RegisterSample( "Issues", "Hull Crash", HullCrash::Create );
|
|
|
|
class ConvexJitter : public Sample
|
|
{
|
|
public:
|
|
explicit ConvexJitter( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( 0.0f, 15.0f, 10.0f, { 0.0f, 2.0f, 0.0f } );
|
|
}
|
|
|
|
AddGroundBox( 10.0f );
|
|
|
|
float s = 0.01f;
|
|
|
|
{
|
|
b3Vec3 b = { -459.292877f, 217.398331f, 1.00115335f };
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.position = { s * b.x, s * b.z + 2.0f, s * b.y };
|
|
bodyDef.rotation = { { 0.0f, -0.707106769f, 0.0f }, 0.707106769f };
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
constexpr int count = 16;
|
|
b3Vec3 points[count];
|
|
points[0] = { -44.8770714, -91.6598053, -1.92012548 };
|
|
points[1] = { -92.5001831, 51.0151291, 15.8006573 };
|
|
points[2] = { -91.0282211, -9.44371605, 15.6148796 };
|
|
points[3] = { 90.2375641, 77.3870087, 15.9356089 };
|
|
points[4] = { -85.5353241, 91.3750992, -1.36629653 };
|
|
points[5] = { 88.9092178, -87.2975464, -1.86754704 };
|
|
points[6] = { 83.7932816, -89.8572235, 15.4168339 };
|
|
points[7] = { 87.0243988, 88.9776535, -1.32423306 };
|
|
points[8] = { -91.6564941, -85.4949493, 15.3782759 };
|
|
points[9] = { -90.2922516, -87.2074127, -1.92012548 };
|
|
points[10] = { -87.2944870, 89.9510498, 15.9215889 };
|
|
points[11] = { 79.2338104, 89.9690781, 15.9724140 };
|
|
points[12] = { -91.6744461, 81.0823212, -1.39959598 };
|
|
points[13] = { 90.3452759, -76.4459610, 15.4588966 };
|
|
points[14] = { -87.4021912, -89.2263107, 15.3677588 };
|
|
points[15] = { 76.3258057, 92.0059967, 1.82873762 };
|
|
|
|
for ( int i = 0; i < count; ++i )
|
|
{
|
|
b3Vec3 p = points[i];
|
|
points[i] = { s * p.x, s * p.z, s * p.y };
|
|
}
|
|
|
|
b3HullData* hull = b3CreateHull( points, count, count );
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, hull );
|
|
b3DestroyHull( hull );
|
|
}
|
|
|
|
{
|
|
b3Vec3 b = { -402.321838f, 157.310364f, 16.8169250f };
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.position = { s * b.x, s * b.z + 2.0f, s * b.y };
|
|
bodyDef.rotation = { { 0.0f, -0.00152086187f, 0.0f }, 0.999998868f };
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
shapeDef.baseMaterial.rollingResistance = 0.1f;
|
|
|
|
constexpr int count = 18;
|
|
b3Vec3 points[count];
|
|
points[0] = { 29.5000000, 17.1488495, 0.175081104 };
|
|
points[1] = { 29.5000000, -17.2990532, 0.125000000 };
|
|
points[2] = { 29.4840164, -17.3057766, 24.0200863 };
|
|
points[3] = { 29.4840164, 17.1648350, 24.1781254 };
|
|
points[4] = { -29.1345520, 17.5529804, 0.125000000 };
|
|
points[5] = { -29.1345520, 17.5529804, 23.7899799 };
|
|
points[6] = { -29.1441040, 16.9679585, 24.3750000 };
|
|
points[7] = { -29.1345520, -17.2990532, 24.3750000 };
|
|
points[8] = { -29.1345520, -17.2990532, 0.175081253 };
|
|
points[9] = { 29.0720215, 17.5529785, 0.125000000 };
|
|
points[10] = { 29.0859070, 17.5629406, 23.8120594 };
|
|
points[11] = { 29.1401348, -17.2990532, 24.3750000 };
|
|
points[12] = { 29.1123581, 16.9722290, 24.4027710 };
|
|
points[13] = { 29.3944912, 17.2543602, 24.1206398 };
|
|
points[14] = { -29.1345520, -17.2990532, 24.0759430 };
|
|
points[15] = { -29.1345520, -16.9722252, 24.4027710 };
|
|
points[16] = { 29.1123619, -16.9722271, 24.4027729 };
|
|
points[17] = { 29.5000000, 17.3429642, 24.0000000 };
|
|
|
|
for ( int i = 0; i < count; ++i )
|
|
{
|
|
b3Vec3 p = points[i];
|
|
points[i] = { s * p.x, s * p.z, s * p.y };
|
|
}
|
|
|
|
b3HullData* hull = b3CreateHull( points, count, count );
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, hull );
|
|
b3DestroyHull( hull );
|
|
}
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new ConvexJitter( context );
|
|
}
|
|
};
|
|
|
|
static int sampleConvexJitter = RegisterSample( "Issues", "Convex Jitter", ConvexJitter::Create );
|
|
|
|
class SBoxMover : public Sample
|
|
{
|
|
public:
|
|
explicit SBoxMover( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( m_context->restart == false )
|
|
{
|
|
m_camera->SetView( 45.0f, 30.0f, 12.0f, b3Pos_zero );
|
|
}
|
|
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.position = { -10.0f, 0.0f, -10.0f };
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
m_heightField = b3CreateGrid( 40, 40, { 0.5f, 1.0f, 0.5f }, false );
|
|
// m_heightField = b3CreateWave( 40, 40, {1.0f, 2.0f, 1.0f}, 0.02f, 0.04f, false );
|
|
b3CreateHeightFieldShape( groundId, &shapeDef, m_heightField );
|
|
|
|
m_gridMesh = b3CreateGridMesh( 40, 40, 0.5f, 1, true );
|
|
// b3CreateMeshShape( groundId, &shapeDef, m_gridMesh, b3Vec3_one );
|
|
}
|
|
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
// m_boxMesh = b3CreateBoxMesh( { 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f }, true );
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
m_boxMesh = b3CreatePlatformMesh( { 0.0f, 0.5f, 0.0f }, 1.0f, 2.0f, 5.0f );
|
|
b3Vec3 scale = b3Vec3_one;
|
|
b3CreateMeshShape( groundId, &shapeDef, m_boxMesh, scale );
|
|
}
|
|
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.position = { 0.0f, 3.5f, 0.0f };
|
|
bodyDef.motionLocks.angularX = true;
|
|
bodyDef.motionLocks.angularY = true;
|
|
bodyDef.motionLocks.angularZ = true;
|
|
bodyDef.enableContactRecycling = false;
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3BoxHull box = b3MakeBoxHull( 0.25f, 1.0f, 0.25f );
|
|
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
|
}
|
|
}
|
|
|
|
~SBoxMover() override
|
|
{
|
|
b3DestroyMesh( m_boxMesh );
|
|
b3DestroyHeightField( m_heightField );
|
|
b3DestroyMesh( m_gridMesh );
|
|
}
|
|
|
|
void Render() override
|
|
{
|
|
Sample::Render();
|
|
b3Transform transform = { { 0.0f, 1.1f, 0.0f }, b3Quat_identity };
|
|
DrawAxes( b3MakeWorldTransform( transform ), 3.0f );
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new SBoxMover( context );
|
|
}
|
|
|
|
b3MeshData* m_boxMesh;
|
|
b3HeightFieldData* m_heightField;
|
|
b3MeshData* m_gridMesh;
|
|
};
|
|
|
|
static int sampleBoxMesh = RegisterSample( "Issues", "s&box mover", SBoxMover::Create );
|
|
|
|
class CapsuleMeshBug : public Sample
|
|
{
|
|
public:
|
|
explicit CapsuleMeshBug( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( m_context->restart == false )
|
|
{
|
|
m_camera->SetView( 20.0f, 10.0f, 30.0f, { 0.0f, 2.0f, 0.0f } );
|
|
}
|
|
|
|
// --- Ground plane ---
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3BoxHull ground = b3MakeBoxHull( 50.0f, 0.1f, 50.0f );
|
|
b3CreateHullShape( body, &shapeDef, &ground.base );
|
|
}
|
|
|
|
// --- Building mesh on top of ground ---
|
|
m_building = CreateMeshData( "data/meshes/building.obj", 1.0f, false, false, true, true );
|
|
if ( m_building != nullptr )
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.position = { 0.0f, 0.1f, 0.0f };
|
|
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3CreateMeshShape( body, &shapeDef, m_building, b3Vec3_one );
|
|
}
|
|
|
|
// --- Locked capsule (same setup as player controller body) ---
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.position = { 0.0f, 4.0f, 10.0f };
|
|
bodyDef.motionLocks.angularX = true;
|
|
bodyDef.motionLocks.angularY = true;
|
|
bodyDef.motionLocks.angularZ = true;
|
|
bodyDef.enableSleep = false;
|
|
bodyDef.enableContactRecycling = false;
|
|
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
shapeDef.baseMaterial.friction = 0.3f;
|
|
shapeDef.baseMaterial.customColor = b3_colorMagenta;
|
|
|
|
b3Capsule capsule = { { 0.0f, -0.5f, 0.0f }, { 0.0f, 0.5f, 0.0f }, 0.3f };
|
|
b3CreateCapsuleShape( body, &shapeDef, &capsule );
|
|
}
|
|
}
|
|
|
|
~CapsuleMeshBug() override
|
|
{
|
|
if ( m_building )
|
|
{
|
|
b3DestroyMesh( m_building );
|
|
}
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new CapsuleMeshBug( context );
|
|
}
|
|
|
|
b3MeshData* m_building = nullptr;
|
|
};
|
|
|
|
static int sampleIndex = RegisterSample( "Issues", "Capsule Mesh", CapsuleMeshBug::Create );
|
|
|
|
// Reproduces s&box rigid body character ghost collisions on a FLAT floor.
|
|
//
|
|
// The s&box player is a fixed rotation dynamic body (zero radius box hull) moved by setting
|
|
// velocity. The floor here is modeled on an s&box map area that ghosts badly: concrete slabs
|
|
// and a run of parallel chamfered beams over deep pits. Every walkable vertex is at exactly
|
|
// y = 0 - there is nothing to climb and nothing to trip on. The only mesh features are AT or
|
|
// BELOW the walkable plane: chamfer facets sloping down from beam tops, pit walls and floors,
|
|
// T-junction seams between tiles tessellated at different resolutions, and a chunk seam where
|
|
// two mesh shapes meet (separate contact pairs, like s&box world chunks).
|
|
//
|
|
// Walking back and forth still launches the body upward (60-190 inch/s at run speed). No shape
|
|
// casts, no step up, no ground snapping - any upward velocity spike is a ghost collision from
|
|
// speculative hull vs triangle contacts against below-plane geometry.
|
|
class SBoxGhostCollisions : public Sample
|
|
{
|
|
public:
|
|
// s&box works in inches: 1 unit = 0.0254 m (40 units per meter)
|
|
static constexpr float SRC = 0.0254f;
|
|
|
|
// Floor layout (s&box inches)
|
|
static constexpr int m_halfLengthU = 256; // strip half length along x
|
|
static constexpr int m_halfWidthU = 64; // strip half width along z
|
|
static constexpr int m_tileSizeU = 32; // slab tile stride
|
|
|
|
// Beam section: beams run along z, the character walks along x across them.
|
|
// Tops are 12 wide with 1.5 chamfers, pits between are 10 wide and 24 deep.
|
|
// The 16 wide hull always spans the 13 gap between flat tops.
|
|
static constexpr float m_beamPitchU = 22.0f;
|
|
static constexpr float m_beamWidthU = 12.0f;
|
|
static constexpr float m_chamferWidthU = 1.5f;
|
|
static constexpr float m_chamferDropU = 1.0f;
|
|
static constexpr float m_pitDepthU = 24.0f;
|
|
static constexpr int m_beamCount = 9;
|
|
static constexpr float m_beamRegion0 = -94.0f; // first beam start
|
|
static constexpr float m_beamRegion1 = 94.0f; // last beam end
|
|
|
|
// Character (s&box player: 16 wide zero radius box hull, 72 tall, mass 500)
|
|
static constexpr float m_bodyHalfWidth = 16.0f * SRC;
|
|
static constexpr float m_bodyHalfHeight = 36.0f * SRC;
|
|
static constexpr float m_characterMass = 500.0f;
|
|
|
|
explicit SBoxGhostCollisions( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( m_context->restart == false )
|
|
{
|
|
m_camera->SetView( 90.0f, 25.0f, 10.0f, { 0.0f, 1.0f, 0.0f } );
|
|
}
|
|
|
|
m_fixWinding = true;
|
|
m_combineChunks = true;
|
|
m_remesh = true;
|
|
m_simplifyErrorMillimeters = 10.0f;
|
|
m_repeatSimplify = true;
|
|
CreateFloor();
|
|
|
|
// Character
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.position = { -m_walkRangeX, m_bodyHalfHeight + 0.1f, 0.0f };
|
|
bodyDef.motionLocks.angularX = true;
|
|
bodyDef.motionLocks.angularY = true;
|
|
bodyDef.motionLocks.angularZ = true;
|
|
bodyDef.enableSleep = false;
|
|
|
|
// With current ghost mitigation this seems unnecessary
|
|
//bodyDef.enableContactRecycling = false;
|
|
|
|
bodyDef.gravityScale = 2.03f; // s&box gravity: 800 inch/s^2
|
|
bodyDef.name = "character";
|
|
bodyDef.safetyFactor = 0.05f;
|
|
m_characterId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
shapeDef.baseMaterial.friction = 0.0f;
|
|
shapeDef.baseMaterial.restitution = 0.0f;
|
|
// shapeDef.baseMaterial.customColor = b3_colorLimeGreen;
|
|
|
|
float volume = 8.0f * m_bodyHalfWidth * m_bodyHalfHeight * m_bodyHalfWidth;
|
|
shapeDef.density = m_characterMass / volume;
|
|
|
|
// With current ghost mitigation this seems unnecessary
|
|
//shapeDef.enableSpeculativeContact = false;
|
|
|
|
b3BoxHull box = b3MakeBoxHull( m_bodyHalfWidth, m_bodyHalfHeight, m_bodyHalfWidth );
|
|
b3CreateHullShape( m_characterId, &shapeDef, &box.base );
|
|
}
|
|
|
|
m_walkDirectionX = 1.0f;
|
|
m_walkDirectionZ = 1.0f;
|
|
m_walkSpeedX = 350.0f * SRC; // s&box run speed
|
|
m_walkSpeedZ = 20.0f * SRC;
|
|
m_launchCount = 0;
|
|
m_maxLaunchSpeed = 0.0f;
|
|
m_launchMarkerCount = 0;
|
|
m_wasLaunched = false;
|
|
|
|
m_stepWhilePaused = true;
|
|
}
|
|
|
|
~SBoxGhostCollisions() override
|
|
{
|
|
for ( int i = 0; i < 2; ++i )
|
|
{
|
|
if ( m_chunkMesh[i] != nullptr )
|
|
{
|
|
b3DestroyMesh( m_chunkMesh[i] );
|
|
}
|
|
}
|
|
}
|
|
|
|
void CreateFloor()
|
|
{
|
|
m_chunkTriangleCount[1] = 0;
|
|
m_simplifyPassCount[1] = 0;
|
|
|
|
if ( m_combineChunks )
|
|
{
|
|
CreateFloorChunk( 0, -(float)m_halfLengthU, (float)m_halfLengthU );
|
|
return;
|
|
}
|
|
|
|
// Two chunks meeting at x = 0, each its own body and mesh shape, so seam contacts live
|
|
// in separate contact pairs like s&box world mesh chunks. A beam top straddles the seam.
|
|
CreateFloorChunk( 0, -(float)m_halfLengthU, 0.0f );
|
|
CreateFloorChunk( 1, 0.0f, (float)m_halfLengthU );
|
|
}
|
|
|
|
void DestroyFloor()
|
|
{
|
|
for ( int i = 0; i < 2; ++i )
|
|
{
|
|
if ( B3_IS_NON_NULL( m_chunkBodyIds[i] ) )
|
|
{
|
|
b3DestroyBody( m_chunkBodyIds[i] );
|
|
b3DestroyMesh( m_chunkMesh[i] );
|
|
}
|
|
|
|
m_chunkBodyIds[i] = b3_nullBodyId;
|
|
m_chunkMesh[i] = nullptr;
|
|
}
|
|
}
|
|
|
|
// Deterministic integer hash for tile tessellation selection
|
|
static uint32_t Hash( uint32_t x )
|
|
{
|
|
x ^= x >> 16;
|
|
x *= 0x7feb352du;
|
|
x ^= x >> 15;
|
|
x *= 0x846ca68bu;
|
|
x ^= x >> 16;
|
|
return x;
|
|
}
|
|
|
|
static void EmitTriangle( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices,
|
|
b3Vec3 a, b3Vec3 b, b3Vec3 c )
|
|
{
|
|
int32_t base = (int32_t)vertices.size();
|
|
vertices.push_back( a );
|
|
vertices.push_back( b );
|
|
vertices.push_back( c );
|
|
indices.push_back( base );
|
|
indices.push_back( base + 1 );
|
|
indices.push_back( base + 2 );
|
|
}
|
|
|
|
// Horizontal patch at height y spanning [x0,x1]x[z0,z1] (inches), normal +y
|
|
static void EmitPatch( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices, float x0,
|
|
float x1, float z0, float z1, float y, float cell )
|
|
{
|
|
if ( x1 - x0 < 0.01f )
|
|
{
|
|
return;
|
|
}
|
|
|
|
int countX = (int)( ( x1 - x0 ) / cell + 0.99f );
|
|
int countZ = (int)( ( z1 - z0 ) / cell + 0.99f );
|
|
|
|
for ( int ix = 0; ix < countX; ++ix )
|
|
{
|
|
for ( int iz = 0; iz < countZ; ++iz )
|
|
{
|
|
float cx0 = x0 + ( x1 - x0 ) * (float)ix / (float)countX;
|
|
float cx1 = x0 + ( x1 - x0 ) * (float)( ix + 1 ) / (float)countX;
|
|
float cz0 = z0 + ( z1 - z0 ) * (float)iz / (float)countZ;
|
|
float cz1 = z0 + ( z1 - z0 ) * (float)( iz + 1 ) / (float)countZ;
|
|
|
|
b3Vec3 a = { SRC * cx0, SRC * y, SRC * cz0 };
|
|
b3Vec3 b = { SRC * cx1, SRC * y, SRC * cz0 };
|
|
b3Vec3 c = { SRC * cx1, SRC * y, SRC * cz1 };
|
|
b3Vec3 d = { SRC * cx0, SRC * y, SRC * cz1 };
|
|
|
|
// Alternate the split diagonal like typical cooked map data
|
|
if ( ( ix + iz ) & 1 )
|
|
{
|
|
EmitTriangle( vertices, indices, a, d, c );
|
|
EmitTriangle( vertices, indices, a, c, b );
|
|
}
|
|
else
|
|
{
|
|
EmitTriangle( vertices, indices, a, d, b );
|
|
EmitTriangle( vertices, indices, b, d, c );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sloped strip from edge (xLow, yLow) to edge (xHigh, yHigh) spanning the full z width
|
|
static void EmitSlope( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices, float xLow,
|
|
float yLow, float xHigh, float yHigh, float zCell, bool fixWinding )
|
|
{
|
|
// The original winding only faces out for a slope rising toward +x
|
|
bool flip = fixWinding && xHigh < xLow;
|
|
|
|
int countZ = (int)( 2.0f * (float)m_halfWidthU / zCell + 0.99f );
|
|
for ( int iz = 0; iz < countZ; ++iz )
|
|
{
|
|
float z0 =
|
|
-(float)m_halfWidthU + 2.0f * (float)m_halfWidthU * (float)iz / (float)countZ;
|
|
float z1 = -(float)m_halfWidthU +
|
|
2.0f * (float)m_halfWidthU * (float)( iz + 1 ) / (float)countZ;
|
|
|
|
b3Vec3 l0 = { SRC * xLow, SRC * yLow, SRC * z0 };
|
|
b3Vec3 l1 = { SRC * xLow, SRC * yLow, SRC * z1 };
|
|
b3Vec3 h0 = { SRC * xHigh, SRC * yHigh, SRC * z0 };
|
|
b3Vec3 h1 = { SRC * xHigh, SRC * yHigh, SRC * z1 };
|
|
|
|
if ( flip )
|
|
{
|
|
EmitTriangle( vertices, indices, l0, h1, l1 );
|
|
EmitTriangle( vertices, indices, l0, h0, h1 );
|
|
}
|
|
else
|
|
{
|
|
EmitTriangle( vertices, indices, l0, l1, h1 );
|
|
EmitTriangle( vertices, indices, l0, h1, h0 );
|
|
}
|
|
}
|
|
}
|
|
|
|
// Vertical wall at x from y0 (bottom) to y1 (top). facing = +1 faces +x, -1 faces -x
|
|
static void EmitWall( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices, float x,
|
|
float y0, float y1, int facing, float zCell, bool fixWinding )
|
|
{
|
|
// The original winding faced every wall into the solid
|
|
bool faceNegativeX = fixWinding ? facing < 0 : facing > 0;
|
|
|
|
int countZ = (int)( 2.0f * (float)m_halfWidthU / zCell + 0.99f );
|
|
for ( int iz = 0; iz < countZ; ++iz )
|
|
{
|
|
float z0 =
|
|
-(float)m_halfWidthU + 2.0f * (float)m_halfWidthU * (float)iz / (float)countZ;
|
|
float z1 = -(float)m_halfWidthU +
|
|
2.0f * (float)m_halfWidthU * (float)( iz + 1 ) / (float)countZ;
|
|
|
|
b3Vec3 b0 = { SRC * x, SRC * y0, SRC * z0 };
|
|
b3Vec3 b1 = { SRC * x, SRC * y0, SRC * z1 };
|
|
b3Vec3 t0 = { SRC * x, SRC * y1, SRC * z0 };
|
|
b3Vec3 t1 = { SRC * x, SRC * y1, SRC * z1 };
|
|
|
|
if ( faceNegativeX )
|
|
{
|
|
EmitTriangle( vertices, indices, b0, b1, t1 );
|
|
EmitTriangle( vertices, indices, b0, t1, t0 );
|
|
}
|
|
else
|
|
{
|
|
EmitTriangle( vertices, indices, b0, t0, t1 );
|
|
EmitTriangle( vertices, indices, b0, t1, b1 );
|
|
}
|
|
}
|
|
}
|
|
|
|
// Clip [a0,a1] to [c0,c1]
|
|
static bool ClipSpan( float a0, float a1, float c0, float c1, float* o0, float* o1 )
|
|
{
|
|
*o0 = a0 > c0 ? a0 : c0;
|
|
*o1 = a1 < c1 ? a1 : c1;
|
|
return *o1 - *o0 > 0.01f;
|
|
}
|
|
|
|
// Weld exact duplicates, then let meshoptimizer collapse every edge that leaves the surface in
|
|
// place. A t-junction vertex sits on an open border, so it can slide along that border onto a
|
|
// corner, which closes the seam. Flat collapses still cost a little from the simplifier's
|
|
// vertex regularization, so the error limit has to clear that while staying far below the inch
|
|
// scale cost of collapsing a real crease. Regularize options raise that cost. Vertex kinds are
|
|
// fixed per call, so a corner locked by a t-junction stays locked after its seam closes.
|
|
// Repeating reclassifies, but the error limit applies per pass, so deviation can accumulate.
|
|
// Winding is not repaired. Returns the number of passes.
|
|
static int Remesh( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices,
|
|
float errorLimit, unsigned int options, bool repeat )
|
|
{
|
|
size_t indexCount = indices.size();
|
|
const unsigned int* sourceIndices = reinterpret_cast<const unsigned int*>( indices.data() );
|
|
|
|
std::vector<unsigned int> remap( vertices.size() );
|
|
size_t vertexCount =
|
|
meshopt_generateVertexRemap( remap.data(), sourceIndices, indexCount, vertices.data(),
|
|
vertices.size(), sizeof( b3Vec3 ) );
|
|
|
|
std::vector<unsigned int> welded( indexCount );
|
|
meshopt_remapIndexBuffer( welded.data(), sourceIndices, indexCount, remap.data() );
|
|
|
|
std::vector<b3Vec3> positions( vertexCount );
|
|
meshopt_remapVertexBuffer( positions.data(), vertices.data(), vertices.size(),
|
|
sizeof( b3Vec3 ), remap.data() );
|
|
|
|
std::vector<unsigned int> simplified( indexCount );
|
|
int passCount = 0;
|
|
for ( ;; )
|
|
{
|
|
size_t simplifiedCount = meshopt_simplify(
|
|
simplified.data(), welded.data(), welded.size(), &positions[0].x, vertexCount,
|
|
sizeof( b3Vec3 ), 0, errorLimit, meshopt_SimplifyErrorAbsolute | options, nullptr );
|
|
passCount += 1;
|
|
|
|
bool reduced = simplifiedCount < welded.size();
|
|
welded.assign( simplified.begin(), simplified.begin() + simplifiedCount );
|
|
|
|
if ( repeat == false || reduced == false )
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
size_t usedCount =
|
|
meshopt_optimizeVertexFetch( positions.data(), welded.data(), welded.size(),
|
|
positions.data(), vertexCount, sizeof( b3Vec3 ) );
|
|
positions.resize( usedCount );
|
|
|
|
vertices = positions;
|
|
indices.assign( welded.begin(), welded.end() );
|
|
return passCount;
|
|
}
|
|
|
|
void CreateFloorChunk( int chunk, float x0U, float x1U )
|
|
{
|
|
std::vector<b3Vec3> vertices;
|
|
std::vector<int32_t> indices;
|
|
|
|
float s0, s1;
|
|
|
|
// --- Concrete slabs at y = 0 outside the beam region ---
|
|
// Tiles tessellate at a hash-picked resolution so neighbors meet with T-junctions,
|
|
// like cooked s&box map collision.
|
|
float slabSpans[2][2] = { { -(float)m_halfLengthU, m_beamRegion0 },
|
|
{ m_beamRegion1, (float)m_halfLengthU } };
|
|
for ( int i = 0; i < 2; ++i )
|
|
{
|
|
if ( ClipSpan( slabSpans[i][0], slabSpans[i][1], x0U, x1U, &s0, &s1 ) == false )
|
|
{
|
|
continue;
|
|
}
|
|
|
|
for ( float tx = s0; tx < s1; tx += (float)m_tileSizeU )
|
|
{
|
|
float tx1 = b3MinFloat( tx + (float)m_tileSizeU, s1 );
|
|
for ( int tz = -m_halfWidthU; tz < m_halfWidthU; tz += m_tileSizeU )
|
|
{
|
|
// Seeded by side rather than chunk so combined and split floors tessellate the
|
|
// same
|
|
uint32_t side = tx < 0.0f ? 0u : 1u;
|
|
uint32_t h =
|
|
Hash( (uint32_t)( (int)tx * 73856093 ) ^ (uint32_t)( tz * 19349663 ) ^
|
|
(uint32_t)( side * 2654435761u ) );
|
|
float cells[3] = { 4.0f, 8.0f, 16.0f };
|
|
EmitPatch( vertices, indices, tx, tx1, (float)tz, (float)( tz + m_tileSizeU ),
|
|
0.0f, cells[h % 3] );
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Beam section: flat tops at y = 0, chamfers dropping to pits ---
|
|
float pitTop = -m_chamferDropU;
|
|
float pitBottom = -m_pitDepthU;
|
|
|
|
// Coarse walls and pit floors leave t-junctions against the 8 inch chamfer cells
|
|
float wellCell = 16.0f;
|
|
|
|
for ( int k = 0; k < m_beamCount; ++k )
|
|
{
|
|
float bx = m_beamRegion0 + m_beamPitchU * (float)k;
|
|
bool pitLeft = k > 0;
|
|
bool pitRight = k < m_beamCount - 1;
|
|
|
|
// Flat top (flush with the slab on outer sides)
|
|
float top0 = pitLeft ? bx + m_chamferWidthU : bx;
|
|
float top1 = pitRight ? bx + m_beamWidthU - m_chamferWidthU : bx + m_beamWidthU;
|
|
if ( ClipSpan( top0, top1, x0U, x1U, &s0, &s1 ) )
|
|
{
|
|
EmitPatch( vertices, indices, s0, s1, -(float)m_halfWidthU, (float)m_halfWidthU,
|
|
0.0f, 8.0f );
|
|
}
|
|
|
|
// Chamfers sloping below the walkable plane
|
|
if ( pitLeft && bx >= x0U && bx < x1U )
|
|
{
|
|
EmitSlope( vertices, indices, bx, pitTop, bx + m_chamferWidthU, 0.0f, 8.0f,
|
|
m_fixWinding );
|
|
}
|
|
if ( pitRight && bx + m_beamWidthU > x0U && bx + m_beamWidthU <= x1U )
|
|
{
|
|
EmitSlope( vertices, indices, bx + m_beamWidthU, pitTop,
|
|
bx + m_beamWidthU - m_chamferWidthU, 0.0f, 8.0f, m_fixWinding );
|
|
}
|
|
|
|
// Pit to the right of this beam
|
|
if ( pitRight )
|
|
{
|
|
float pitL = bx + m_beamWidthU;
|
|
float pitR = bx + m_beamPitchU;
|
|
if ( pitL >= x0U && pitL < x1U )
|
|
{
|
|
EmitWall( vertices, indices, pitL, pitBottom, pitTop, +1, wellCell,
|
|
m_fixWinding );
|
|
}
|
|
if ( pitR > x0U && pitR <= x1U )
|
|
{
|
|
EmitWall( vertices, indices, pitR, pitBottom, pitTop, -1, wellCell,
|
|
m_fixWinding );
|
|
}
|
|
if ( ClipSpan( pitL, pitR, x0U, x1U, &s0, &s1 ) )
|
|
{
|
|
EmitPatch( vertices, indices, s0, s1, -(float)m_halfWidthU, (float)m_halfWidthU,
|
|
pitBottom, wellCell );
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( m_remesh )
|
|
{
|
|
m_simplifyPassCount[chunk] = Remesh(
|
|
vertices, indices, 0.001f * m_simplifyErrorMillimeters, 0, m_repeatSimplify );
|
|
}
|
|
|
|
m_chunkTriangleCount[chunk] = (int)( indices.size() / 3 );
|
|
|
|
b3MeshDef meshDef = {};
|
|
meshDef.vertices = vertices.data();
|
|
meshDef.indices = indices.data();
|
|
meshDef.vertexCount = (int)vertices.size();
|
|
meshDef.triangleCount = (int)( indices.size() / 3 );
|
|
meshDef.weldVertices = true;
|
|
meshDef.weldTolerance = 0.005f; // == B3_LINEAR_SLOP, same as s&box
|
|
meshDef.identifyEdges = true;
|
|
|
|
m_chunkMesh[chunk] = b3CreateMesh( &meshDef, nullptr, 0 );
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
m_chunkBodyIds[chunk] = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3CreateMeshShape( m_chunkBodyIds[chunk], &shapeDef, m_chunkMesh[chunk], b3Vec3_one );
|
|
}
|
|
|
|
void Step() override
|
|
{
|
|
// Drive the character with pure velocity control: keep the solver's vertical velocity,
|
|
// set the horizontal velocity. This is how the s&box player controller moves.
|
|
b3Pos position = b3Body_GetPosition( m_characterId );
|
|
if ( position.x > m_walkRangeX )
|
|
{
|
|
m_walkDirectionX = -1.0f;
|
|
}
|
|
else if ( position.x < -m_walkRangeX )
|
|
{
|
|
m_walkDirectionX = 1.0f;
|
|
}
|
|
|
|
if ( position.z > m_walkRangeZ )
|
|
{
|
|
m_walkDirectionZ = -1.0f;
|
|
}
|
|
else if ( position.z < -m_walkRangeZ )
|
|
{
|
|
m_walkDirectionZ = 1.0f;
|
|
}
|
|
|
|
b3Vec3 velocity = b3Body_GetLinearVelocity( m_characterId );
|
|
velocity.x = m_walkDirectionX * m_walkSpeedX;
|
|
velocity.z = m_walkDirectionZ * m_walkSpeedZ;
|
|
b3Body_SetLinearVelocity( m_characterId, velocity );
|
|
|
|
Sample::Step();
|
|
|
|
if ( m_didStep )
|
|
{
|
|
// The walkable plane is exactly y = 0, so the grounded body center never rises above
|
|
// rest height. Any upward velocity spike while grounded is a ghost collision: there
|
|
// is nothing to climb and nothing to bounce off.
|
|
position = b3Body_GetPosition( m_characterId );
|
|
velocity = b3Body_GetLinearVelocity( m_characterId );
|
|
|
|
bool grounded = position.y < m_bodyHalfHeight + 0.01f + 4.0f * SRC;
|
|
bool launched = velocity.y > m_launchThreshold;
|
|
|
|
if ( grounded && launched && m_wasLaunched == false )
|
|
{
|
|
m_launchCount += 1;
|
|
m_maxLaunchSpeed = b3MaxFloat( m_maxLaunchSpeed, velocity.y );
|
|
|
|
if ( m_launchMarkerCount < m_markerCapacity )
|
|
{
|
|
m_launchMarkers[m_launchMarkerCount] = position;
|
|
m_launchMarkerCount += 1;
|
|
}
|
|
}
|
|
|
|
m_wasLaunched = launched;
|
|
}
|
|
|
|
for ( int i = 0; i < m_launchMarkerCount; ++i )
|
|
{
|
|
DrawPoint( m_launchMarkers[i], 8.0f, MakeColor( b3_colorRed ) );
|
|
}
|
|
|
|
b3Vec3 currentVelocity = b3Body_GetLinearVelocity( m_characterId );
|
|
DrawTextLine( "ghost launches: %d, worst: %.2f m/s (%.0f inch/s)", m_launchCount,
|
|
m_maxLaunchSpeed, m_maxLaunchSpeed / SRC );
|
|
DrawTextLine( "vertical velocity: %.2f m/s", currentVelocity.y );
|
|
if ( m_combineChunks )
|
|
{
|
|
DrawTextLine( "floor triangles: %d", m_chunkTriangleCount[0] );
|
|
}
|
|
else
|
|
{
|
|
DrawTextLine( "floor triangles: %d (chunks %d + %d)",
|
|
m_chunkTriangleCount[0] + m_chunkTriangleCount[1],
|
|
m_chunkTriangleCount[0], m_chunkTriangleCount[1] );
|
|
}
|
|
|
|
if ( m_remesh )
|
|
{
|
|
DrawTextLine( m_combineChunks ? "simplify passes: %d" : "simplify passes: %d + %d",
|
|
m_simplifyPassCount[0], m_simplifyPassCount[1] );
|
|
}
|
|
}
|
|
|
|
bool DrawControls() override
|
|
{
|
|
float speedUX = m_walkSpeedX / SRC;
|
|
if ( ImGui::SliderFloat( "Walk Speed X (inch/s)", &speedUX, 100.0f, 400.0f, "%.0f" ) )
|
|
{
|
|
m_walkSpeedX = speedUX * SRC;
|
|
}
|
|
|
|
float speedUZ = m_walkSpeedZ / SRC;
|
|
if ( ImGui::SliderFloat( "Walk Speed Z (inch/s)", &speedUZ, 10.0f, 100.0f, "%.0f" ) )
|
|
{
|
|
m_walkSpeedZ = speedUZ * SRC;
|
|
}
|
|
|
|
bool rebuild = ImGui::Checkbox( "Fix Winding", &m_fixWinding );
|
|
rebuild = ImGui::Checkbox( "Combine Chunks", &m_combineChunks ) || rebuild;
|
|
rebuild = ImGui::Checkbox( "Remesh (meshoptimizer)", &m_remesh ) || rebuild;
|
|
|
|
ImGui::SliderFloat( "Simplify Error (mm)", &m_simplifyErrorMillimeters, 0.1f, 50.0f, "%.1f",
|
|
ImGuiSliderFlags_Logarithmic );
|
|
rebuild = ( ImGui::IsItemDeactivatedAfterEdit() && m_remesh ) || rebuild;
|
|
|
|
rebuild =
|
|
( ImGui::Checkbox( "Repeat Simplify", &m_repeatSimplify ) && m_remesh ) || rebuild;
|
|
|
|
if ( rebuild )
|
|
{
|
|
DestroyFloor();
|
|
CreateFloor();
|
|
}
|
|
|
|
if ( ImGui::Button( "Reset Counters" ) || rebuild )
|
|
{
|
|
m_launchCount = 0;
|
|
m_maxLaunchSpeed = 0.0f;
|
|
m_launchMarkerCount = 0;
|
|
}
|
|
|
|
ImGui::Text( "Launches: %d", m_launchCount );
|
|
return true;
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new SBoxGhostCollisions( context );
|
|
}
|
|
|
|
static constexpr float m_walkRangeX = 3.5f; // turn around beyond +/- this x (meters)
|
|
static constexpr float m_walkRangeZ = 0.5f; // turn around beyond +/- this x (meters)
|
|
static constexpr float m_launchThreshold =
|
|
0.5f; // upward m/s counted as a ghost launch (~20 inch/s)
|
|
static constexpr int m_markerCapacity = 64;
|
|
|
|
b3MeshData* m_chunkMesh[2] = {};
|
|
b3BodyId m_chunkBodyIds[2] = { b3_nullBodyId, b3_nullBodyId };
|
|
bool m_fixWinding;
|
|
bool m_combineChunks;
|
|
bool m_remesh;
|
|
float m_simplifyErrorMillimeters;
|
|
bool m_repeatSimplify;
|
|
int m_simplifyPassCount[2] = {};
|
|
int m_chunkTriangleCount[2] = {};
|
|
b3BodyId m_characterId;
|
|
float m_walkDirectionX;
|
|
float m_walkDirectionZ;
|
|
float m_walkSpeedX;
|
|
float m_walkSpeedZ;
|
|
int m_launchCount;
|
|
float m_maxLaunchSpeed;
|
|
bool m_wasLaunched;
|
|
b3Pos m_launchMarkers[m_markerCapacity];
|
|
int m_launchMarkerCount;
|
|
};
|
|
|
|
static int sampleSBoxGhostCollisions =
|
|
RegisterSample( "Issues", "s&box Ghost Collisions", SBoxGhostCollisions::Create );
|
|
|
|
static const b3Vec3 s_metalWheel1Verts[317] = {
|
|
{ 0.010279f, 0.086341f, 0.051768f }, { -0.010314f, -0.084708f, 0.051804f },
|
|
{ 0.010279f, -0.084708f, 0.051768f }, { -0.010314f, 0.086341f, 0.051804f },
|
|
{ 0.029138f, -0.084708f, -0.043911f }, { 0.043724f, -0.084708f, -0.029376f },
|
|
{ 0.010098f, -0.084708f, -0.051759f }, { -0.010494f, -0.084708f, -0.051723f },
|
|
{ 0.051638f, -0.084708f, -0.010364f }, { 0.051674f, -0.084708f, 0.010229f },
|
|
{ 0.043827f, -0.084708f, 0.029268f }, { 0.029291f, -0.084708f, 0.043855f },
|
|
{ -0.029353f, -0.084708f, 0.043957f }, { -0.051889f, -0.084708f, -0.010183f },
|
|
{ -0.051853f, -0.084708f, 0.010409f }, { -0.043939f, -0.084708f, 0.029421f },
|
|
{ -0.029506f, -0.084708f, -0.043809f }, { -0.044042f, -0.084708f, -0.029222f },
|
|
{ -0.029353f, 0.086341f, 0.043957f }, { 0.043724f, 0.086341f, -0.029376f },
|
|
{ 0.029138f, 0.086341f, -0.043911f }, { 0.010098f, 0.086341f, -0.051759f },
|
|
{ 0.051638f, 0.086341f, -0.010364f }, { 0.051674f, 0.086341f, 0.010229f },
|
|
{ -0.044042f, 0.086341f, -0.029222f }, { 0.043827f, 0.086341f, 0.029268f },
|
|
{ -0.051889f, 0.086341f, -0.010183f }, { -0.043939f, 0.086341f, 0.029421f },
|
|
{ -0.029506f, 0.086341f, -0.043809f }, { 0.035354f, 0.070897f, 0.035658f },
|
|
{ 0.035354f, -0.069397f, 0.035658f }, { -0.035498f, 0.070897f, 0.035658f },
|
|
{ -0.035498f, -0.069397f, 0.035658f }, { 0.035354f, -0.069397f, 0.365503f },
|
|
{ 0.035354f, 0.070897f, 0.365503f }, { -0.035498f, 0.070897f, 0.365503f },
|
|
{ -0.035498f, -0.069397f, 0.365503f }, { 0.035354f, 0.070897f, -0.365033f },
|
|
{ 0.035354f, -0.069397f, -0.365033f }, { -0.035498f, 0.070897f, -0.365033f },
|
|
{ -0.035498f, -0.069397f, -0.365033f }, { 0.035354f, -0.069397f, -0.035187f },
|
|
{ 0.035354f, 0.070897f, -0.035187f }, { -0.035498f, 0.070897f, -0.035187f },
|
|
{ -0.035498f, -0.069397f, -0.035187f }, { -0.035494f, -0.069397f, -0.035191f },
|
|
{ -0.365340f, -0.069397f, -0.035191f }, { -0.035494f, 0.070897f, -0.035191f },
|
|
{ -0.365340f, 0.070897f, -0.035191f }, { -0.365340f, -0.069397f, 0.035661f },
|
|
{ -0.035494f, -0.069397f, 0.035661f }, { -0.035494f, 0.070897f, 0.035661f },
|
|
{ -0.365340f, 0.070897f, 0.035661f }, { 0.035350f, 0.070897f, -0.035191f },
|
|
{ 0.365196f, 0.070897f, -0.035191f }, { 0.035350f, -0.069397f, -0.035191f },
|
|
{ 0.365196f, -0.069397f, -0.035191f }, { 0.365196f, 0.070897f, 0.035661f },
|
|
{ 0.035350f, 0.070897f, 0.035661f }, { 0.035350f, -0.069397f, 0.035661f },
|
|
{ 0.365196f, -0.069397f, 0.035661f }, { -0.070802f, 0.086337f, 0.355420f },
|
|
{ -0.070802f, -0.084705f, 0.355420f }, { -0.097081f, 0.086335f, 0.487537f },
|
|
{ -0.097081f, -0.084704f, 0.487537f }, { -0.000108f, 0.086337f, 0.362383f },
|
|
{ -0.000108f, 0.086335f, 0.497088f }, { -0.000108f, -0.084704f, 0.497088f },
|
|
{ -0.000108f, -0.084705f, 0.362383f }, { -0.138787f, -0.084705f, 0.334799f },
|
|
{ -0.138787f, 0.086337f, 0.334799f }, { -0.070810f, -0.084705f, 0.355419f },
|
|
{ -0.070810f, 0.086337f, 0.355419f }, { -0.097090f, 0.086335f, 0.487536f },
|
|
{ -0.190336f, 0.086335f, 0.459250f }, { -0.190336f, -0.084704f, 0.459250f },
|
|
{ -0.097090f, -0.084704f, 0.487536f }, { -0.138795f, 0.086337f, 0.334796f },
|
|
{ -0.138795f, -0.084705f, 0.334796f }, { -0.201442f, 0.086337f, 0.301310f },
|
|
{ -0.201442f, -0.084705f, 0.301310f }, { -0.276280f, -0.084704f, 0.413313f },
|
|
{ -0.276280f, 0.086335f, 0.413313f }, { -0.190344f, 0.086335f, 0.459247f },
|
|
{ -0.190344f, -0.084704f, 0.459247f }, { -0.201450f, -0.084705f, 0.301306f },
|
|
{ -0.256361f, -0.084705f, 0.256241f }, { -0.201450f, 0.086337f, 0.301306f },
|
|
{ -0.256361f, 0.086337f, 0.256241f }, { -0.351612f, 0.086335f, 0.351492f },
|
|
{ -0.351612f, -0.084704f, 0.351492f }, { -0.276288f, 0.086335f, 0.413309f },
|
|
{ -0.276288f, -0.084704f, 0.413309f }, { -0.301431f, -0.084705f, 0.201325f },
|
|
{ -0.413435f, -0.084704f, 0.276163f }, { -0.413435f, 0.086335f, 0.276163f },
|
|
{ -0.301431f, 0.086337f, 0.201325f }, { -0.256367f, -0.084705f, 0.256236f },
|
|
{ -0.256367f, 0.086337f, 0.256236f }, { -0.351618f, 0.086335f, 0.351487f },
|
|
{ -0.351618f, -0.084704f, 0.351487f }, { -0.334922f, -0.084705f, 0.138670f },
|
|
{ -0.459374f, -0.084704f, 0.190220f }, { -0.459374f, 0.086335f, 0.190220f },
|
|
{ -0.334922f, 0.086337f, 0.138670f }, { -0.301436f, 0.086337f, 0.201318f },
|
|
{ -0.301436f, -0.084705f, 0.201318f }, { -0.413440f, 0.086335f, 0.276156f },
|
|
{ -0.413440f, -0.084704f, 0.276156f }, { -0.487663f, -0.084704f, 0.096966f },
|
|
{ -0.355546f, -0.084705f, 0.070686f }, { -0.459377f, -0.084704f, 0.190212f },
|
|
{ -0.334926f, -0.084705f, 0.138663f }, { -0.355546f, 0.086337f, 0.070686f },
|
|
{ -0.334926f, 0.086337f, 0.138663f }, { -0.487663f, 0.086335f, 0.096966f },
|
|
{ -0.459377f, 0.086335f, 0.190212f }, { -0.362511f, -0.084705f, -0.000016f },
|
|
{ -0.355549f, -0.084705f, 0.070678f }, { -0.497217f, -0.084704f, -0.000016f },
|
|
{ -0.487665f, -0.084704f, 0.096957f }, { -0.497217f, 0.086335f, -0.000016f },
|
|
{ -0.362511f, 0.086337f, -0.000016f }, { -0.355549f, 0.086337f, 0.070678f },
|
|
{ -0.487665f, 0.086335f, 0.096957f }, { -0.362512f, 0.086337f, -0.000024f },
|
|
{ -0.362512f, -0.084705f, -0.000024f }, { -0.355549f, 0.086337f, -0.070717f },
|
|
{ -0.355549f, -0.084705f, -0.070717f }, { -0.497217f, -0.084704f, -0.000024f },
|
|
{ -0.487666f, -0.084704f, -0.096997f }, { -0.497217f, 0.086335f, -0.000024f },
|
|
{ -0.487666f, 0.086335f, -0.096997f }, { -0.334927f, -0.084705f, -0.138702f },
|
|
{ -0.355548f, 0.086337f, -0.070726f }, { -0.355548f, -0.084705f, -0.070726f },
|
|
{ -0.334927f, 0.086337f, -0.138702f }, { -0.487665f, -0.084704f, -0.097005f },
|
|
{ -0.459379f, -0.084704f, -0.190252f }, { -0.487665f, 0.086335f, -0.097005f },
|
|
{ -0.459379f, 0.086335f, -0.190252f }, { -0.413442f, -0.084704f, -0.276196f },
|
|
{ -0.301439f, -0.084705f, -0.201358f }, { -0.334925f, -0.084705f, -0.138710f },
|
|
{ -0.459376f, -0.084704f, -0.190260f }, { -0.334925f, 0.086337f, -0.138710f },
|
|
{ -0.301439f, 0.086337f, -0.201358f }, { -0.459376f, 0.086335f, -0.190260f },
|
|
{ -0.413442f, 0.086335f, -0.276196f }, { -0.256370f, -0.084705f, -0.256276f },
|
|
{ -0.301434f, 0.086337f, -0.201365f }, { -0.301434f, -0.084705f, -0.201365f },
|
|
{ -0.256370f, 0.086337f, -0.256276f }, { -0.413438f, 0.086335f, -0.276203f },
|
|
{ -0.351621f, 0.086335f, -0.351527f }, { -0.413438f, -0.084704f, -0.276203f },
|
|
{ -0.351621f, -0.084704f, -0.351527f }, { -0.256364f, -0.084705f, -0.256283f },
|
|
{ -0.201453f, 0.086337f, -0.301347f }, { -0.256364f, 0.086337f, -0.256283f },
|
|
{ -0.201453f, -0.084705f, -0.301347f }, { -0.276292f, -0.084704f, -0.413350f },
|
|
{ -0.351615f, -0.084704f, -0.351534f }, { -0.351615f, 0.086335f, -0.351534f },
|
|
{ -0.276292f, 0.086335f, -0.413350f }, { -0.201447f, -0.084705f, -0.301352f },
|
|
{ -0.276285f, -0.084704f, -0.413355f }, { -0.138799f, -0.084705f, -0.334838f },
|
|
{ -0.190349f, -0.084704f, -0.459289f }, { -0.276285f, 0.086335f, -0.413355f },
|
|
{ -0.201447f, 0.086337f, -0.301352f }, { -0.190349f, 0.086335f, -0.459289f },
|
|
{ -0.138799f, 0.086337f, -0.334838f }, { -0.190341f, 0.086335f, -0.459293f },
|
|
{ -0.190341f, -0.084704f, -0.459293f }, { -0.138791f, -0.084705f, -0.334842f },
|
|
{ -0.138791f, 0.086337f, -0.334842f }, { -0.070815f, 0.086337f, -0.355462f },
|
|
{ -0.070815f, -0.084705f, -0.355462f }, { -0.097095f, 0.086335f, -0.487579f },
|
|
{ -0.097095f, -0.084704f, -0.487579f }, { -0.070807f, 0.086337f, -0.355464f },
|
|
{ -0.097086f, 0.086335f, -0.487581f }, { -0.097086f, -0.084704f, -0.487581f },
|
|
{ -0.070807f, -0.084705f, -0.355464f }, { -0.000113f, -0.084705f, -0.362427f },
|
|
{ -0.000113f, 0.086337f, -0.362427f }, { -0.000113f, 0.086335f, -0.497132f },
|
|
{ -0.000113f, -0.084704f, -0.497132f }, { 0.070588f, 0.086337f, -0.355465f },
|
|
{ 0.096868f, -0.084704f, -0.487582f }, { 0.096868f, 0.086335f, -0.487582f },
|
|
{ 0.070588f, -0.084705f, -0.355465f }, { -0.000105f, 0.086337f, -0.362427f },
|
|
{ -0.000105f, 0.086335f, -0.497133f }, { -0.000105f, -0.084704f, -0.497133f },
|
|
{ -0.000105f, -0.084705f, -0.362427f }, { 0.190123f, -0.084704f, -0.459294f },
|
|
{ 0.190123f, 0.086335f, -0.459294f }, { 0.138573f, -0.084705f, -0.334843f },
|
|
{ 0.138573f, 0.086337f, -0.334843f }, { 0.070597f, 0.086337f, -0.355463f },
|
|
{ 0.070597f, -0.084705f, -0.355463f }, { 0.096876f, -0.084704f, -0.487580f },
|
|
{ 0.096876f, 0.086335f, -0.487580f }, { 0.201229f, -0.084705f, -0.301354f },
|
|
{ 0.276067f, -0.084704f, -0.413358f }, { 0.201229f, 0.086337f, -0.301354f },
|
|
{ 0.138581f, -0.084705f, -0.334840f }, { 0.138581f, 0.086337f, -0.334840f },
|
|
{ 0.190131f, 0.086335f, -0.459292f }, { 0.276067f, 0.086335f, -0.413358f },
|
|
{ 0.190131f, -0.084704f, -0.459292f }, { 0.201236f, 0.086337f, -0.301350f },
|
|
{ 0.256147f, -0.084705f, -0.256286f }, { 0.256147f, 0.086337f, -0.256286f },
|
|
{ 0.201236f, -0.084705f, -0.301350f }, { 0.351398f, -0.084704f, -0.351537f },
|
|
{ 0.351398f, 0.086335f, -0.351537f }, { 0.276074f, -0.084704f, -0.413353f },
|
|
{ 0.276074f, 0.086335f, -0.413353f }, { 0.301218f, -0.084705f, -0.201369f },
|
|
{ 0.301218f, 0.086337f, -0.201369f }, { 0.256154f, -0.084705f, -0.256280f },
|
|
{ 0.256154f, 0.086337f, -0.256280f }, { 0.413221f, 0.086335f, -0.276207f },
|
|
{ 0.413221f, -0.084704f, -0.276207f }, { 0.351405f, 0.086335f, -0.351531f },
|
|
{ 0.351405f, -0.084704f, -0.351531f }, { 0.334709f, 0.086337f, -0.138715f },
|
|
{ 0.301223f, 0.086337f, -0.201362f }, { 0.334709f, -0.084705f, -0.138715f },
|
|
{ 0.301223f, -0.084705f, -0.201362f }, { 0.459160f, -0.084704f, -0.190264f },
|
|
{ 0.459160f, 0.086335f, -0.190264f }, { 0.413226f, 0.086335f, -0.276200f },
|
|
{ 0.413226f, -0.084704f, -0.276200f }, { 0.334713f, -0.084705f, -0.138707f },
|
|
{ 0.355333f, -0.084705f, -0.070731f }, { 0.334713f, 0.086337f, -0.138707f },
|
|
{ 0.355333f, 0.086337f, -0.070731f }, { 0.459164f, 0.086335f, -0.190257f },
|
|
{ 0.487450f, 0.086335f, -0.097010f }, { 0.487450f, -0.084704f, -0.097010f },
|
|
{ 0.459164f, -0.084704f, -0.190257f }, { 0.355335f, -0.084705f, -0.070722f },
|
|
{ 0.362298f, -0.084705f, -0.000029f }, { 0.355335f, 0.086337f, -0.070722f },
|
|
{ 0.362298f, 0.086337f, -0.000029f }, { 0.497003f, -0.084704f, -0.000029f },
|
|
{ 0.497003f, 0.086335f, -0.000029f }, { 0.487452f, 0.086335f, -0.097002f },
|
|
{ 0.487452f, -0.084704f, -0.097002f }, { 0.362298f, -0.084705f, -0.000021f },
|
|
{ 0.497003f, -0.084704f, -0.000021f }, { 0.355336f, -0.084705f, 0.070673f },
|
|
{ 0.487453f, -0.084704f, 0.096952f }, { 0.497003f, 0.086335f, -0.000021f },
|
|
{ 0.362298f, 0.086337f, -0.000021f }, { 0.355336f, 0.086337f, 0.070673f },
|
|
{ 0.487453f, 0.086335f, 0.096952f }, { 0.355334f, -0.084705f, 0.070681f },
|
|
{ 0.355334f, 0.086337f, 0.070681f }, { 0.487451f, 0.086335f, 0.096961f },
|
|
{ 0.487451f, -0.084704f, 0.096961f }, { 0.334714f, -0.084705f, 0.138658f },
|
|
{ 0.459165f, -0.084704f, 0.190207f }, { 0.334714f, 0.086337f, 0.138658f },
|
|
{ 0.459165f, 0.086335f, 0.190207f }, { 0.334711f, 0.086337f, 0.138666f },
|
|
{ 0.301225f, 0.086337f, 0.201313f }, { 0.459163f, 0.086335f, 0.190215f },
|
|
{ 0.413229f, 0.086335f, 0.276151f }, { 0.334711f, -0.084705f, 0.138666f },
|
|
{ 0.301225f, -0.084705f, 0.201313f }, { 0.413229f, -0.084704f, 0.276151f },
|
|
{ 0.459163f, -0.084704f, 0.190215f }, { 0.301221f, -0.084705f, 0.201321f },
|
|
{ 0.256157f, -0.084705f, 0.256232f }, { 0.301221f, 0.086337f, 0.201321f },
|
|
{ 0.256157f, 0.086337f, 0.256232f }, { 0.413224f, -0.084704f, 0.276159f },
|
|
{ 0.413224f, 0.086335f, 0.276159f }, { 0.351408f, 0.086335f, 0.351483f },
|
|
{ 0.351408f, -0.084704f, 0.351483f }, { 0.201240f, -0.084705f, 0.301302f },
|
|
{ 0.201240f, 0.086337f, 0.301302f }, { 0.256151f, -0.084705f, 0.256238f },
|
|
{ 0.256151f, 0.086337f, 0.256238f }, { 0.351402f, 0.086335f, 0.351489f },
|
|
{ 0.351402f, -0.084704f, 0.351489f }, { 0.276078f, 0.086335f, 0.413305f },
|
|
{ 0.276078f, -0.084704f, 0.413305f }, { 0.138586f, -0.084705f, 0.334793f },
|
|
{ 0.201233f, -0.084705f, 0.301307f }, { 0.190135f, -0.084704f, 0.459244f },
|
|
{ 0.276071f, -0.084704f, 0.413311f }, { 0.201233f, 0.086337f, 0.301307f },
|
|
{ 0.138586f, 0.086337f, 0.334793f }, { 0.276071f, 0.086335f, 0.413311f },
|
|
{ 0.190135f, 0.086335f, 0.459244f }, { 0.138578f, -0.084705f, 0.334797f },
|
|
{ 0.070602f, -0.084705f, 0.355417f }, { 0.138578f, 0.086337f, 0.334797f },
|
|
{ 0.070602f, 0.086337f, 0.355417f }, { 0.190127f, 0.086335f, 0.459248f },
|
|
{ 0.190127f, -0.084704f, 0.459248f }, { 0.096881f, 0.086335f, 0.487534f },
|
|
{ 0.096881f, -0.084704f, 0.487534f }, { 0.070593f, -0.084705f, 0.355419f },
|
|
{ 0.096873f, 0.086335f, 0.487536f }, { 0.096873f, -0.084704f, 0.487536f },
|
|
{ 0.070593f, 0.086337f, 0.355419f }, { -0.000100f, 0.086337f, 0.362382f },
|
|
{ -0.000100f, 0.086335f, 0.497087f }, { -0.000100f, -0.084704f, 0.497087f },
|
|
{ -0.000100f, -0.084705f, 0.362382f },
|
|
};
|
|
|
|
typedef struct WheelHullSpan
|
|
{
|
|
int offset;
|
|
int count;
|
|
} WheelHullSpan;
|
|
static const WheelHullSpan s_metalWheel1Hulls[37] = {
|
|
{ 0, 29 }, { 29, 8 }, { 37, 8 }, { 45, 8 }, { 53, 8 }, { 61, 8 }, { 69, 8 }, { 77, 8 },
|
|
{ 85, 8 }, { 93, 8 }, { 101, 8 }, { 109, 8 }, { 117, 8 }, { 125, 8 }, { 133, 8 }, { 141, 8 },
|
|
{ 149, 8 }, { 157, 8 }, { 165, 8 }, { 173, 8 }, { 181, 8 }, { 189, 8 }, { 197, 8 }, { 205, 8 },
|
|
{ 213, 8 }, { 221, 8 }, { 229, 8 }, { 237, 8 }, { 245, 8 }, { 253, 8 }, { 261, 8 }, { 269, 8 },
|
|
{ 277, 8 }, { 285, 8 }, { 293, 8 }, { 301, 8 }, { 309, 8 },
|
|
};
|
|
#define s_metalWheel1HullCount 37
|
|
|
|
// 30 metal_wheel1 props (37-piece convex decompositions) stacked. Body-pair contact merging
|
|
// lets them settle and sleep instead of wobbling.
|
|
class WheelStack : public Sample
|
|
{
|
|
public:
|
|
explicit WheelStack( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( 0.0f, 12.0f, 5.0f, { 0.0f, 0.85f, 0.0f } );
|
|
}
|
|
|
|
AddGroundBox( 10.0f );
|
|
|
|
constexpr int N = 512;
|
|
b3Vec3 buffer[N];
|
|
int bufferCount = 0;
|
|
for ( int h = 0; h < s_metalWheel1HullCount; ++h )
|
|
{
|
|
const WheelHullSpan& span = s_metalWheel1Hulls[h];
|
|
m_hulls[h] = b3CreateHull( &s_metalWheel1Verts[span.offset], span.count, span.count );
|
|
|
|
const b3Vec3* points = b3GetHullPoints( m_hulls[h] );
|
|
for ( int j = 0; j < m_hulls[h]->vertexCount && bufferCount < N; ++j )
|
|
{
|
|
buffer[bufferCount] = points[j];
|
|
++bufferCount;
|
|
}
|
|
}
|
|
|
|
// Create a single hull that wraps the input hulls.
|
|
b3HullData* wheelHull = b3CreateHull( buffer, bufferCount, bufferCount );
|
|
|
|
const float height = 0.171f;
|
|
const float spacing = height + 0.006f;
|
|
const float startY = 0.5f * height + 0.004f;
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
shapeDef.baseMaterial.friction = 0.6f;
|
|
|
|
for ( int i = 0; i < m_wheelCount; ++i )
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.name = "wheel";
|
|
bodyDef.position = { 0.0f, startY + i * spacing, 0.0f };
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
// for ( int h = 0; h < s_metalWheel1HullCount; ++h )
|
|
//{
|
|
// b3CreateHullShape( bodyId, &shapeDef, m_hulls[h] );
|
|
// }
|
|
|
|
// Using a single hull improves the simulation.
|
|
b3CreateHullShape( bodyId, &shapeDef, wheelHull );
|
|
}
|
|
|
|
b3DestroyHull( wheelHull );
|
|
|
|
b3World_SetContactTuning( m_worldId, 240.0f, 10.0f, 3.0f );
|
|
}
|
|
|
|
~WheelStack() override
|
|
{
|
|
for ( int h = 0; h < s_metalWheel1HullCount; ++h )
|
|
{
|
|
b3DestroyHull( m_hulls[h] );
|
|
}
|
|
}
|
|
|
|
void Step() override
|
|
{
|
|
Sample::Step();
|
|
|
|
b3Profile p = b3World_GetProfile( m_worldId );
|
|
float substepRate = m_context->hertz * m_context->subStepCount;
|
|
|
|
DrawTextLine( "wheels %d, hull pieces/wheel %d (%d total shapes)", m_wheelCount,
|
|
s_metalWheel1HullCount, m_wheelCount * s_metalWheel1HullCount );
|
|
DrawTextLine( "step %.0f hz, sub-steps %d -> substep rate %.0f hz", m_context->hertz,
|
|
m_context->subStepCount, substepRate );
|
|
DrawTextLine( "eff contact hz = min(240, %.0f) = %.0f", 0.125f * substepRate,
|
|
b3MinFloat( 240.0f, 0.125f * substepRate ) );
|
|
DrawTextLine( "step %.3f ms | collide %.3f ms (%.0f%%) | solve %.3f ms (%.0f%%)", p.step,
|
|
p.collide, p.step > 0.0f ? 100.0f * p.collide / p.step : 0.0f, p.solve,
|
|
p.step > 0.0f ? 100.0f * p.solve / p.step : 0.0f );
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new WheelStack( context );
|
|
}
|
|
|
|
b3HullData* m_hulls[s_metalWheel1HullCount];
|
|
static constexpr int m_wheelCount = 30;
|
|
};
|
|
|
|
static int sampleWheelStack = RegisterSample( "Issues", "GMod Wheel Stack", WheelStack::Create );
|
|
|
|
// There was a problem where this bouncing box could gain energy. This has been fixed by the
|
|
// deferred restitution update.
|
|
class RestitutionOvershoot : public Sample
|
|
{
|
|
public:
|
|
static constexpr float m_boxHalf = 0.5f;
|
|
static constexpr float m_floorHalfXZ = 0.375f;
|
|
static constexpr float m_floorHalfY = 0.25f;
|
|
static constexpr float m_dropHeight = 10.0f;
|
|
static constexpr float m_tolerance = 0.05f;
|
|
|
|
explicit RestitutionOvershoot( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( 20.0f, 0.0f, 28.0f, { 0.0f, m_dropHeight + m_boxHalf, 0.0f } );
|
|
}
|
|
|
|
b3BoxHull floor = b3MakeBoxHull( m_floorHalfXZ, m_floorHalfY, m_floorHalfXZ );
|
|
b3BodyDef floorDef = b3DefaultBodyDef();
|
|
floorDef.type = b3_staticBody;
|
|
floorDef.position = { 0.0f, -m_floorHalfY, 0.0f };
|
|
b3BodyId floorBody = b3CreateBody( m_worldId, &floorDef );
|
|
|
|
b3ShapeDef floorShape = b3DefaultShapeDef();
|
|
b3CreateHullShape( floorBody, &floorShape, &floor.base );
|
|
|
|
b3BoxHull box = b3MakeBoxHull( m_boxHalf, m_boxHalf, m_boxHalf );
|
|
b3BodyDef boxDef = b3DefaultBodyDef();
|
|
boxDef.type = b3_dynamicBody;
|
|
boxDef.position = { 0.0f, m_dropHeight, 0.0f };
|
|
boxDef.safetyFactor = 0.1f;
|
|
m_boxBody = b3CreateBody( m_worldId, &boxDef );
|
|
|
|
b3ShapeDef boxShape = b3DefaultShapeDef();
|
|
boxShape.baseMaterial.restitution = 1.0f;
|
|
b3CreateHullShape( m_boxBody, &boxShape, &box.base );
|
|
|
|
m_currentY = m_dropHeight;
|
|
m_maxBounceY = 0.0f;
|
|
m_bounced = false;
|
|
m_failed = false;
|
|
m_startEnergy = MeasureEnergy( m_worldId, &m_boxBody, 1 ).Total();
|
|
m_maxEnergy = m_startEnergy;
|
|
m_energyFailed = false;
|
|
}
|
|
|
|
void Step() override
|
|
{
|
|
Sample::Step();
|
|
|
|
if ( B3_IS_NON_NULL( m_boxBody ) == false )
|
|
{
|
|
return;
|
|
}
|
|
|
|
b3Pos position = b3Body_GetPosition( m_boxBody );
|
|
m_currentY = position.y;
|
|
|
|
b3Vec3 velocity = b3Body_GetLinearVelocity( m_boxBody );
|
|
if ( m_bounced == false && velocity.y > 0.0f )
|
|
{
|
|
m_bounced = true;
|
|
}
|
|
|
|
if ( m_bounced )
|
|
{
|
|
if ( position.y > m_maxBounceY )
|
|
{
|
|
m_maxBounceY = position.y;
|
|
}
|
|
if ( position.y > m_dropHeight + m_tolerance )
|
|
{
|
|
m_failed = true;
|
|
}
|
|
}
|
|
|
|
b3Pos markerPoint = { 0.0f, m_dropHeight + m_boxHalf, 0.0f };
|
|
DrawPlane( b3Vec3_axisY, markerPoint, MakeColor( b3_colorYellow ) );
|
|
|
|
// Perfect restitution and no friction, so the total energy may only decrease. It is the
|
|
// honest invariant here: the height check cannot tell a bounce that gained potential energy
|
|
// from one that converted it into spin, and this drop does both.
|
|
MechanicalEnergy energy = MeasureEnergy( m_worldId, &m_boxBody, 1 );
|
|
float total = energy.Total();
|
|
|
|
if ( total > m_maxEnergy )
|
|
{
|
|
m_maxEnergy = total;
|
|
}
|
|
|
|
if ( m_bounced && total > m_startEnergy * ( 1.0f + m_energyTolerance ) )
|
|
{
|
|
m_energyFailed = true;
|
|
}
|
|
|
|
DrawTextLine( "drop height = %.2f m", m_dropHeight );
|
|
DrawTextLine( "current y = %.2f m", m_currentY );
|
|
DrawTextLine( "max bounce = %.2f m", m_maxBounceY );
|
|
DrawTextLine( "kinetic = %.0f J linear + %.0f J angular", energy.linear,
|
|
energy.angular );
|
|
DrawTextLine( "potential = %.0f J", energy.potential );
|
|
DrawTextLine( "total = %.0f J (%.2f%% of start, peak %.2f%%)", total,
|
|
100.0f * total / m_startEnergy, 100.0f * m_maxEnergy / m_startEnergy );
|
|
|
|
if ( m_bounced == false )
|
|
{
|
|
DrawTextLine( "waiting for first bounce..." );
|
|
}
|
|
else if ( m_failed )
|
|
{
|
|
DrawTextLine( "FAIL: box exceeded drop height" );
|
|
}
|
|
else
|
|
{
|
|
DrawTextLine( "PASS: bounce stays at or below drop height" );
|
|
}
|
|
|
|
if ( m_energyFailed )
|
|
{
|
|
DrawTextLine( "FAIL: total energy increased" );
|
|
}
|
|
else if ( m_bounced )
|
|
{
|
|
DrawTextLine( "PASS: total energy did not increase" );
|
|
}
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new RestitutionOvershoot( context );
|
|
}
|
|
|
|
static constexpr float m_energyTolerance = 0.001f;
|
|
|
|
b3BodyId m_boxBody = {};
|
|
float m_currentY = 0.0f;
|
|
float m_maxBounceY = 0.0f;
|
|
float m_startEnergy = 0.0f;
|
|
float m_maxEnergy = 0.0f;
|
|
bool m_bounced = false;
|
|
bool m_failed = false;
|
|
bool m_energyFailed = false;
|
|
};
|
|
|
|
static int sampleRestitutionOvershoot =
|
|
RegisterSample( "Issues", "Restitution Overshoot", RestitutionOvershoot::Create );
|
|
|
|
class SlideTwistOffCenterShape : public Sample
|
|
{
|
|
public:
|
|
explicit SlideTwistOffCenterShape( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( -30.0f, 17.0f, 30.0f, { 0.0f, 5.0f, 0.0f } );
|
|
}
|
|
|
|
AddGroundBox( 50.0f );
|
|
|
|
b3Quat orientation = b3MakeQuatFromAxisAngle( b3Vec3_axisX, 20.0f * B3_DEG_TO_RAD );
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
bodyDef.position = { 0.0f, 4.0f, 0.0f };
|
|
bodyDef.rotation = orientation;
|
|
b3BodyId planeBody = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3BoxHull plane = b3MakeBoxHull( 10.0f, 0.5f, 10.0f );
|
|
shapeDef.baseMaterial.friction = 0.6f;
|
|
b3CreateHullShape( planeBody, &shapeDef, &plane.base );
|
|
|
|
b3Vec3 boxLocalCenter = { 1.0f, 0.5f, 1.0f };
|
|
b3Vec3 boxOffset = b3RotateVector( orientation, boxLocalCenter );
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.position = { -boxOffset.x, 5.0f - boxOffset.y, -boxOffset.z };
|
|
bodyDef.rotation = orientation;
|
|
// bodyDef.angularVelocity = 25.0f * b3RotateVector( orientation, b3Vec3_axisY );
|
|
b3BodyId boxBody = b3CreateBody( m_worldId, &bodyDef );
|
|
b3BoxHull mBox = b3MakeOffsetBoxHull( 1.0f, 0.5f, 1.0f, boxLocalCenter );
|
|
shapeDef.baseMaterial.friction = 0.3f;
|
|
b3CreateHullShape( boxBody, &shapeDef, &mBox.base );
|
|
|
|
b3Body_SetAngularVelocity( boxBody, 25.0f * b3RotateVector( orientation, b3Vec3_axisY ) );
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new SlideTwistOffCenterShape( context );
|
|
}
|
|
};
|
|
|
|
static int sampleSlideTwistOffCenterShape =
|
|
RegisterSample( "Issues", "Slide Twist Off Center Shape", SlideTwistOffCenterShape::Create );
|
|
|
|
// Very large dynamic bodies have overflow when computing the inertia tensor.
|
|
class HugeBox : public Sample
|
|
{
|
|
public:
|
|
explicit HugeBox( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( 0.0f, 25.0f, 10.0f, b3Pos_zero );
|
|
}
|
|
|
|
AddGroundBox( 400.0f );
|
|
|
|
{
|
|
float a = 100.0f;
|
|
|
|
b3BoxHull cube = b3MakeCubeHull( a );
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.name = "cube";
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.position = { 0.0f, a, 0.0f };
|
|
bodyDef.rotation = b3MakeQuatFromAxisAngle( { 0.0f, 0.0f, 1.0f }, 0.2f );
|
|
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3CreateHullShape( m_bodyId, &shapeDef, &cube.base );
|
|
}
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new HugeBox( context );
|
|
}
|
|
|
|
b3BodyId m_bodyId;
|
|
};
|
|
|
|
static int sampleHugeBox = RegisterSample( "Issues", "Huge Box", HugeBox::Create );
|
|
|
|
// A tilted cylinder whose bounds cover more than 512 height field triangles
|
|
class HeightfieldIssue : public Sample
|
|
{
|
|
public:
|
|
explicit HeightfieldIssue( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( -90.0f, 5.0f, 50.0f, { 50.0f, 10.0f, 50.0f } );
|
|
}
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
m_heightField = b3CreateGrid( 100, 100, { 1, 1, 1 }, false );
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
b3CreateHeightFieldShape( groundId, &shapeDef, m_heightField );
|
|
|
|
b3HullData* hull = b3CreateCylinder( 20, 5, -10, 24 );
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.position = { 50, 20, 50 };
|
|
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisX, B3_PI / 4 );
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
b3CreateHullShape( bodyId, &shapeDef, hull );
|
|
b3DestroyHull( hull );
|
|
}
|
|
|
|
~HeightfieldIssue() override
|
|
{
|
|
b3DestroyHeightField( m_heightField );
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new HeightfieldIssue( context );
|
|
}
|
|
|
|
b3HeightFieldData* m_heightField;
|
|
};
|
|
static int sampleIssueIndex = RegisterSample( "Issues", "Heightfield", HeightfieldIssue::Create );
|
|
|
|
// Issue #135: the quasi static push out idiom, zero the velocity then step, drives a deeply
|
|
// overlapping body out through a thin static wall instead of back into the room. The solver
|
|
// resolves along the shallow exit, so once the mover center crosses the wall mid plane every
|
|
// step compounds the error outward. Interior is -x, exterior is +x. The wire box marks where
|
|
// a mover resting flush against the interior face belongs.
|
|
// This is NOT a bug. This is just how the separating axis works. I'm keeping this sample
|
|
// in case there is further discussion.
|
|
class ThinWallPushOut : public Sample
|
|
{
|
|
public:
|
|
static constexpr float m_wallHalfThickness = 0.1f;
|
|
static constexpr float m_wallHalfHeight = 1.5f;
|
|
static constexpr float m_moverHalfExtent = 0.5f;
|
|
static constexpr float m_tolerance = 0.01f;
|
|
|
|
explicit ThinWallPushOut( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( 20.0f, 15.0f, 6.0f, { 0.0f, m_wallHalfHeight, 0.0f } );
|
|
}
|
|
|
|
// Furniture is dragged into place, never dropped
|
|
b3World_SetGravity( m_worldId, b3Vec3_zero );
|
|
|
|
AddGroundBox( 20.0f );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
bodyDef.name = "wall";
|
|
bodyDef.position = { 0.0f, m_wallHalfHeight, 0.0f };
|
|
b3BodyId wallId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3BoxHull wall = b3MakeBoxHull( m_wallHalfThickness, m_wallHalfHeight, 2.0f );
|
|
b3CreateHullShape( wallId, &shapeDef, &wall.base );
|
|
|
|
bodyDef.name = "mover";
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.position = { m_startX, m_wallHalfHeight, 0.0f };
|
|
|
|
// Velocity is zeroed every step, so without this the mover falls asleep on the first one
|
|
bodyDef.enableSleep = false;
|
|
|
|
// A room planner slides items on the floor plane and yaws them to face a wall
|
|
bodyDef.motionLocks = { false, true, false, true, false, true };
|
|
m_moverId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3BoxHull mover = b3MakeBoxHull( m_moverHalfExtent, m_moverHalfExtent, m_moverHalfExtent );
|
|
shapeDef.baseMaterial.friction = 0.0f;
|
|
b3CreateHullShape( m_moverId, &shapeDef, &mover.base );
|
|
}
|
|
|
|
void PlaceMover()
|
|
{
|
|
b3Body_SetTransform( m_moverId, { m_startX, m_wallHalfHeight, 0.0f }, b3Quat_identity );
|
|
b3Body_SetLinearVelocity( m_moverId, b3Vec3_zero );
|
|
b3Body_SetAngularVelocity( m_moverId, b3Vec3_zero );
|
|
}
|
|
|
|
bool DrawControls() override
|
|
{
|
|
ImGui::PushItemWidth( 6.0f * ImGui::GetFontSize() );
|
|
|
|
// Anywhere past the wall mid plane tunnels out, even while the near face is far deeper
|
|
bool changed = ImGui::SliderFloat( "Start X", &m_startX, -0.6f, 0.6f, "%.2f" );
|
|
changed = changed || ImGui::Button( "Reset Mover" );
|
|
|
|
if ( changed )
|
|
{
|
|
PlaceMover();
|
|
}
|
|
|
|
ImGui::PopItemWidth();
|
|
return true;
|
|
}
|
|
|
|
void Step() override
|
|
{
|
|
// The reported idiom. Zeroing the velocity leaves the penetration correction as the
|
|
// only thing that can move the body.
|
|
b3Body_SetLinearVelocity( m_moverId, b3Vec3_zero );
|
|
b3Body_SetAngularVelocity( m_moverId, b3Vec3_zero );
|
|
|
|
Sample::Step();
|
|
|
|
float interiorRestX = -m_wallHalfThickness - m_moverHalfExtent;
|
|
float exteriorRestX = m_wallHalfThickness + m_moverHalfExtent;
|
|
|
|
b3Vec3 lower = { interiorRestX - m_moverHalfExtent, m_wallHalfHeight - m_moverHalfExtent,
|
|
-m_moverHalfExtent };
|
|
b3Vec3 upper = { interiorRestX + m_moverHalfExtent, m_wallHalfHeight + m_moverHalfExtent,
|
|
m_moverHalfExtent };
|
|
DrawAabb( lower, upper, MakeColor( b3_colorLime ) );
|
|
|
|
float x = (float)b3Body_GetPosition( m_moverId ).x;
|
|
DrawTextLine( "start x = %.3f m", m_startX );
|
|
DrawTextLine( "current x = %.3f m", x );
|
|
DrawTextLine( "interior rest x = %.3f m", interiorRestX );
|
|
|
|
if ( b3AbsFloat( x - exteriorRestX ) < m_tolerance )
|
|
{
|
|
DrawTextLine( "FAIL: mover pushed out of the room" );
|
|
}
|
|
else if ( b3AbsFloat( x - interiorRestX ) < m_tolerance )
|
|
{
|
|
DrawTextLine( "PASS: mover pushed back into the room" );
|
|
}
|
|
else
|
|
{
|
|
DrawTextLine( "relaxing ..." );
|
|
}
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new ThinWallPushOut( context );
|
|
}
|
|
|
|
b3BodyId m_moverId = {};
|
|
float m_startX = 0.2f;
|
|
};
|
|
|
|
static int sampleThinWallPushOut =
|
|
RegisterSample( "Issues", "Thin Wall Push Out", ThinWallPushOut::Create );
|
|
|
|
// Two crates resting on a rolling deck, one each side of the roll axis: the orange crate with
|
|
// contact recycling (the default), the blue crate with it off for its body.
|
|
//
|
|
// A recycled contact keeps the normal and anchors it was made with, fixed in the world, while each
|
|
// body turns less than B3_CONTACT_RECYCLE_ANGULAR_DISTANCE (10 degrees) from where the contact was
|
|
// made and the two stay within the recycle distance (5 cm). Only the separation is updated. A deck
|
|
// and the cargo resting on it turn together, so their contact is recycled through the roll. Its
|
|
// normal stays the deck's up as it was, and its anchors stay at the same world offsets from each
|
|
// body's center of mass, so the orange crate moves as the deck moves at that fixed spot in space,
|
|
// not as the deck under it moves. It goes straight up and down instead of along its arc, and slides
|
|
// out across the deck by about offset * (1 - cos(roll)). While that stays under the recycle
|
|
// distance the crate sways out and back. Past it (8 m and 8 degrees here, 78 mm) the contact is
|
|
// made again partway through, at the deck's angle then, and frozen there, so the crate walks off
|
|
// across the deck roll by roll. The angular tolerance doesn't stop that: at 12 or 15 degrees the
|
|
// contact is made again more often and the crate walks all the same. The blue crate rides with the
|
|
// deck. The slide arrows on the deck are drawn ten times longer.
|
|
class RollingDeck : public Sample
|
|
{
|
|
public:
|
|
static constexpr float m_deckHalfLength = 2.0f;
|
|
static constexpr float m_deckHalfWidth = 10.0f;
|
|
static constexpr float m_deckHalfThickness = 0.25f;
|
|
static constexpr float m_crateHalf = 0.5f;
|
|
static constexpr float m_crateOffset = 8.0f;
|
|
static constexpr float m_friction = 1.0f;
|
|
static constexpr float m_settleTime = 1.0f;
|
|
static constexpr float m_slideScale = 10.0f;
|
|
|
|
explicit RollingDeck( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( 75.0f, 15.0f, 32.0f, { 0.0f, 0.5f, 0.0f } );
|
|
}
|
|
|
|
m_rollDegrees = 8.0f;
|
|
m_period = 6.0f;
|
|
m_deckId = {};
|
|
m_crateIds[0] = {};
|
|
m_crateIds[1] = {};
|
|
|
|
CreateScene();
|
|
}
|
|
|
|
void CreateScene()
|
|
{
|
|
if ( B3_IS_NON_NULL( m_deckId ) )
|
|
{
|
|
b3DestroyBody( m_deckId );
|
|
b3DestroyBody( m_crateIds[0] );
|
|
b3DestroyBody( m_crateIds[1] );
|
|
}
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
shapeDef.baseMaterial.friction = m_friction;
|
|
|
|
// The deck rolls about the world x axis through its center
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.type = b3_kinematicBody;
|
|
m_deckId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3BoxHull box = b3MakeBoxHull( m_deckHalfLength, m_deckHalfThickness, m_deckHalfWidth );
|
|
shapeDef.baseMaterial.customColor = b3_colorSlateGray;
|
|
b3CreateHullShape( m_deckId, &shapeDef, &box.base );
|
|
}
|
|
|
|
// The crates rest on the level deck, m_crateOffset either side of the roll axis
|
|
b3BoxHull box = b3MakeBoxHull( m_crateHalf, m_crateHalf, m_crateHalf );
|
|
for ( int i = 0; i < 2; ++i )
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.position = { 0.0f, m_deckHalfThickness + m_crateHalf,
|
|
i == 0 ? m_crateOffset : -m_crateOffset };
|
|
|
|
// The orange crate recycles its contact with the deck, the blue crate doesn't. Both
|
|
// stay awake, so a small roll can't put them to sleep.
|
|
bodyDef.enableContactRecycling = i == 0;
|
|
bodyDef.enableSleep = false;
|
|
m_crateIds[i] = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
shapeDef.baseMaterial.customColor = i == 0 ? b3_colorOrange : b3_colorDodgerBlue;
|
|
b3CreateHullShape( m_crateIds[i], &shapeDef, &box.base );
|
|
|
|
m_start[i] = b3Vec3_zero;
|
|
m_maxSlide[i] = 0.0f;
|
|
m_maxNormalError[i] = 0.0f;
|
|
}
|
|
|
|
m_time = 0.0f;
|
|
m_started = false;
|
|
}
|
|
|
|
// The roll angle in radians at a time since the start: still while the crates settle, then
|
|
// rolling, eased in over the first quarter roll so the crates aren't jolted by a deck starting
|
|
// at full speed.
|
|
float RollAngle( float time ) const
|
|
{
|
|
float t = time - m_settleTime;
|
|
if ( t <= 0.0f )
|
|
{
|
|
return 0.0f;
|
|
}
|
|
|
|
float ease = 1.0f;
|
|
if ( t < 0.25f * m_period )
|
|
{
|
|
float u = t / ( 0.25f * m_period );
|
|
ease = u * u * ( 3.0f - 2.0f * u );
|
|
}
|
|
|
|
return ease * m_rollDegrees * B3_PI / 180.0f * sinf( 2.0f * B3_PI * t / m_period );
|
|
}
|
|
|
|
bool DrawControls() override
|
|
{
|
|
// Changing the roll starts the scene again, so the contacts are made with the deck level.
|
|
ImGui::SliderFloat( "Roll (deg)", &m_rollDegrees, 0.0f, 15.0f, "%.1f" );
|
|
if ( ImGui::IsItemDeactivatedAfterEdit() )
|
|
{
|
|
CreateScene();
|
|
}
|
|
|
|
ImGui::SliderFloat( "Period (s)", &m_period, 2.0f, 12.0f, "%.1f" );
|
|
if ( ImGui::IsItemDeactivatedAfterEdit() )
|
|
{
|
|
CreateScene();
|
|
}
|
|
|
|
if ( ImGui::Button( "Reset" ) )
|
|
{
|
|
CreateScene();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void Step() override
|
|
{
|
|
float timeStep = m_context->hertz > 0.0f ? 1.0f / m_context->hertz : 0.0f;
|
|
if ( m_context->pause && m_context->singleStep == 0 )
|
|
{
|
|
timeStep = 0.0f;
|
|
}
|
|
|
|
if ( timeStep > 0.0f )
|
|
{
|
|
b3Quat rotation =
|
|
b3MakeQuatFromAxisAngle( b3Vec3_axisX, RollAngle( m_time + timeStep ) );
|
|
b3Body_SetTargetTransform( m_deckId, { { 0.0f, 0.0f, 0.0f }, rotation }, timeStep,
|
|
true );
|
|
}
|
|
|
|
Sample::Step();
|
|
|
|
m_time += timeStep;
|
|
|
|
// Where the crates settled, in the deck's frame, measured from as the deck starts to roll
|
|
if ( m_started == false && m_time >= m_settleTime )
|
|
{
|
|
for ( int i = 0; i < 2; ++i )
|
|
{
|
|
m_start[i] = b3Body_GetLocalPoint( m_deckId, b3Body_GetPosition( m_crateIds[i] ) );
|
|
}
|
|
m_started = true;
|
|
}
|
|
|
|
b3Vec3 deckUp = b3Body_GetWorldVector( m_deckId, b3Vec3_axisY );
|
|
float slide[2] = { 0.0f, 0.0f };
|
|
float normalError[2] = { 0.0f, 0.0f };
|
|
|
|
for ( int i = 0; i < 2; ++i )
|
|
{
|
|
Vec4 color = i == 0 ? MakeColor( b3_colorOrange ) : MakeColor( b3_colorDodgerBlue );
|
|
b3Pos center = b3Body_GetPosition( m_crateIds[i] );
|
|
|
|
// The deck's up (white) and the crate's contact normal (in the crate's color), from the
|
|
// crate's center
|
|
DrawArrow( center, b3OffsetPos( center, b3MulSV( 2.5f, deckUp ) ),
|
|
MakeColor( b3_colorWhite ) );
|
|
|
|
b3ContactData contactData;
|
|
if ( b3Body_GetContactData( m_crateIds[i], &contactData, 1 ) == 1 &&
|
|
contactData.manifoldCount > 0 )
|
|
{
|
|
b3Vec3 normal = contactData.manifolds[0].normal;
|
|
if ( b3Dot( normal, deckUp ) < 0.0f )
|
|
{
|
|
normal = b3Neg( normal );
|
|
}
|
|
|
|
DrawArrow( center, b3OffsetPos( center, b3MulSV( 2.5f, normal ) ), color );
|
|
normalError[i] =
|
|
acosf( b3MinFloat( b3Dot( normal, deckUp ), 1.0f ) ) * 180.0f / B3_PI;
|
|
}
|
|
|
|
DrawString3D( b3OffsetPos( center, b3MulSV( 3.0f, deckUp ) ), color, "%s",
|
|
i == 0 ? "recycling on" : "recycling off" );
|
|
|
|
if ( m_started == false )
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Where the crate settled, outlined on the deck's top, and how far it has slid from
|
|
// there across the deck, drawn m_slideScale times longer
|
|
b3Vec3 local = b3Body_GetLocalPoint( m_deckId, center );
|
|
b3Vec3 offset = b3Sub( local, m_start[i] );
|
|
offset.y = 0.0f;
|
|
slide[i] = b3Length( offset );
|
|
|
|
float top = m_deckHalfThickness + 0.01f;
|
|
b3Vec3 mark = { m_start[i].x, top, m_start[i].z };
|
|
b3Vec3 corners[4] = {
|
|
{ mark.x - m_crateHalf, top, mark.z - m_crateHalf },
|
|
{ mark.x + m_crateHalf, top, mark.z - m_crateHalf },
|
|
{ mark.x + m_crateHalf, top, mark.z + m_crateHalf },
|
|
{ mark.x - m_crateHalf, top, mark.z + m_crateHalf },
|
|
};
|
|
for ( int j = 0; j < 4; ++j )
|
|
{
|
|
DrawLine( b3Body_GetWorldPoint( m_deckId, corners[j] ),
|
|
b3Body_GetWorldPoint( m_deckId, corners[( j + 1 ) % 4] ), color );
|
|
}
|
|
|
|
b3Vec3 tip = b3MulAdd( mark, m_slideScale, offset );
|
|
DrawArrow( b3Body_GetWorldPoint( m_deckId, mark ),
|
|
b3Body_GetWorldPoint( m_deckId, tip ), color );
|
|
|
|
m_maxSlide[i] = b3MaxFloat( m_maxSlide[i], slide[i] );
|
|
m_maxNormalError[i] = b3MaxFloat( m_maxNormalError[i], normalError[i] );
|
|
}
|
|
|
|
float roll = RollAngle( m_time ) * 180.0f / B3_PI;
|
|
float expected = m_crateOffset * ( 1.0f - cosf( m_rollDegrees * B3_PI / 180.0f ) );
|
|
|
|
DrawTextLine( "deck rolls %.1f deg each way every %.1f s (now %+.1f deg), crates %.0f m "
|
|
"from the roll axis, friction %.0f",
|
|
m_rollDegrees, m_period, roll, m_crateOffset, m_friction );
|
|
DrawTextLine( "a contact is recycled while each body turns < 10 deg from where it was made "
|
|
"(B3_CONTACT_RECYCLE_ANGULAR_DISTANCE)" );
|
|
DrawTextLine( "contacts recycled this step: %d (only the orange crate's can be)",
|
|
b3World_GetCounters( m_worldId ).recycledContactCount );
|
|
DrawTextLine( "orange, recycling on : normal %4.1f deg off the deck's up (most %4.1f), "
|
|
"slid %5.1f mm (most %5.1f)",
|
|
normalError[0], m_maxNormalError[0], 1000.0f * slide[0],
|
|
1000.0f * m_maxSlide[0] );
|
|
DrawTextLine( "blue, recycling off: normal %4.1f deg off the deck's up (most %4.1f), "
|
|
"slid %5.1f mm (most %5.1f)",
|
|
normalError[1], m_maxNormalError[1], 1000.0f * slide[1],
|
|
1000.0f * m_maxSlide[1] );
|
|
DrawTextLine(
|
|
"recycled, the crate moves straight up and down: offset * (1 - cos(roll)) = %.1f mm at "
|
|
"each end; past the "
|
|
"%.0f cm recycle distance the contact is made again tilted and the crate walks off",
|
|
1000.0f * expected, 100.0f * b3World_GetContactRecycleDistance( m_worldId ) );
|
|
DrawTextLine( "arrows: white is the deck's up, colored is the contact normal; slide arrows "
|
|
"on the deck are drawn %.0fx",
|
|
m_slideScale );
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new RollingDeck( context );
|
|
}
|
|
|
|
b3BodyId m_deckId;
|
|
b3BodyId m_crateIds[2];
|
|
b3Vec3 m_start[2];
|
|
float m_maxSlide[2];
|
|
float m_maxNormalError[2];
|
|
float m_rollDegrees;
|
|
float m_period;
|
|
float m_time;
|
|
bool m_started;
|
|
};
|
|
|
|
static int sampleRollingDeck = RegisterSample( "Issues", "Rolling Deck", RollingDeck::Create );
|
|
|
|
// A car made of one dynamic box (the chassis) and four ray cast 'wheels'.
|
|
// There are no wheel bodies and no joints.
|
|
|
|
// W/S to drive, A/D to steer and Space to brake. T toggles a camera that follows the car.
|
|
|
|
// Each step every wheel casts a ray down the chassis' own down axis. Where a ray hits the ground
|
|
// the wheel applies two forces to the chassis at the contact point:
|
|
// - a spring and damper force along the contact normal, which holds the chassis up
|
|
// - a tyre force in the contact plane, which drives, brakes and stops the wheel sliding sideways
|
|
// The tyre force is limited by friction times the spring force, so a wheel that is lightly loaded
|
|
// grips less. Weight transfer, body roll and rollover come from where these forces are applied.
|
|
class RaycastVehicle : public Sample
|
|
{
|
|
public:
|
|
static constexpr int m_wheelCount = 4;
|
|
static constexpr uint64_t m_vehicleBit = 0x2;
|
|
|
|
struct Wheel
|
|
{
|
|
b3Vec3 localMount;
|
|
bool steered;
|
|
|
|
// Distance from the mount down to the wheel's center
|
|
float suspensionLength;
|
|
bool grounded;
|
|
|
|
// Rotation about the axle, for drawing only
|
|
float spinAngle;
|
|
float spinSpeed;
|
|
};
|
|
|
|
explicit RaycastVehicle( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( 25.0f, 20.0f, 12.0f, { 0.0f, 1.0f, 0.0f } );
|
|
}
|
|
|
|
m_suspensionHertz = 1.5f;
|
|
m_suspensionDampingRatio = 0.45f;
|
|
m_friction = 1.0f;
|
|
m_wheelRadius = 0.35f;
|
|
m_suspensionTravel = 0.3f;
|
|
m_engineAcceleration = 5.0f;
|
|
m_brakeDeceleration = 8.0f;
|
|
m_maxSteeringAngle = 0.15f * B3_PI;
|
|
m_steeringAngle = 0.0f;
|
|
|
|
AddGroundBox( 100.0f );
|
|
|
|
// A ramp to show the suspension working
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.position = { 15.0f, -0.5f, 0.0f };
|
|
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, 0.1f );
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3BoxHull box = b3MakeBoxHull( 6.0f, 0.5f, 4.0f );
|
|
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
|
}
|
|
|
|
// The chassis is the only body. Its shape has its own category so the wheel rays can skip
|
|
// it.
|
|
float halfLength = 2.2f;
|
|
float halfHeight = 0.7f;
|
|
float halfWidth = 0.9f;
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.position = { 0.0f, 1.2f, 0.0f };
|
|
m_chassisId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
shapeDef.density = 130.0f;
|
|
shapeDef.filter.categoryBits = m_vehicleBit;
|
|
b3BoxHull box = b3MakeBoxHull( halfLength, halfHeight, halfWidth );
|
|
b3CreateHullShape( m_chassisId, &shapeDef, &box.base );
|
|
|
|
// A car tips over when its lateral acceleration exceeds half the track width divided by
|
|
// the height of its center of mass. At the middle of this box the center of mass would
|
|
// be about as high as the half track, and the car would roll before its tyres slide.
|
|
// Lower it to near the floor, where the heavy parts of a car are.
|
|
b3MassData massData = b3Body_GetMassData( m_chassisId );
|
|
massData.center = { 0.0f, -0.45f, 0.0f };
|
|
b3Body_SetMassData( m_chassisId, massData );
|
|
}
|
|
|
|
// The mounts are the top of each wheel's travel, inside the chassis at the sides. +x is
|
|
// forward.
|
|
float axle = 0.75f * halfLength;
|
|
float mountHeight = -0.3f;
|
|
m_wheels[0] = { { axle, mountHeight, halfWidth }, true, m_suspensionTravel };
|
|
m_wheels[1] = { { axle, mountHeight, -halfWidth }, true, m_suspensionTravel };
|
|
m_wheels[2] = { { -axle, mountHeight, halfWidth }, false, m_suspensionTravel };
|
|
m_wheels[3] = { { -axle, mountHeight, -halfWidth }, false, m_suspensionTravel };
|
|
|
|
// A cylinder along its y axis, centered, for drawing the wheels
|
|
float wheelWidth = 0.25f;
|
|
m_wheelHull = b3CreateCylinder( wheelWidth, m_wheelRadius, -0.5f * wheelWidth, 12 );
|
|
}
|
|
|
|
~RaycastVehicle() override
|
|
{
|
|
b3DestroyHull( m_wheelHull );
|
|
|
|
if ( m_camera->m_thirdPerson )
|
|
{
|
|
ToggleThirdPerson();
|
|
}
|
|
}
|
|
|
|
void Keyboard( int key, int action, int mods ) override
|
|
{
|
|
if ( key == KEY_T && action == ACTION_PRESS )
|
|
{
|
|
ToggleThirdPerson();
|
|
}
|
|
}
|
|
|
|
bool DrawControls() override
|
|
{
|
|
ImGui::SliderFloat( "Suspension Hertz", &m_suspensionHertz, 0.5f, 5.0f, "%.1f" );
|
|
ImGui::SliderFloat( "Suspension Damping", &m_suspensionDampingRatio, 0.0f, 2.0f, "%.2f" );
|
|
ImGui::SliderFloat( "Tyre Friction", &m_friction, 0.0f, 2.0f, "%.2f" );
|
|
|
|
bool thirdPerson = m_camera->m_thirdPerson;
|
|
if ( ImGui::Checkbox( "Third Person (T)", &thirdPerson ) )
|
|
{
|
|
ToggleThirdPerson();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Apply the forces of every wheel to the chassis for the coming step
|
|
void UpdateWheels( float timeStep, float throttle, float brake, float steer )
|
|
{
|
|
b3WorldTransform transform = b3Body_GetTransform( m_chassisId );
|
|
b3Vec3 forward = b3RotateVector( transform.q, b3Vec3_axisX );
|
|
|
|
// The acceleration the driver asks for along the chassis' nose. The brake opposes the
|
|
// chassis' motion and is capped at what stops it this step, so it never drives it
|
|
// backwards.
|
|
float speed = b3Dot( b3Body_GetLinearVelocity( m_chassisId ), forward );
|
|
float acceleration = m_engineAcceleration * throttle;
|
|
float stopping = b3MinFloat( m_brakeDeceleration * brake, b3AbsFloat( speed ) / timeStep );
|
|
acceleration -= speed > 0.0f ? stopping : -stopping;
|
|
|
|
for ( int i = 0; i < m_wheelCount; ++i )
|
|
{
|
|
UpdateWheel( m_wheels[i], transform, timeStep, acceleration, steer );
|
|
}
|
|
}
|
|
|
|
// Cast one wheel's ray and apply its suspension and tyre forces to the chassis
|
|
void UpdateWheel( Wheel& wheel, b3WorldTransform transform, float timeStep, float acceleration,
|
|
float steer )
|
|
{
|
|
b3Vec3 up = b3RotateVector( transform.q, b3Vec3_axisY );
|
|
b3Vec3 forward = b3RotateVector( transform.q, b3Vec3_axisX );
|
|
|
|
// The ray reaches from the mount down through the spring's travel and the wheel's radius
|
|
float reach = m_suspensionTravel + m_wheelRadius;
|
|
b3Vec3 translation = b3MulSV( -reach, up );
|
|
|
|
b3QueryFilter filter = b3DefaultQueryFilter();
|
|
filter.categoryBits = m_vehicleBit;
|
|
filter.maskBits = ~m_vehicleBit;
|
|
|
|
b3Pos mount = b3TransformWorldPoint( transform, wheel.localMount );
|
|
b3RayResult hit = b3World_CastRayClosest( m_worldId, mount, translation, filter );
|
|
wheel.grounded = hit.hit;
|
|
float fraction = hit.hit ? hit.fraction : 1.0f;
|
|
wheel.suspensionLength = fraction * reach - m_wheelRadius;
|
|
|
|
// A wheel turns at the speed of the ground under it, and keeps that speed in the air
|
|
wheel.spinAngle = b3UnwindAngle( wheel.spinAngle + wheel.spinSpeed * timeStep );
|
|
|
|
if ( hit.hit == false )
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Each wheel carries an equal share of the mass. The spring rate gives that share the
|
|
// requested natural frequency.
|
|
float share = b3Body_GetMass( m_chassisId ) / m_wheelCount;
|
|
float omega = 2.0f * B3_PI * m_suspensionHertz;
|
|
float stiffness = share * omega * omega;
|
|
float damping = 2.0f * m_suspensionDampingRatio * share * omega;
|
|
|
|
// Suspension. A spring only pushes: a negative load would pull the chassis onto the
|
|
// ground and hold it there through a jump.
|
|
float compression = ( 1.0f - hit.fraction ) * reach;
|
|
float compressionSpeed = -b3Dot( b3Body_GetWorldPointVelocity( m_chassisId, mount ), up );
|
|
float load = b3MaxFloat( stiffness * compression + damping * compressionSpeed, 0.0f );
|
|
b3Body_ApplyForce( m_chassisId, b3MulSV( load, hit.normal ), hit.point, true );
|
|
|
|
// The wheel's rolling direction in the contact plane, and the direction across it
|
|
b3Vec3 heading = forward;
|
|
if ( wheel.steered )
|
|
{
|
|
heading = b3RotateVector( b3MakeQuatFromAxisAngle( up, steer ), forward );
|
|
}
|
|
b3Vec3 rolling =
|
|
b3Normalize( b3MulAdd( heading, -b3Dot( heading, hit.normal ), hit.normal ) );
|
|
b3Vec3 lateral = b3Cross( hit.normal, rolling );
|
|
|
|
// The lateral force cancels the contact point's sideways sliding in one step. The rolling
|
|
// force gives this wheel's share of the mass the requested acceleration, resolved onto the
|
|
// wheel, so a steered wheel puts less of it into the nose direction.
|
|
b3Vec3 contactVelocity = b3Body_GetWorldPointVelocity( m_chassisId, hit.point );
|
|
float lateralForce = -share * b3Dot( contactVelocity, lateral ) / timeStep;
|
|
float rollingForce = share * acceleration * b3Dot( forward, rolling );
|
|
|
|
// Rolling forward along +x turns the wheel backwards about its axle, the chassis' +z
|
|
wheel.spinSpeed = -b3Dot( contactVelocity, rolling ) / m_wheelRadius;
|
|
|
|
// Friction circle: the tyre cannot give more than friction times the load on it
|
|
float maxForce = m_friction * load;
|
|
float magnitude = sqrtf( lateralForce * lateralForce + rollingForce * rollingForce );
|
|
if ( magnitude > maxForce )
|
|
{
|
|
lateralForce *= maxForce / magnitude;
|
|
rollingForce *= maxForce / magnitude;
|
|
}
|
|
|
|
b3Vec3 tyreForce =
|
|
b3Add( b3MulSV( rollingForce, rolling ), b3MulSV( lateralForce, lateral ) );
|
|
b3Body_ApplyForce( m_chassisId, tyreForce, hit.point, true );
|
|
}
|
|
|
|
void Step() override
|
|
{
|
|
float throttle = 0.0f;
|
|
throttle += IsKeyDown( KEY_W ) ? 1.0f : 0.0f;
|
|
throttle -= IsKeyDown( KEY_S ) ? 1.0f : 0.0f;
|
|
float brake = IsKeyDown( KEY_SPACE ) ? 1.0f : 0.0f;
|
|
|
|
m_steeringAngle = 0.0f;
|
|
m_steeringAngle += IsKeyDown( KEY_A ) ? m_maxSteeringAngle : 0.0f;
|
|
m_steeringAngle -= IsKeyDown( KEY_D ) ? m_maxSteeringAngle : 0.0f;
|
|
|
|
// Forces accumulate until the world steps, so apply them only on a step that runs
|
|
bool stepping = m_context->pause == false || m_context->singleStep > 0;
|
|
if ( stepping && m_context->hertz > 0.0f )
|
|
{
|
|
UpdateWheels( 1.0f / m_context->hertz, throttle, brake, m_steeringAngle );
|
|
}
|
|
|
|
if ( m_camera->m_thirdPerson )
|
|
{
|
|
m_camera->m_pivot = b3Body_GetPosition( m_chassisId );
|
|
m_camera->UpdateTransform();
|
|
}
|
|
|
|
Sample::Step();
|
|
}
|
|
|
|
void Render() override
|
|
{
|
|
Sample::Render();
|
|
|
|
b3WorldTransform transform = b3Body_GetTransform( m_chassisId );
|
|
b3Vec3 down = b3RotateVector( transform.q, b3Neg( b3Vec3_axisY ) );
|
|
|
|
// The wheel cylinder is along y. This turns it onto the axle, the chassis' z.
|
|
b3Quat cylinderToAxle = b3MakeQuatFromAxisAngle( b3Vec3_axisX, 0.5f * B3_PI );
|
|
|
|
for ( int i = 0; i < m_wheelCount; ++i )
|
|
{
|
|
const Wheel& wheel = m_wheels[i];
|
|
b3Pos mount = b3TransformWorldPoint( transform, wheel.localMount );
|
|
b3Pos center = b3OffsetPos( mount, b3MulSV( wheel.suspensionLength, down ) );
|
|
|
|
float steer = wheel.steered ? m_steeringAngle : 0.0f;
|
|
b3Quat q = b3MulQuat( transform.q, b3MakeQuatFromAxisAngle( b3Vec3_axisY, steer ) );
|
|
q = b3MulQuat( q, b3MakeQuatFromAxisAngle( b3Vec3_axisZ, wheel.spinAngle ) );
|
|
q = b3MulQuat( q, cylinderToAxle );
|
|
|
|
b3HexColor color = wheel.grounded ? b3_colorGreen : b3_colorRed;
|
|
DrawLine( mount, center, MakeColor( color ) );
|
|
DrawHull( { center, q }, m_wheelHull, MakeColor( b3_colorGray ) );
|
|
}
|
|
|
|
b3Vec3 forward = b3RotateVector( b3Body_GetRotation( m_chassisId ), b3Vec3_axisX );
|
|
float speed = b3Dot( b3Body_GetLinearVelocity( m_chassisId ), forward );
|
|
DrawTextLine( "speed = %.1f m/s", speed );
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new RaycastVehicle( context );
|
|
}
|
|
|
|
b3BodyId m_chassisId;
|
|
Wheel m_wheels[m_wheelCount];
|
|
b3HullData* m_wheelHull;
|
|
|
|
float m_suspensionHertz;
|
|
float m_suspensionDampingRatio;
|
|
float m_friction;
|
|
float m_wheelRadius;
|
|
float m_suspensionTravel;
|
|
float m_engineAcceleration;
|
|
float m_brakeDeceleration;
|
|
float m_maxSteeringAngle;
|
|
float m_steeringAngle;
|
|
};
|
|
|
|
static int sampleRaycastVehicle =
|
|
RegisterSample( "Vehicle", "Raycast Vehicle", RaycastVehicle::Create );
|
|
|
|
// A user wants the load on a static body C when a box A lands on it, sometimes through a thin sleeping
|
|
// plate B. Hit events report approach speed, and a resting plate has none against C, so C gets no hit
|
|
// event when B is in the middle. Summing the contact impulses on C each step gives the load either way.
|
|
class StaticLoad : public Sample
|
|
{
|
|
public:
|
|
static constexpr float m_boxHalf = 1.0f;
|
|
static constexpr float m_tableTop = 3.0f;
|
|
static constexpr float m_dropX = 1.5f;
|
|
static constexpr int m_settleSteps = 10;
|
|
static constexpr int m_historyCapacity = 240;
|
|
static constexpr int m_maxHits = 12;
|
|
|
|
explicit StaticLoad( SampleContext* context )
|
|
: Sample( context )
|
|
{
|
|
if ( context->restart == false )
|
|
{
|
|
m_camera->SetView( 20.0f, 15.0f, 22.0f, { 0.0f, 5.0f, 0.0f } );
|
|
}
|
|
|
|
// The weld to the wall lives in game code, so Box3D only sees two static bodies
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.name = "wall";
|
|
bodyDef.position = { -4.25f, 4.0f, 0.0f };
|
|
b3BodyId wallId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 0.25f, 4.0f, 3.0f );
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
b3CreateHullShape( wallId, &shapeDef, &box.base );
|
|
}
|
|
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.name = "C";
|
|
bodyDef.position = { 0.0f, m_tableTop - 1.0f, 0.0f };
|
|
m_bodyC = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 4.0f, 1.0f, 2.0f );
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
shapeDef.enableHitEvents = true;
|
|
m_shapeC = b3CreateHullShape( m_bodyC, &shapeDef, &box.base );
|
|
}
|
|
|
|
Reset();
|
|
}
|
|
|
|
void Reset()
|
|
{
|
|
if ( B3_IS_NON_NULL( m_bodyA ) )
|
|
{
|
|
b3DestroyBody( m_bodyA );
|
|
}
|
|
|
|
if ( B3_IS_NON_NULL( m_bodyB ) )
|
|
{
|
|
b3DestroyBody( m_bodyB );
|
|
}
|
|
|
|
m_bodyA = b3_nullBodyId;
|
|
m_bodyB = b3_nullBodyId;
|
|
m_shapeA = b3_nullShapeId;
|
|
m_shapeB = b3_nullShapeId;
|
|
|
|
m_resetStep = m_stepCount;
|
|
m_lastStep = m_stepCount;
|
|
m_dropped = false;
|
|
m_massA = 0.0f;
|
|
m_massB = 0.0f;
|
|
m_impactStep = -1;
|
|
m_impactSpeed = 0.0f;
|
|
m_excessImpulse = 0.0f;
|
|
m_stepImpulseC = 0.0f;
|
|
m_stepImpulseAB = 0.0f;
|
|
m_stepImpulseAC = 0.0f;
|
|
m_stepImpulseBC = 0.0f;
|
|
m_peakForceC = 0.0f;
|
|
m_peakForceAB = 0.0f;
|
|
m_peakForceAC = 0.0f;
|
|
m_peakForceBC = 0.0f;
|
|
m_peakStep = -1;
|
|
m_hitCountC = 0;
|
|
m_hitCountB = 0;
|
|
m_hitCount = 0;
|
|
m_historyCount = 0;
|
|
m_historyIndex = 0;
|
|
|
|
if ( m_includeB )
|
|
{
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.name = "B";
|
|
bodyDef.position = { m_dropX, m_tableTop + m_plateHalfHeight, 0.0f };
|
|
m_bodyB = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 1.5f, m_plateHalfHeight, 1.0f );
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
shapeDef.density = m_densityB;
|
|
shapeDef.enableHitEvents = true;
|
|
m_shapeB = b3CreateHullShape( m_bodyB, &shapeDef, &box.base );
|
|
}
|
|
else
|
|
{
|
|
DropA();
|
|
}
|
|
}
|
|
|
|
// B rests on C long enough to build its contact, then sleeps the way a settled game object would
|
|
void DropA()
|
|
{
|
|
float baseY = m_tableTop;
|
|
if ( m_includeB )
|
|
{
|
|
if ( m_sleepB )
|
|
{
|
|
b3Body_SetAwake( m_bodyB, false );
|
|
}
|
|
|
|
baseY += 2.0f * m_plateHalfHeight;
|
|
}
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
bodyDef.type = b3_dynamicBody;
|
|
bodyDef.name = "A";
|
|
bodyDef.position = { m_dropX, baseY + m_dropHeight + m_boxHalf, 0.0f };
|
|
m_bodyA = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
b3BoxHull box = b3MakeBoxHull( m_boxHalf, m_boxHalf, m_boxHalf );
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
shapeDef.density = m_densityA;
|
|
shapeDef.enableHitEvents = true;
|
|
m_shapeA = b3CreateHullShape( m_bodyA, &shapeDef, &box.base );
|
|
|
|
m_massA = b3Body_GetMass( m_bodyA );
|
|
m_massB = m_includeB ? b3Body_GetMass( m_bodyB ) : 0.0f;
|
|
m_dropped = true;
|
|
}
|
|
|
|
const char* Label( b3ShapeId shapeId ) const
|
|
{
|
|
if ( B3_ID_EQUALS( shapeId, m_shapeA ) )
|
|
{
|
|
return "A";
|
|
}
|
|
|
|
if ( B3_ID_EQUALS( shapeId, m_shapeB ) )
|
|
{
|
|
return "B";
|
|
}
|
|
|
|
if ( B3_ID_EQUALS( shapeId, m_shapeC ) )
|
|
{
|
|
return "C";
|
|
}
|
|
|
|
return "?";
|
|
}
|
|
|
|
// Total normal impulse a contact applied over the step, sub-steps and restitution included.
|
|
// A speculative point that never pushed contributes zero.
|
|
static float ContactImpulse( const b3ContactData& data )
|
|
{
|
|
float impulse = 0.0f;
|
|
for ( int i = 0; i < data.manifoldCount; ++i )
|
|
{
|
|
const b3Manifold& manifold = data.manifolds[i];
|
|
for ( int j = 0; j < manifold.pointCount; ++j )
|
|
{
|
|
impulse += manifold.points[j].totalNormalImpulse;
|
|
}
|
|
}
|
|
|
|
return impulse;
|
|
}
|
|
|
|
// Sum the impulse on a shape from every touching contact, or only the one against a given shape
|
|
float ShapeImpulse( b3ShapeId shapeId, b3ShapeId otherId, bool drawArrows )
|
|
{
|
|
if ( B3_IS_NULL( shapeId ) )
|
|
{
|
|
return 0.0f;
|
|
}
|
|
|
|
int capacity = b3Shape_GetContactCapacity( shapeId );
|
|
m_contactData.resize( capacity );
|
|
int count = b3Shape_GetContactData( shapeId, m_contactData.data(), capacity );
|
|
|
|
float hertz = m_context->hertz;
|
|
float arrowScale = m_restingWeight > 0.0f ? 1.0f / m_restingWeight : 0.0f;
|
|
|
|
float impulse = 0.0f;
|
|
for ( int i = 0; i < count; ++i )
|
|
{
|
|
const b3ContactData& data = m_contactData[i];
|
|
bool isShapeA = B3_ID_EQUALS( data.shapeIdA, shapeId );
|
|
b3ShapeId other = isShapeA ? data.shapeIdB : data.shapeIdA;
|
|
if ( B3_IS_NON_NULL( otherId ) && B3_ID_EQUALS( other, otherId ) == false )
|
|
{
|
|
continue;
|
|
}
|
|
|
|
impulse += ContactImpulse( data );
|
|
|
|
if ( drawArrows == false )
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// The normal points from shape A to shape B, so it pushes on B and pulls on A
|
|
b3Pos center = b3Body_GetWorldCenter( b3Shape_GetBody( data.shapeIdA ) );
|
|
for ( int j = 0; j < data.manifoldCount; ++j )
|
|
{
|
|
const b3Manifold& manifold = data.manifolds[j];
|
|
b3Vec3 direction = isShapeA ? -manifold.normal : manifold.normal;
|
|
for ( int k = 0; k < manifold.pointCount; ++k )
|
|
{
|
|
const b3ManifoldPoint& mp = manifold.points[k];
|
|
if ( mp.totalNormalImpulse <= 0.0f )
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// One meter per resting weight, clamped so an impact spike stays on screen
|
|
float length = b3MinFloat( mp.totalNormalImpulse * hertz * arrowScale, 4.0f );
|
|
b3Pos p = b3OffsetPos( center, mp.anchorA );
|
|
DrawArrow( b3OffsetPos( p, -length * direction ), p, MakeColor( b3_colorRed ) );
|
|
}
|
|
}
|
|
}
|
|
|
|
return impulse;
|
|
}
|
|
|
|
void Step() override
|
|
{
|
|
b3Vec3 velocityA = B3_IS_NON_NULL( m_bodyA ) ? b3Body_GetLinearVelocity( m_bodyA ) : b3Vec3_zero;
|
|
|
|
Sample::Step();
|
|
|
|
float gravity = -b3World_GetGravity( m_worldId ).y;
|
|
m_restingWeight = ( m_massA + m_massB ) * gravity;
|
|
|
|
// A thin B leaves A within speculative reach of C, so part of the load can skip B entirely
|
|
m_stepImpulseC = ShapeImpulse( m_shapeC, b3_nullShapeId, true );
|
|
m_stepImpulseAC = ShapeImpulse( m_shapeC, m_shapeA, false );
|
|
m_stepImpulseBC = ShapeImpulse( m_shapeC, m_shapeB, false );
|
|
m_stepImpulseAB = ShapeImpulse( m_shapeA, m_shapeB, false );
|
|
float impulseA = ShapeImpulse( m_shapeA, b3_nullShapeId, false );
|
|
|
|
// Pausing keeps the last step's events, so only consume a step once
|
|
if ( m_stepCount != m_lastStep )
|
|
{
|
|
m_lastStep = m_stepCount;
|
|
ProcessStep( impulseA, velocityA );
|
|
}
|
|
|
|
DrawHits();
|
|
DrawStats();
|
|
}
|
|
|
|
void ProcessStep( float impulseA, b3Vec3 velocityA )
|
|
{
|
|
if ( m_dropped == false )
|
|
{
|
|
if ( m_stepCount - m_resetStep >= m_settleSteps )
|
|
{
|
|
DropA();
|
|
}
|
|
return;
|
|
}
|
|
|
|
float hertz = m_context->hertz;
|
|
float timeStep = hertz > 0.0f ? 1.0f / hertz : 0.0f;
|
|
|
|
if ( m_impactStep < 0 && impulseA > 0.0f )
|
|
{
|
|
m_impactStep = m_stepCount;
|
|
m_impactSpeed = -velocityA.y;
|
|
|
|
if ( m_pauseOnImpact )
|
|
{
|
|
m_context->pause = true;
|
|
}
|
|
}
|
|
|
|
// From impact on, the impulse on C beyond the resting weight removes the momentum A brought in
|
|
if ( m_impactStep >= 0 )
|
|
{
|
|
m_excessImpulse += m_stepImpulseC - m_restingWeight * timeStep;
|
|
}
|
|
|
|
float forceC = m_stepImpulseC * hertz;
|
|
float forceAB = m_stepImpulseAB * hertz;
|
|
if ( forceC > m_peakForceC )
|
|
{
|
|
m_peakForceC = forceC;
|
|
m_peakStep = m_stepCount;
|
|
}
|
|
|
|
m_peakForceAB = b3MaxFloat( m_peakForceAB, forceAB );
|
|
m_peakForceAC = b3MaxFloat( m_peakForceAC, m_stepImpulseAC * hertz );
|
|
m_peakForceBC = b3MaxFloat( m_peakForceBC, m_stepImpulseBC * hertz );
|
|
|
|
m_historyC[m_historyIndex] = forceC;
|
|
m_historyAB[m_historyIndex] = forceAB;
|
|
m_historyIndex = ( m_historyIndex + 1 ) % m_historyCapacity;
|
|
m_historyCount = b3MinInt( m_historyCount + 1, m_historyCapacity );
|
|
|
|
b3ContactEvents events = b3World_GetContactEvents( m_worldId );
|
|
for ( int i = 0; i < events.hitCount; ++i )
|
|
{
|
|
const b3ContactHitEvent& event = events.hitEvents[i];
|
|
bool touchesC = B3_ID_EQUALS( event.shapeIdA, m_shapeC ) || B3_ID_EQUALS( event.shapeIdB, m_shapeC );
|
|
bool touchesB = B3_ID_EQUALS( event.shapeIdA, m_shapeB ) || B3_ID_EQUALS( event.shapeIdB, m_shapeB );
|
|
m_hitCountC += touchesC ? 1 : 0;
|
|
m_hitCountB += touchesB ? 1 : 0;
|
|
|
|
HitRecord record = {};
|
|
record.event = event;
|
|
record.step = m_stepCount;
|
|
if ( b3Contact_IsValid( event.contactId ) )
|
|
{
|
|
record.impulse = ContactImpulse( b3Contact_GetData( event.contactId ) );
|
|
}
|
|
|
|
// Newest first, dropping the oldest when full
|
|
int count = b3MinInt( m_hitCount, m_maxHits - 1 );
|
|
for ( int j = count; j > 0; --j )
|
|
{
|
|
m_hits[j] = m_hits[j - 1];
|
|
}
|
|
|
|
m_hits[0] = record;
|
|
m_hitCount = count + 1;
|
|
}
|
|
}
|
|
|
|
void DrawHits()
|
|
{
|
|
for ( int i = 0; i < m_hitCount; ++i )
|
|
{
|
|
const b3ContactHitEvent& event = m_hits[i].event;
|
|
b3Pos p1 = event.point;
|
|
b3Pos p2 = b3OffsetPos( p1, -event.approachSpeed * event.normal );
|
|
DrawPoint( p1, 10.0f, MakeColor( b3_colorYellow ) );
|
|
DrawLine( p1, p2, MakeColor( b3_colorYellow ) );
|
|
DrawString3D( p1, MakeColor( b3_colorWhite ), "%s-%s %.1f m/s", Label( event.shapeIdA ),
|
|
Label( event.shapeIdB ), event.approachSpeed );
|
|
}
|
|
}
|
|
|
|
void DrawStats()
|
|
{
|
|
float hertz = m_context->hertz;
|
|
|
|
if ( m_dropped == false )
|
|
{
|
|
DrawTextLine( "B settling on C" );
|
|
return;
|
|
}
|
|
|
|
DrawTextLine( "mass A = %.2f kg, mass B = %.2f kg, resting weight on C = %.1f N", m_massA, m_massB,
|
|
m_restingWeight );
|
|
|
|
if ( m_includeB )
|
|
{
|
|
DrawTextLine( "B is %s", b3Body_IsAwake( m_bodyB ) ? "awake" : "asleep" );
|
|
}
|
|
|
|
if ( m_impactStep < 0 )
|
|
{
|
|
DrawTextLine( "A falling" );
|
|
return;
|
|
}
|
|
|
|
float momentum = m_massA * m_impactSpeed;
|
|
DrawTextLine( "impact at step %d, speed %.2f m/s, momentum %.1f N*s", m_impactStep - m_resetStep,
|
|
m_impactSpeed, momentum );
|
|
|
|
// No continuous collision between dynamic bodies, so A can sink a full step of travel into B before
|
|
// they get a contact. If that reaches within speculative distance of C, A loads C directly.
|
|
if ( m_includeB )
|
|
{
|
|
DrawTextLine( "A travels %.2f m per step, B is %.2f m thick", m_impactSpeed / hertz,
|
|
2.0f * m_plateHalfHeight );
|
|
}
|
|
DrawTextLine( "step load on C = %.1f N (impulse %.3f N*s)", m_stepImpulseC * hertz, m_stepImpulseC );
|
|
DrawTextLine( "peak load on C = %.1f N at step %d, %.1fx resting weight", m_peakForceC, m_peakStep - m_resetStep,
|
|
m_restingWeight > 0.0f ? m_peakForceC / m_restingWeight : 0.0f );
|
|
|
|
if ( m_includeB )
|
|
{
|
|
DrawTextLine( " from B = %.1f N, peak %.1f N", m_stepImpulseBC * hertz, m_peakForceBC );
|
|
DrawTextLine( " from A directly = %.1f N, peak %.1f N", m_stepImpulseAC * hertz, m_peakForceAC );
|
|
DrawTextLine( "load A on B = %.1f N, peak %.1f N", m_stepImpulseAB * hertz, m_peakForceAB );
|
|
}
|
|
|
|
DrawTextLine( "impulse on C beyond resting weight = %.1f N*s (A brought %.1f N*s)", m_excessImpulse,
|
|
momentum );
|
|
DrawTextLine( "hit events touching C = %d, touching B = %d", m_hitCountC, m_hitCountB );
|
|
|
|
for ( int i = 0; i < m_hitCount; ++i )
|
|
{
|
|
const HitRecord& record = m_hits[i];
|
|
DrawTextLine( " step %d: %s-%s approach %.2f m/s, contact impulse %.3f N*s", record.step - m_resetStep,
|
|
Label( record.event.shapeIdA ), Label( record.event.shapeIdB ), record.event.approachSpeed,
|
|
record.impulse );
|
|
}
|
|
}
|
|
|
|
bool DrawControls() override
|
|
{
|
|
bool reset = false;
|
|
reset |= ImGui::Checkbox( "Include B", &m_includeB );
|
|
reset |= ImGui::Checkbox( "B Asleep", &m_sleepB );
|
|
reset |= ImGui::SliderFloat( "B Half Thickness", &m_plateHalfHeight, 0.02f, 0.5f, "%.2f m" );
|
|
reset |= ImGui::SliderFloat( "B Density", &m_densityB, 100.0f, 10000.0f, "%.0f kg/m^3",
|
|
ImGuiSliderFlags_Logarithmic );
|
|
reset |= ImGui::SliderFloat( "A Density", &m_densityA, 100.0f, 10000.0f, "%.0f kg/m^3",
|
|
ImGuiSliderFlags_Logarithmic );
|
|
reset |= ImGui::SliderFloat( "Drop Height", &m_dropHeight, 0.0f, 10.0f, "%.1f m" );
|
|
|
|
float threshold = b3World_GetHitEventThreshold( m_worldId );
|
|
if ( ImGui::SliderFloat( "Hit Threshold", &threshold, 0.0f, 5.0f, "%.2f m/s" ) )
|
|
{
|
|
b3World_SetHitEventThreshold( m_worldId, threshold );
|
|
}
|
|
|
|
ImGui::Checkbox( "Pause On Impact", &m_pauseOnImpact );
|
|
|
|
if ( ImGui::Button( "Drop Again" ) )
|
|
{
|
|
reset = true;
|
|
}
|
|
|
|
if ( reset )
|
|
{
|
|
Reset();
|
|
}
|
|
|
|
int offset = m_historyCount < m_historyCapacity ? 0 : m_historyIndex;
|
|
ImGui::PlotLines( "##loadC", m_historyC, m_historyCount, offset, "load on C (N)", 0.0f, FLT_MAX,
|
|
ImVec2( 0.0f, 80.0f ) );
|
|
ImGui::PlotLines( "##loadAB", m_historyAB, m_historyCount, offset, "load A on B (N)", 0.0f, FLT_MAX,
|
|
ImVec2( 0.0f, 80.0f ) );
|
|
|
|
return true;
|
|
}
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
{
|
|
return new StaticLoad( context );
|
|
}
|
|
|
|
struct HitRecord
|
|
{
|
|
b3ContactHitEvent event;
|
|
float impulse;
|
|
int step;
|
|
};
|
|
|
|
std::vector<b3ContactData> m_contactData;
|
|
|
|
b3BodyId m_bodyA = {};
|
|
b3BodyId m_bodyB = {};
|
|
b3BodyId m_bodyC = {};
|
|
b3ShapeId m_shapeA = {};
|
|
b3ShapeId m_shapeB = {};
|
|
b3ShapeId m_shapeC = {};
|
|
|
|
HitRecord m_hits[m_maxHits] = {};
|
|
float m_historyC[m_historyCapacity] = {};
|
|
float m_historyAB[m_historyCapacity] = {};
|
|
|
|
float m_dropHeight = 3.0f;
|
|
float m_plateHalfHeight = 0.15f;
|
|
float m_densityA = 1000.0f;
|
|
float m_densityB = 1000.0f;
|
|
float m_massA = 0.0f;
|
|
float m_massB = 0.0f;
|
|
float m_restingWeight = 0.0f;
|
|
float m_impactSpeed = 0.0f;
|
|
float m_excessImpulse = 0.0f;
|
|
float m_stepImpulseC = 0.0f;
|
|
float m_stepImpulseAB = 0.0f;
|
|
float m_stepImpulseAC = 0.0f;
|
|
float m_stepImpulseBC = 0.0f;
|
|
float m_peakForceC = 0.0f;
|
|
float m_peakForceAB = 0.0f;
|
|
float m_peakForceAC = 0.0f;
|
|
float m_peakForceBC = 0.0f;
|
|
|
|
int m_resetStep = 0;
|
|
int m_lastStep = 0;
|
|
int m_impactStep = -1;
|
|
int m_peakStep = -1;
|
|
int m_hitCountC = 0;
|
|
int m_hitCountB = 0;
|
|
int m_hitCount = 0;
|
|
int m_historyCount = 0;
|
|
int m_historyIndex = 0;
|
|
|
|
bool m_includeB = true;
|
|
bool m_sleepB = true;
|
|
bool m_pauseOnImpact = false;
|
|
bool m_dropped = false;
|
|
};
|
|
|
|
static int sampleStaticLoad = RegisterSample( "Issues", "Static Load", StaticLoad::Create );
|