box3d/samples/sample_mesh.cpp

2247 lines
57 KiB
C++
Raw Normal View History

2026-05-10 13:36:52 -07:00
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT
#include "human.h"
#include "mesh_loader.h"
#include "sample.h"
#include "gfx/draw.h"
#include "gfx/debug_adapter.h"
2026-05-10 13:36:52 -07:00
#include "box3d/box3d.h"
#include <imgui.h>
#include <queue>
#include <stdio.h>
enum class ShapeType
{
sphere,
capsule,
box,
cylinder
};
class GridMesh : public Sample
{
public:
explicit GridMesh( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 6.0f, b3Pos_zero );
2026-05-10 13:36:52 -07:00
}
b3BodyDef bodyDef = b3DefaultBodyDef();
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
m_gridMesh = b3CreateGridMesh( 20, 20, 1.0f, 0, true );
m_scale = b3Vec3_one;
m_scale = { 2.0f, 2.0f, 2.0f };
// m_scale = { -1.2f, 2.0f, 4.0f };
// m_scale = { 8.0f, 8.0f, 8.0f };
// m_scale = { 0.5f, 0.5f, 0.5f };
// m_scale = { 0.25f, 0.25f, 0.25f };
2026-05-10 13:36:52 -07:00
b3ShapeDef shapeDef = b3DefaultShapeDef();
m_gridShapeId = b3CreateMeshShape( groundId, &shapeDef, m_gridMesh, m_scale );
m_shapeType = ShapeType::cylinder;
m_bodyId = b3_nullBodyId;
m_cylinderHull = b3CreateCylinder( 1.0f, 0.25f, 0.0f, 15 );
// m_cylinderHull = b3CreateCylinder( 0.5f, 0.2f, 0.0f, 15 );
2026-05-10 13:36:52 -07:00
Spawn();
GetGuiDraw()->forceScale = 0.01f;
2026-05-10 13:36:52 -07:00
}
~GridMesh() override
{
b3DestroyHull( m_cylinderHull );
b3DestroyMesh( m_gridMesh );
}
void Spawn()
{
if ( B3_IS_NON_NULL( m_bodyId ) )
{
b3DestroyBody( m_bodyId );
}
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
// bodyDef.position = { 0.5f, 2.0f, -0.5f };
2026-05-10 13:36:52 -07:00
bodyDef.position = { 0.1f, 1.0f, -0.1f };
// bodyDef.enableContactRecycling = m_shapeType != ShapeType::cylinder;
bodyDef.angularDamping = m_shapeType == ShapeType::cylinder ? 0.1f : 0.0f;
2026-05-10 13:36:52 -07:00
// bodyDef.linearVelocity = { 2.0f, 0.0f, 0.0f };
// bodyDef.position = { 0.208701894, 0.0791450217, -0.298710823 };
// bodyDef.rotation = { { 0.0808477178, -0.00305216131, 0.000387475739 }, 0.996721804 };
// bodyDef.linearVelocity = { 7.28029263e-05, 0.000761006493, 0.0104003791 };
// bodyDef.angularVelocity = { 0.116714634, -0.00774557842, -0.000242586044 };
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
switch ( m_shapeType )
{
case ShapeType::sphere:
{
shapeDef.baseMaterial.rollingResistance = 0.05f;
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f };
b3CreateSphereShape( m_bodyId, &shapeDef, &sphere );
}
break;
case ShapeType::capsule:
{
shapeDef.baseMaterial.rollingResistance = 0.05f;
b3Capsule capsule = { { 0.0f, 0.0f, 1.276f }, { 0.0f, 0.0f, 0.476f }, 0.15f };
b3CreateCapsuleShape( m_bodyId, &shapeDef, &capsule );
}
break;
case ShapeType::box:
{
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
// shapeDef.baseMaterial.friction = 0.01f;
2026-05-10 13:36:52 -07:00
b3CreateHullShape( m_bodyId, &shapeDef, &box.base );
}
break;
case ShapeType::cylinder:
{
shapeDef.baseMaterial.rollingResistance = 0.02f;
2026-05-10 13:36:52 -07:00
b3CreateHullShape( m_bodyId, &shapeDef, m_cylinderHull );
}
break;
default:
assert( false );
}
}
bool DrawControls() override
2026-05-10 13:36:52 -07:00
{
if ( ImGui::RadioButton( "Sphere", m_shapeType == ShapeType::sphere ) )
{
m_shapeType = ShapeType::sphere;
Spawn();
}
if ( ImGui::RadioButton( "Capsule", m_shapeType == ShapeType::capsule ) )
{
m_shapeType = ShapeType::capsule;
Spawn();
}
if ( ImGui::RadioButton( "Box", m_shapeType == ShapeType::box ) )
{
m_shapeType = ShapeType::box;
Spawn();
}
if ( ImGui::RadioButton( "Cylinder", m_shapeType == ShapeType::cylinder ) )
{
m_shapeType = ShapeType::cylinder;
Spawn();
}
b3Vec3 scale = m_scale;
bool changed = false;
changed = changed || ImGui::SliderFloat( "Scale X", &scale.x, -2.0f, 2.0f, "%.1f" );
changed = changed || ImGui::SliderFloat( "Scale Z", &scale.z, -2.0f, 2.0f, "%.1f" );
if ( changed )
{
m_scale = scale;
b3Shape_SetMesh( m_gridShapeId, m_gridMesh, m_scale );
}
return true;
2026-05-10 13:36:52 -07:00
}
void Render() override
{
Sample::Render();
DrawTextLine( "triangle count = %d, bytes = %d", m_gridMesh->triangleCount, m_gridMesh->byteCount );
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
2026-05-10 13:36:52 -07:00
}
void Step() override
{
if ( m_stepCount == 83 )
{
// m_context->settings.pause = true;
}
if ( m_stepCount < 100 )
{
// b3Vec3 p = b3Body_GetPosition( m_bodyId );
// printf( "%d : %g %g %g\n", m_stepCount, p.x, p.y, p.z );
// b3Vec3 v = b3Body_GetLinearVelocity( m_bodyId );
// printf( "%d : %g %g %g\n", m_stepCount, v.x, v.y, v.z );
}
Sample::Step();
}
static Sample* Create( SampleContext* context )
{
return new GridMesh( context );
}
b3HullData* m_cylinderHull;
2026-05-10 13:36:52 -07:00
ShapeType m_shapeType;
b3BodyId m_bodyId;
b3MeshData* m_gridMesh;
b3ShapeId m_gridShapeId;
b3Vec3 m_scale;
};
static int sampleGridMesh = RegisterSample( "Mesh", "Grid", GridMesh::Create );
2026-05-10 13:36:52 -07:00
class BigBoxMesh : public Sample
{
public:
explicit BigBoxMesh( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 6.0f, b3Pos_zero );
2026-05-10 13:36:52 -07:00
}
b3BodyDef bodyDef = b3DefaultBodyDef();
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
m_boxMesh = b3CreateBoxMesh( {0.0f, -1.0f, 0.0f}, { 50.0f, 1.0f, 50.0f }, true );
2026-05-10 13:36:52 -07:00
m_scale = b3Vec3_one;
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.baseMaterial.friction = 0.5f;
// shapeDef.baseMaterial.restitution = 0.5f;
// shapeDef.baseMaterial.rollingResistance = 0.5f;
2026-05-10 13:36:52 -07:00
m_gridShapeId = b3CreateMeshShape( groundId, &shapeDef, m_boxMesh, m_scale );
m_shapeType = ShapeType::cylinder;
m_bodyId = b3_nullBodyId;
//m_cylinderHull = b3CreateCylinder( 0.5f, 0.2f, 0.0f, 15 );
m_cylinderHull = b3CreateCylinder( 0.3f, 0.15f, 0.0f, 32 );
2026-05-10 13:36:52 -07:00
m_stepWhilePaused = false;
Spawn();
}
~BigBoxMesh() override
{
b3DestroyHull( m_cylinderHull );
b3DestroyMesh( m_boxMesh );
}
void Spawn()
{
if ( B3_IS_NON_NULL( m_bodyId ) )
{
b3DestroyBody( m_bodyId );
}
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = { 0.5f, 0.0f, 0.0f };
2026-05-10 13:36:52 -07:00
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.baseMaterial.rollingResistance = 0.05f;
switch ( m_shapeType )
{
case ShapeType::sphere:
{
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f };
b3CreateSphereShape( m_bodyId, &shapeDef, &sphere );
}
break;
case ShapeType::capsule:
{
b3SurfaceMaterial material = b3DefaultSurfaceMaterial();
// material.friction = 0.0f;
// material.restitution = 1.0f;
2026-05-10 13:36:52 -07:00
material.rollingResistance = 0.1f;
shapeDef.materials = &material;
shapeDef.materialCount = 1;
b3Capsule capsule = { { 0.0f, 0.0f, 1.276f }, { 0.0f, 0.0f, 0.476f }, 0.15f };
b3CreateCapsuleShape( m_bodyId, &shapeDef, &capsule );
}
break;
case ShapeType::box:
{
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
// shapeDef.baseMaterial.friction = 0.01f;
2026-05-10 13:36:52 -07:00
b3CreateHullShape( m_bodyId, &shapeDef, &box.base );
}
break;
case ShapeType::cylinder:
{
b3CreateHullShape( m_bodyId, &shapeDef, m_cylinderHull );
}
break;
default:
assert( false );
}
}
bool DrawControls() override
2026-05-10 13:36:52 -07:00
{
if ( ImGui::RadioButton( "Sphere", m_shapeType == ShapeType::sphere ) )
{
m_shapeType = ShapeType::sphere;
Spawn();
}
if ( ImGui::RadioButton( "Capsule", m_shapeType == ShapeType::capsule ) )
{
m_shapeType = ShapeType::capsule;
Spawn();
}
if ( ImGui::RadioButton( "Box", m_shapeType == ShapeType::box ) )
{
m_shapeType = ShapeType::box;
Spawn();
}
if ( ImGui::RadioButton( "Cylinder", m_shapeType == ShapeType::cylinder ) )
{
m_shapeType = ShapeType::cylinder;
Spawn();
}
b3Vec3 scale = m_scale;
bool changed = false;
changed = changed || ImGui::SliderFloat( "Scale X", &scale.x, -2.0f, 2.0f, "%.1f" );
changed = changed || ImGui::SliderFloat( "Scale Z", &scale.z, -2.0f, 2.0f, "%.1f" );
if ( changed )
{
m_scale = scale;
b3Shape_SetMesh( m_gridShapeId, m_boxMesh, m_scale );
}
return true;
2026-05-10 13:36:52 -07:00
}
void Render() override
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
2026-05-10 13:36:52 -07:00
}
void Step() override
{
if ( m_stepCount == 83 )
{
// m_context->settings.pause = true;
}
if ( m_stepCount < 100 )
{
// b3Vec3 p = b3Body_GetPosition( m_bodyId );
// printf( "%d : %g %g %g\n", m_stepCount, p.x, p.y, p.z );
// b3Vec3 v = b3Body_GetLinearVelocity( m_bodyId );
// printf( "%d : %g %g %g\n", m_stepCount, v.x, v.y, v.z );
}
Sample::Step();
}
static Sample* Create( SampleContext* context )
{
return new BigBoxMesh( context );
}
b3HullData* m_cylinderHull;
2026-05-10 13:36:52 -07:00
ShapeType m_shapeType;
b3BodyId m_bodyId;
b3MeshData* m_boxMesh;
b3ShapeId m_gridShapeId;
b3Vec3 m_scale;
};
static int sampleBigBoxMesh = RegisterSample( "Mesh", "Big Box", BigBoxMesh::Create );
2026-05-10 13:36:52 -07:00
class BoxMesh : public Sample
{
public:
explicit BoxMesh( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 6.0f, b3Pos_zero );
2026-05-10 13:36:52 -07:00
}
AddGroundBox( 20.0f );
2026-05-10 13:36:52 -07:00
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.position = { 0.0f, -1.0f, 0.0f };
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisY, 0.25f * B3_PI );
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
m_boxMesh = b3CreateBoxMesh( { 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f }, true );
// m_scale = { -0.6f, 1.0f, 2.0f };
m_scale = b3Vec3_one;
m_boxShapeId = b3CreateMeshShape( groundId, &shapeDef, m_boxMesh, m_scale );
m_cylinderHull = b3CreateCylinder( 1.0f, 0.75f, 0.0f, 8 );
m_shapeType = ShapeType::box;
m_bodyId = b3_nullBodyId;
Spawn();
}
~BoxMesh() override
{
b3DestroyMesh( m_boxMesh );
b3DestroyHull( m_cylinderHull );
}
void Spawn()
{
if ( B3_IS_NON_NULL( m_bodyId ) )
{
b3DestroyBody( m_bodyId );
}
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = { 0.0f, 1.5f, 0.0f };
// bodyDef.position = { -0.2f, 1.5f, -1.25f };
2026-05-10 13:36:52 -07:00
if ( m_shapeType == ShapeType::cylinder )
{
bodyDef.position.y -= 0.5f;
}
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
switch ( m_shapeType )
{
case ShapeType::sphere:
{
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f };
b3CreateSphereShape( m_bodyId, &shapeDef, &sphere );
}
break;
case ShapeType::capsule:
{
b3Capsule capsule = { { -0.5f, 0.0f, 0.0f }, { 0.5f, 0.0f, 0.0f }, 0.1f };
b3CreateCapsuleShape( m_bodyId, &shapeDef, &capsule );
}
break;
case ShapeType::box:
{
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
// b3BoxHull box = b3MakeBoxHull( 0.1f, 1.0f, 0.1f );
b3CreateHullShape( m_bodyId, &shapeDef, &box.base );
}
break;
case ShapeType::cylinder:
{
b3CreateHullShape( m_bodyId, &shapeDef, m_cylinderHull );
}
break;
default:
assert( false );
}
}
bool DrawControls() override
2026-05-10 13:36:52 -07:00
{
if ( ImGui::RadioButton( "Sphere", m_shapeType == ShapeType::sphere ) )
{
m_shapeType = ShapeType::sphere;
Spawn();
}
if ( ImGui::RadioButton( "Capsule", m_shapeType == ShapeType::capsule ) )
{
m_shapeType = ShapeType::capsule;
Spawn();
}
if ( ImGui::RadioButton( "Box", m_shapeType == ShapeType::box ) )
{
m_shapeType = ShapeType::box;
Spawn();
}
if ( ImGui::RadioButton( "Cylinder", m_shapeType == ShapeType::cylinder ) )
{
m_shapeType = ShapeType::cylinder;
Spawn();
}
b3Vec3 scale = m_scale;
bool changed = false;
changed = changed || ImGui::SliderFloat( "Scale X", &scale.x, -2.0f, 2.0f, "%.1f" );
changed = changed || ImGui::SliderFloat( "Scale Z", &scale.z, -2.0f, 2.0f, "%.1f" );
if ( changed )
{
m_scale = scale;
b3Shape_SetMesh( m_boxShapeId, m_boxMesh, m_scale );
}
return true;
2026-05-10 13:36:52 -07:00
}
void Step() override
{
if ( m_stepCount == 83 )
{
// m_context->settings.pause = true;
}
if ( m_stepCount < 100 )
{
// b3Vec3 p = b3Body_GetPosition( m_bodyId );
// printf( "%d : %g %g %g\n", m_stepCount, p.x, p.y, p.z );
// b3Vec3 v = b3Body_GetLinearVelocity( m_bodyId );
// printf( "%d : %g %g %g\n", m_stepCount, v.x, v.y, v.z );
}
Sample::Step();
}
static Sample* Create( SampleContext* context )
{
return new BoxMesh( context );
}
ShapeType m_shapeType;
b3BodyId m_bodyId;
b3MeshData* m_boxMesh;
b3HullData* m_cylinderHull;
2026-05-10 13:36:52 -07:00
b3ShapeId m_boxShapeId;
b3Vec3 m_scale;
};
static int sampleBoxMesh = RegisterSample( "Mesh", "Box", BoxMesh::Create );
2026-05-10 13:36:52 -07:00
class MeshReflection : public Sample
{
public:
enum
{
e_humanCount = 20
};
explicit MeshReflection( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Pos_zero );
2026-05-10 13:36:52 -07:00
}
b3BodyDef bodyDef = b3DefaultBodyDef();
b3ShapeDef shapeDef = b3DefaultShapeDef();
{
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
m_gridMesh = b3CreateGridMesh( 20, 20, 2.0f, 2, true );
b3CreateMeshShape( body, &shapeDef, m_gridMesh, b3Vec3_one );
}
{
m_buildingMesh = CreateMeshData( "data/meshes/building.obj", 1.0f, false, false, true, true );
b3ShapeDef meshShapeDef = b3DefaultShapeDef();
b3SurfaceMaterial materials[3] = {
{
.friction = 0.6f,
},
{
.friction = 0.0f,
.restitution = 0.95f,
.userMaterialId = 1,
},
{
.friction = 0.2f,
.restitution = 0.2f,
.userMaterialId = 2,
},
};
meshShapeDef.materials = materials;
meshShapeDef.materialCount = 3;
bodyDef.position = { -10.0f, 0.0f, 0.0f };
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
m_scale = { -1.0f, 1.0f, 1.0f };
bodyDef.position = { 10.0f, 0.0f, 0.0f };
m_meshBodyId = b3CreateBody( m_worldId, &bodyDef );
m_meshShapeId = b3_nullShapeId;
if ( m_buildingMesh != nullptr )
{
b3CreateMeshShape( body, &meshShapeDef, m_buildingMesh, b3Vec3_one );
m_meshShapeId = b3CreateMeshShape( m_meshBodyId, &meshShapeDef, m_buildingMesh, m_scale );
}
2026-05-10 13:36:52 -07:00
}
bodyDef.type = b3_dynamicBody;
{
bodyDef.position = { 6.0f, 15.0f, 0.0f };
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
b3Sphere sphere = { b3Vec3_zero, 0.5f };
shapeDef.baseMaterial.rollingResistance = 0.2f;
shapeDef.baseMaterial.userMaterialId = 42;
b3CreateSphereShape( body, &shapeDef, &sphere );
}
{
bodyDef.position = { 9.0f, 15.0f, 0.0f };
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
b3Capsule capsule = { { -0.5f, 0.5f, 0.0f }, { 0.5f, 0.0f, 0.0f }, 0.25f };
shapeDef.baseMaterial.rollingResistance = 0.2f;
shapeDef.baseMaterial.userMaterialId = 11;
b3CreateCapsuleShape( body, &shapeDef, &capsule );
}
{
bodyDef.position = { 12.0f, 15.0f, 0.0f };
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
b3BoxHull hull = b3MakeBoxHull( 0.25f, 0.5f, 0.75f );
shapeDef.baseMaterial.userMaterialId = 555;
b3CreateHullShape( body, &shapeDef, &hull.base );
}
float frictionTorque = 5.0f;
float hertz = 1.0f;
float dampingRatio = 0.7f;
bool colorize = false;
for ( int humanIndex = 0; humanIndex < e_humanCount; humanIndex += 1 )
{
m_humans[humanIndex] = {};
int groupIndex = humanIndex;
b3Pos position = { -14.0f + 1.5f * humanIndex, 8.0f, 0.0f };
2026-05-10 13:36:52 -07:00
CreateHuman( &m_humans[humanIndex], m_worldId, position, frictionTorque, hertz, dampingRatio, groupIndex, nullptr,
colorize );
}
}
~MeshReflection() override
{
b3DestroyMesh( m_gridMesh );
DestroyMeshData( m_buildingMesh );
2026-05-10 13:36:52 -07:00
for ( int i = 0; i < e_humanCount; ++i )
{
DestroyHuman( m_humans + i );
}
}
bool DrawControls() override
2026-05-10 13:36:52 -07:00
{
bool changed = false;
b3Vec3 scale = m_scale;
if ( ImGui::RadioButton( "Neg X", scale.x < 0.0f ) )
{
scale.x = -1.0f;
changed = true;
}
ImGui::SameLine();
if ( ImGui::RadioButton( "Pos X", scale.x > 0.0f ) )
{
scale.x = 1.0f;
changed = true;
}
if ( ImGui::RadioButton( "Neg Y", scale.y < 0.0f ) )
{
scale.y = -1.0f;
changed = true;
}
ImGui::SameLine();
if ( ImGui::RadioButton( "Pos Y", scale.y > 0.0f ) )
{
scale.y = 1.0f;
changed = true;
}
if ( ImGui::RadioButton( "Neg Z", scale.z < 0.0f ) )
{
scale.z = -1.0f;
changed = true;
}
ImGui::SameLine();
if ( ImGui::RadioButton( "Pos Z", scale.z > 0.0f ) )
{
scale.z = 1.0f;
changed = true;
}
if ( changed )
{
m_scale = scale;
if ( m_buildingMesh != nullptr )
{
b3Shape_SetMesh( m_meshShapeId, m_buildingMesh, m_scale );
}
2026-05-10 13:36:52 -07:00
}
return true;
2026-05-10 13:36:52 -07:00
}
void Render() override
{
DrawTextLine( "scale = (%.2g, %.2g, %.2g)", m_scale.x, m_scale.y, m_scale.z );
DrawTextLine( "surface type = %d", (int)m_userMaterialId );
2026-05-10 13:36:52 -07:00
Sample::Render();
}
static Sample* Create( SampleContext* context )
{
return new MeshReflection( context );
}
b3Vec3 m_scale;
b3MeshData* m_gridMesh;
b3MeshData* m_buildingMesh;
b3BodyId m_meshBodyId;
b3ShapeId m_meshShapeId;
Human m_humans[e_humanCount];
};
static int sampleMeshReflection = RegisterSample( "Mesh", "Reflection", MeshReflection::Create );
2026-05-10 13:36:52 -07:00
struct CastContext
{
b3Pos point;
2026-05-10 13:36:52 -07:00
b3Vec3 normal;
float fraction;
bool hit;
};
class HeightField : public Sample
{
public:
static Sample* Create( SampleContext* context )
{
return new HeightField( context );
}
explicit HeightField( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Pos_zero );
2026-05-10 13:36:52 -07:00
}
#if defined( NDEBUG )
2026-05-10 13:36:52 -07:00
m_rowCount = 400;
m_columnCount = 400;
#else
m_rowCount = 10;
m_columnCount = 10;
2026-05-10 13:36:52 -07:00
#endif
m_groundId = {};
m_bodyId1 = {};
m_bodyId2 = {};
m_amplitude = 0.75f;
m_holes = false;
// m_rayOrigin = { 1.4f, 2.0f, 1.5f };
// m_rayTranslation = { -3.0f, -4.0f, 3.0f };
// m_rayOrigin = { 30.0f, 2.0f, 0.0f };
// m_rayTranslation = { -60.0f, -4.0f, 0.0f };
m_rayOrigin = { 5.5f, 4.0f, 1.01f };
m_rayTranslation = { 0.0f, -8.0f, 0.0f };
m_radius = 0.2f;
2026-05-10 13:36:52 -07:00
m_heightField = nullptr;
CreateScene();
}
~HeightField() override
{
b3DestroyHeightField( m_heightField );
}
void CreateScene()
{
if ( B3_IS_NULL( m_groundId ) == false )
{
b3DestroyBody( m_groundId );
m_groundId = {};
}
if ( B3_IS_NULL( m_bodyId1 ) == false )
{
b3DestroyBody( m_bodyId1 );
m_bodyId1 = {};
}
if ( B3_IS_NULL( m_bodyId2 ) == false )
{
b3DestroyBody( m_bodyId2 );
m_bodyId2 = {};
}
if ( m_heightField != nullptr )
{
b3DestroyHeightField( m_heightField );
}
b3BodyDef bodyDef = b3DefaultBodyDef();
b3ShapeDef shapeDef = b3DefaultShapeDef();
m_scale = { 2.0f, 2.0f * m_amplitude, 2.0f };
if ( m_amplitude == 0.0f )
{
m_heightField = b3CreateGrid( m_rowCount, m_columnCount, m_scale, m_holes );
}
else
{
m_heightField = b3CreateWave( m_rowCount, m_columnCount, m_scale, 0.1f, 0.03333f, m_holes );
}
bodyDef.position = { -0.5f * m_heightField->scale.x * ( m_columnCount - 1 ), 0.0f,
-0.5f * m_heightField->scale.z * ( m_rowCount - 1 ) };
// bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisY, 0.25f * B3_PI );
m_groundId = b3CreateBody( m_worldId, &bodyDef );
b3CreateHeightFieldShape( m_groundId, &shapeDef, m_heightField );
#if 0
bodyDef.type = b3_dynamicBody;
bodyDef.position = { 0.0f, 4.0f, 1.0f };
m_bodyId1 = b3CreateBody( m_worldId, &bodyDef );
b3Sphere sphere = { b3Vec3_zero, 0.5f };
b3CreateSphereShape( m_bodyId1, &shapeDef, &sphere );
bodyDef.position = { 0.0f, 0.5f, -0.5f };
bodyDef.linearVelocity = { 5.0f, 0.0f, 0.0f };
m_bodyId2 = b3CreateBody( m_worldId, &bodyDef );
b3BoxHull box = b3MakeBoxHull( 0.3f, 0.3f, 0.3f );
shapeDef.baseMaterial.friction = 0.05f;
b3CreateHullShape( m_bodyId2, &shapeDef, &box.base );
#endif
}
bool DrawControls() override
2026-05-10 13:36:52 -07:00
{
ImGui::PushItemWidth( 6.0f * ImGui::GetFontSize() );
2026-05-10 13:36:52 -07:00
if ( ImGui::SliderInt( "columns", &m_columnCount, 1, 500 ) )
{
CreateScene();
}
if ( ImGui::SliderInt( "rows", &m_rowCount, 1, 500 ) )
{
CreateScene();
}
if ( ImGui::SliderFloat( "amplitude", &m_amplitude, 0.0f, 2.0f ) )
{
CreateScene();
}
if ( ImGui::Checkbox( "holes", &m_holes ) )
{
CreateScene();
}
ImGui::SliderFloat( "ray x", &m_rayOrigin.x, -2.0f * m_radius - 0.5f * m_scale.x * ( m_columnCount - 1 ),
2.0f * m_radius + 0.5f * m_scale.x * ( m_columnCount - 1 ) );
ImGui::SliderFloat( "ray z", &m_rayOrigin.z, -2.0f * m_radius - 0.5f * m_scale.z * ( m_rowCount - 1 ),
2.0f * m_radius + 0.5f * m_scale.z * ( m_rowCount - 1 ) );
2026-05-10 13:36:52 -07:00
ImGui::SliderFloat( "delta x", &m_rayTranslation.x, -2.0f * m_scale.x * ( m_columnCount - 1 ),
2.0f * m_scale.x * ( m_columnCount - 1 ) );
ImGui::SliderFloat( "delta z", &m_rayTranslation.z, -2.0f * m_scale.z * ( m_rowCount - 1 ),
2.0f * m_scale.z * ( m_rowCount - 1 ) );
ImGui::SliderFloat( "radius", &m_radius, 0.0f, 1.0f );
ImGui::PopItemWidth();
return true;
2026-05-10 13:36:52 -07:00
}
void Render() override
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.1f, 0.0f }, b3Quat_identity };
DrawAxes( b3MakeWorldTransform( transform ), 0.5f );
2026-05-10 13:36:52 -07:00
}
// This callback finds the closest hit.
static float CastCallback( b3ShapeId shapeId, b3Pos point, b3Vec3 normal, float fraction, uint64_t surfaceType,
2026-05-10 13:36:52 -07:00
int triangleIndex, int childIndex, void* context )
{
(void)shapeId;
(void)surfaceType;
(void)triangleIndex;
(void)childIndex;
CastContext* castContext = (CastContext*)context;
castContext->point = point;
castContext->normal = normal;
castContext->fraction = fraction;
castContext->hit = true;
// By returning the current fraction, we instruct the calling code to clip the ray and
// continue the ray-cast to the next shape. WARNING: do not assume that shapes
// are reported in order. However, by clipping, we can always get the closest shape.
return fraction;
}
void Step() override
{
Sample::Step();
if ( m_radius == 0.0f )
{
// m_rayOrigin = { 0.0f, -FLT_EPSILON, 0.0f };
// m_rayTranslation = { -1000.0f, 0.0f, 0.0 };
b3Pos origin = b3ToPos( m_rayOrigin );
b3RayResult result = b3World_CastRayClosest( m_worldId, origin, m_rayTranslation, b3DefaultQueryFilter() );
2026-05-10 13:36:52 -07:00
DrawPoint( origin, 6.0f, MakeColor( b3_colorGreenYellow ) );
DrawPoint( b3OffsetPos( origin, m_rayTranslation ), 6.0f, MakeColor( b3_colorRed ) );
DrawLine( origin, b3OffsetPos( origin, m_rayTranslation ), MakeColor( b3_colorGray ) );
2026-05-10 13:36:52 -07:00
if ( result.hit )
{
b3Pos point = result.point;
DrawLine( point, b3OffsetPos( point, 0.5f * result.normal ), MakeColor( b3_colorGray ) );
DrawPoint( point, 10.0f, MakeColor( b3_colorOrange ) );
2026-05-10 13:36:52 -07:00
}
}
else
{
b3ShapeProxy proxy = {
.points = &m_rayOrigin,
.count = 1,
.radius = m_radius,
};
CastContext result = {};
result.fraction = 1.0f;
b3World_CastShape( m_worldId, b3Pos_zero, &proxy, m_rayTranslation, b3DefaultQueryFilter(), CastCallback, &result );
2026-05-10 13:36:52 -07:00
b3Pos origin = b3ToPos( m_rayOrigin );
DrawPoint( origin, 2.0f, MakeColor( b3_colorGreen ) );
DrawPoint( b3OffsetPos( origin, m_rayTranslation ), 2.0f, MakeColor( b3_colorRed ) );
DrawLine( origin, b3OffsetPos( origin, m_rayTranslation ), MakeColor( b3_colorYellow ) );
2026-05-10 13:36:52 -07:00
b3Sphere sphere = { b3Vec3_zero, m_radius };
b3Transform sphereXf = { m_rayOrigin + result.fraction * m_rayTranslation, b3Quat_identity };
DrawSolidSphere( b3MakeWorldTransform( sphereXf ), sphere, MakeColor( b3_colorOrange ) );
2026-05-10 13:36:52 -07:00
if ( result.hit )
{
b3Pos point = result.point;
DrawLine( point, b3OffsetPos( point, 0.5f * result.normal ), MakeColor( b3_colorGreen ) );
DrawPoint( point, 6.0f, MakeColor( b3_colorPurple ) );
2026-05-10 13:36:52 -07:00
}
}
}
b3BodyId m_groundId;
b3BodyId m_bodyId1;
b3BodyId m_bodyId2;
float m_amplitude;
b3Vec3 m_scale;
b3Vec3 m_rayOrigin;
b3Vec3 m_rayTranslation;
b3HeightFieldData* m_heightField;
2026-05-10 13:36:52 -07:00
float m_radius;
int m_rowCount;
int m_columnCount;
bool m_holes;
};
static int sampleHeightField = RegisterSample( "Mesh", "Height Field", HeightField::Create );
2026-05-10 13:36:52 -07:00
static float ComputeInternalSurfaceArea( const b3MeshData* data )
{
const b3MeshNode* nodes = b3GetMeshNodes( data );
int nodeCount = data->nodeCount;
float area = 0.0f;
for ( int i = 1; i < nodeCount; ++i )
{
const b3MeshNode* node = nodes + i;
if ( node->data.asLeaf.type == 3 )
{
continue;
}
b3AABB box = {
node->lowerBound,
node->upperBound,
};
area += b3AABB_Area( box );
}
return area;
}
class MeshViewer : public Sample
{
public:
explicit MeshViewer( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 50.0f, b3Pos_zero );
2026-05-10 13:36:52 -07:00
}
m_meshIndex = 0;
m_mesh = nullptr;
m_bodyId = b3_nullBodyId;
m_medianSplit = true;
m_concaveEdges = true;
m_weldVertices = true;
m_weldToleranceMillimeters = 1.5f;
m_area = 0.0f;
m_drawLevel = -1;
m_height = 0;
m_buildTime = 0.0f;
m_launchSpeedScale = 1.0f;
LoadMesh();
}
~MeshViewer() override
{
DestroyMeshData( m_mesh );
2026-05-10 13:36:52 -07:00
}
void LoadMesh()
{
if ( B3_IS_NON_NULL( m_bodyId ) )
{
b3DestroyBody( m_bodyId );
m_bodyId = b3_nullBodyId;
}
if ( m_mesh != nullptr )
{
b3DestroyMesh( m_mesh );
m_mesh = nullptr;
}
b3BodyDef bodyDef = b3DefaultBodyDef();
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
float scale = 0.01f;
const char* filesNames[m_meshCount] = {
"voxel_mesh_01.obj",
"voxel_mesh_02.obj",
"voxel_mesh_03.obj",
"voxel_mesh_04.obj",
2026-05-10 13:36:52 -07:00
};
char buffer[64] = {};
snprintf( buffer, 64, "data/meshes/%s", filesNames[m_meshIndex] );
if ( LoadTempMesh( buffer, &m_tempMesh, scale, true ) == false )
{
m_buildTime = 0.0f;
m_area = 0.0f;
m_height = 0;
m_drawLevel = -1;
return;
}
2026-05-10 13:36:52 -07:00
b3MeshDef def = {};
def.vertices = m_tempMesh.vertices.data();
def.vertexCount = (int)m_tempMesh.vertices.size();
def.indices = m_tempMesh.indices.data();
def.triangleCount = (int)m_tempMesh.indices.size() / 3;
def.materialIndices = m_tempMesh.materialIndices.data();
def.useMedianSplit = m_medianSplit;
def.identifyEdges = m_concaveEdges;
def.weldVertices = m_weldVertices;
def.weldTolerance = 0.001f * m_weldToleranceMillimeters;
uint64_t startTicks = b3GetTicks();
m_mesh = b3CreateMesh( &def, m_degenerateTriangles, m_degenerateCapacity );
m_buildTime = b3GetMilliseconds( startTicks );
if ( m_mesh == nullptr )
{
m_area = 0.0f;
m_height = 0;
m_drawLevel = -1;
return;
}
2026-05-10 13:36:52 -07:00
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateMeshShape( m_bodyId, &shapeDef, m_mesh, b3Vec3_one );
m_area = ComputeInternalSurfaceArea( m_mesh );
m_height = b3GetHeight( m_mesh );
m_drawLevel = b3ClampInt( m_drawLevel, -1, m_height );
}
void DrawNodes()
{
if ( m_drawLevel < 0 || m_mesh == nullptr )
{
return;
}
int count = m_mesh->nodeCount;
if ( count == 0 )
{
return;
}
constexpr int colorCount = 20;
b3HexColor colors[colorCount] = {
b3_colorAliceBlue, b3_colorAntiqueWhite, b3_colorAqua, b3_colorAquamarine, b3_colorAzure,
b3_colorBeige, b3_colorBisque, b3_colorBlanchedAlmond, b3_colorBlue, b3_colorBlueViolet,
b3_colorBrown, b3_colorBurlywood, b3_colorCadetBlue, b3_colorChartreuse, b3_colorChocolate,
b3_colorCoral, b3_colorCornflowerBlue, b3_colorCornsilk, b3_colorCrimson, b3_colorCyan,
};
const b3MeshNode* nodes = b3GetMeshNodes( m_mesh );
const b3MeshNode* root = nodes + 0;
// Queue to store nodes along with their levels
std::queue<std::pair<const b3MeshNode*, int>> q;
// Start with the root at level 0
q.push( { root, 0 } );
while ( !q.empty() )
{
std::pair<const b3MeshNode*, int> pair = q.front();
q.pop();
const b3MeshNode* node = pair.first;
int level = pair.second;
// If the current level matches the target level, add the node's data to the result
if ( level == m_drawLevel )
{
b3AABB box = {
node->lowerBound,
node->upperBound,
};
DrawBounds( box, 0.0f, MakeColor( colors[level % colorCount] ) );
2026-05-10 13:36:52 -07:00
int axis = node->data.asNode.axis;
b3Vec3 center = b3AABB_Center( box );
b3Pos centerPos = b3ToPos( center );
2026-05-10 13:36:52 -07:00
if ( axis == 0 )
{
DrawArrow( centerPos, b3OffsetPos( centerPos, 0.1f * b3Vec3_axisX ), MakeColor( b3_colorRed ) );
2026-05-10 13:36:52 -07:00
}
else if ( axis == 1 )
{
DrawArrow( centerPos, b3OffsetPos( centerPos, 0.1f * b3Vec3_axisY ), MakeColor( b3_colorGreen ) );
2026-05-10 13:36:52 -07:00
}
else if ( axis == 2 )
{
DrawArrow( centerPos, b3OffsetPos( centerPos, 0.1f * b3Vec3_axisZ ), MakeColor( b3_colorBlue ) );
2026-05-10 13:36:52 -07:00
}
else
{
DrawSolidSphere( b3WorldTransform_identity, { center, 0.03f }, MakeColor( b3_colorOrange ) );
2026-05-10 13:36:52 -07:00
}
}
// If the current level exceeds the target level, stop processing
if ( level > m_drawLevel )
{
break;
}
bool isLeaf = node->data.asNode.axis == 3;
if ( isLeaf == false )
{
// Enqueue the left and right children with their respective levels
// I'm using some internal knowledge of how the node data is organized.
q.push( { node + 1, level + 1 } );
q.push( { node + node->data.asNode.childOffset, level + 1 } );
}
}
}
bool DrawControls() override
2026-05-10 13:36:52 -07:00
{
if ( ImGui::SliderInt( "index", &m_meshIndex, 0, m_meshCount - 1 ) )
{
LoadMesh();
}
if ( ImGui::RadioButton( "median split", m_medianSplit ) )
{
m_medianSplit = true;
LoadMesh();
}
if ( ImGui::RadioButton( "sah binning", m_medianSplit == false ) )
{
m_medianSplit = false;
LoadMesh();
}
if ( ImGui::Checkbox( "concave edges", &m_concaveEdges ) )
{
LoadMesh();
}
if ( ImGui::Checkbox( "weld vertices", &m_weldVertices ) )
{
LoadMesh();
}
if ( ImGui::SliderFloat( "tolerance", &m_weldToleranceMillimeters, 0.0f, 10.0f, "%.1f" ) )
{
LoadMesh();
}
ImGui::SliderInt( "draw level", &m_drawLevel, -1, m_height );
return true;
2026-05-10 13:36:52 -07:00
}
void Render() override
{
Sample::Render();
DrawAxes( b3WorldTransform_identity, 1.0f );
2026-05-10 13:36:52 -07:00
if ( m_mesh == nullptr )
{
DrawTextLine( "mesh failed to load" );
return;
}
2026-05-10 13:36:52 -07:00
DrawTextLine( "triangle count = %d", m_mesh->triangleCount );
DrawTextLine( "vertex count = %d", m_mesh->vertexCount );
DrawTextLine( "degenerate count = %d", m_mesh->degenerateCount );
DrawTextLine( "node area = %g", m_area );
DrawTextLine( "height = %d", m_height );
DrawTextLine( "build time (ms) = %g", m_buildTime );
DrawNodes();
b3Vec3 offset = { 0.01f, 0.01f, 0.01f };
for ( int i = 0; i < m_mesh->degenerateCount; ++i )
{
int triangleIndex = m_degenerateTriangles[i];
int i1 = m_tempMesh.indices[3 * triangleIndex + 0];
int i2 = m_tempMesh.indices[3 * triangleIndex + 1];
int i3 = m_tempMesh.indices[3 * triangleIndex + 2];
b3Vec3 v1 = m_tempMesh.vertices[i1];
b3Vec3 v2 = m_tempMesh.vertices[i2];
b3Vec3 v3 = m_tempMesh.vertices[i3];
float area = b3Length( b3Cross( v2 - v1, v3 - v1 ) );
(void)area;
b3Vec3 p = ( 1.0f / 3.0f ) * ( v1 + v2 + v3 );
b3Pos pPos = b3ToPos( p );
DrawPoint( pPos, 10.0f, MakeColor( b3_colorCyan ) );
DrawString3D( b3OffsetPos( pPos, offset ), MakeColor( b3_colorOrange ), "%d", triangleIndex );
2026-05-10 13:36:52 -07:00
{
b3Pos p1 = b3ToPos( v1 );
b3Pos p2 = b3ToPos( v2 );
b3Pos p3 = b3ToPos( v3 );
DrawPoint( p1, 10.0f, MakeColor( b3_colorRed ) );
DrawPoint( p2, 10.0f, MakeColor( b3_colorGreen ) );
DrawPoint( p3, 10.0f, MakeColor( b3_colorBlue ) );
DrawString3D( b3OffsetPos( p1, offset ), MakeColor( b3_colorRed ), "%d", i1 );
DrawString3D( b3OffsetPos( p2, offset ), MakeColor( b3_colorGreen ), "%d", i2 );
DrawString3D( b3OffsetPos( p3, offset ), MakeColor( b3_colorBlue ), "%d", i3 );
2026-05-10 13:36:52 -07:00
}
}
}
static Sample* Create( SampleContext* context )
{
return new MeshViewer( context );
}
static constexpr int m_meshCount = 4;
static constexpr int m_degenerateCapacity = 64;
TempMesh m_tempMesh;
b3MeshData* m_mesh;
b3BodyId m_bodyId;
float m_area;
float m_weldToleranceMillimeters;
float m_buildTime;
int m_meshIndex;
int m_degenerateTriangles[m_degenerateCapacity];
int m_drawLevel;
int m_height;
bool m_medianSplit;
bool m_concaveEdges;
bool m_weldVertices;
};
static int sampleMeshViewer = RegisterSample( "Mesh", "Viewer", MeshViewer::Create );
2026-05-10 13:36:52 -07:00
// Results 9/29/25
// base median split: 0.222 ms per mesh
// weld: 0.526
// weld + edge: 0.755
class MeshCreationBenchmark : public Sample
{
public:
explicit MeshCreationBenchmark( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Pos_zero );
2026-05-10 13:36:52 -07:00
}
LoadTempMesh( "data/meshes/voxel_mesh_01.obj", m_tempMeshes + 0, 0.01f, true );
LoadTempMesh( "data/meshes/voxel_mesh_02.obj", m_tempMeshes + 1, 0.01f, true );
LoadTempMesh( "data/meshes/voxel_mesh_03.obj", m_tempMeshes + 2, 0.01f, true );
LoadTempMesh( "data/meshes/voxel_mesh_04.obj", m_tempMeshes + 3, 0.01f, true );
m_time = FLT_MAX;
}
void Step() override
{
bool computeArea = false;
int iterations = m_isDebug ? 1 : 10;
int meshCount = m_meshCount;
int triangleCount = 0;
float area = 0.0f;
for ( int i = 0; i < iterations; ++i )
{
uint64_t startTicks = b3GetTicks();
for ( int j = 0; j < meshCount; ++j )
{
TempMesh& mesh = m_tempMeshes[j];
b3MeshDef def = {};
def.vertices = mesh.vertices.data();
def.vertexCount = (int)mesh.vertices.size();
def.indices = mesh.indices.data();
def.triangleCount = (int)mesh.indices.size() / 3;
def.materialIndices = mesh.materialIndices.data();
def.useMedianSplit = true;
def.identifyEdges = false;
def.weldVertices = true;
def.weldTolerance = 0.0015f;
b3MeshData* meshData = b3CreateMesh( &def, nullptr, 0 );
if ( meshData == nullptr )
{
continue;
}
2026-05-10 13:36:52 -07:00
triangleCount += i == 0 ? meshData->triangleCount : 0;
if ( computeArea && i == 0 )
{
area += ComputeInternalSurfaceArea( meshData );
}
b3DestroyMesh( meshData );
}
float ms = b3GetMilliseconds( startTicks );
m_time = b3MinFloat( m_time, ms );
}
DrawTextLine( "triangle count = %d, area = %g", triangleCount, area );
DrawTextLine( "total time = %.4f ms", m_time );
DrawTextLine( "time per mesh = %.4f ms", m_time / meshCount );
}
static Sample* Create( SampleContext* context )
{
return new MeshCreationBenchmark( context );
}
static constexpr int m_meshCount = 4;
TempMesh m_tempMeshes[m_meshCount];
float m_time;
};
static int sampleMeshCreationBenchmark = RegisterSample( "Mesh", "Creation Benchmark", MeshCreationBenchmark::Create );
2026-05-10 13:36:52 -07:00
class VoxelMesh : public Sample
{
public:
explicit VoxelMesh( SampleContext* context )
: Sample( context )
{
b3Pos origin = { 5000.0f, 3500.0f, -7000.0f };
2026-05-10 13:36:52 -07:00
if ( context->restart == false )
{
m_camera->SetView( -115.0f, 5.0f, 5.0f, b3OffsetPos( origin, b3Vec3{ 0.0f, 10.0f, 0.0f } ) );
2026-05-10 13:36:52 -07:00
}
m_launchSpeedScale = 1.0f;
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.name = "ground";
bodyDef.position = origin;
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
float scale = 0.01f;
TempMesh tempMesh;
const char* fileName = "data/meshes/collision_mesh_01.obj";
LoadTempMesh( fileName, &tempMesh, scale, true );
b3MeshDef def = {};
def.vertices = tempMesh.vertices.data();
def.vertexCount = (int)tempMesh.vertices.size();
def.indices = tempMesh.indices.data();
def.triangleCount = (int)tempMesh.indices.size() / 3;
def.materialIndices = tempMesh.materialIndices.data();
def.useMedianSplit = true;
// this has a big impact on stability because the faces are nearly coplanar
def.identifyEdges = true;
def.weldVertices = true;
def.weldTolerance = 0.002f;
m_meshData = b3CreateMesh( &def, nullptr, 0 );
if ( m_meshData != nullptr )
{
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateMeshShape( groundId, &shapeDef, m_meshData, b3Vec3_one );
}
2026-05-10 13:36:52 -07:00
}
{
b3Vec3 points[16];
points[0] = { -3.13548756, 3.81141949, 237.289047 };
points[1] = { -16.2333279, -23.4977913, 235.486603 };
points[2] = { -13.8834839, 6.20244455, 23.7760544 };
points[3] = { 14.0794125, 4.63170528, 24.9530792 };
points[4] = { 3.98322797, -16.4192238, 236.704071 };
points[5] = { -23.3520412, -3.26714420, 236.071594 };
points[6] = { 13.4517860, -6.94963741, 24.4085312 };
points[7] = { -5.24953651, 13.9316301, 24.5058060 };
points[8] = { -4.65071201, -24.1484108, 235.974121 };
points[9] = { -14.5111103, -5.37889385, 23.2315063 };
points[10] = { 6.33307076, 13.2810068, 24.9935150 };
points[11] = { 4.81784487, -14.6788225, 23.6787796 };
points[12] = { -14.7180958, 4.46204281, 236.801331 };
points[13] = { -23.9796677, -14.8484812, 235.527039 };
points[14] = { 4.61085415, -4.83788204, 237.248611 };
points[15] = { -6.76476669, -14.0281992, 23.1910706 };
for ( int i = 0; i < 16; ++i )
{
b3Vec3 p = points[i];
points[i] = b3MulSV( 0.01f, b3Vec3{ p.y, p.z, p.x } );
}
b3HullData* hull = b3CreateHull( points, 16, 16 );
2026-05-10 13:36:52 -07:00
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.name = "cylinder";
bodyDef.type = b3_dynamicBody;
bodyDef.position = { 5020.27734f, 3506.22559f, -6986.48584f };
2026-05-10 13:36:52 -07:00
bodyDef.rotation = { { 0.664546967, 0.669287264, 0.135021493 }, 0.303646326 };
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.baseMaterial.rollingResistance = 0.1f;
b3CreateHullShape( bodyId, &shapeDef, hull );
b3DestroyHull( hull );
}
// float jointFrictionTorque = 5.0f;
// float jointHertz = 1.0f;
// float jointDampingRatio = 0.7f;
m_human = {};
// CreateHuman( &m_human, m_worldId, b3Vec3{ 15.0f, 10.0f, 15.0f } + origin, jointFrictionTorque, jointHertz,
// jointDampingRatio, 1, nullptr, false );
}
~VoxelMesh() override
{
DestroyMeshData( m_meshData );
2026-05-10 13:36:52 -07:00
}
void Step() override
{
Sample::Step();
}
static Sample* Create( SampleContext* context )
{
return new VoxelMesh( context );
}
b3MeshData* m_meshData;
Human m_human;
};
static int sampleVoxelMesh = RegisterSample( "Mesh", "Voxel", VoxelMesh::Create );
// Pause this to check contact manifolds for axis aligned collisions.
class HollowBox : public Sample
{
public:
explicit HollowBox( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 30.0f, b3Pos_zero );
}
{
b3BodyDef bodyDef = b3DefaultBodyDef();
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
m_mesh = b3CreateHollowBoxMesh( { 0.0f, 0.0f, 0.0f }, { 10.0f, 10.0f, 10.0f } );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateMeshShape( groundId, &shapeDef, m_mesh, b3Vec3_one );
}
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.gravityScale = 0.0f;
bodyDef.enableSleep = false;
b3ShapeDef shapeDef = b3DefaultShapeDef();
{
b3HullData* cylinderHull = b3CreateCylinder( 1.0f, 0.25f, 0.0f, 8 );
b3Pos positions[6] = {
{ 0.0f, -10.2f, 0.0f }, { 0.0f, 9.2f, 0.0f }, { -9.8f, 0.0f, 0.0f },
{ 9.8f, 0.0f, 0.0f }, { 0.0f, 0.0f, -9.8f }, { 0.0f, 0.0f, 9.8f },
};
for (int i = 0; i < 6; ++i)
{
bodyDef.position = positions[i];
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateHullShape( bodyId, &shapeDef, cylinderHull );
}
b3DestroyHull( cylinderHull );
}
{
b3Capsule capsule ={{0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, 0.25f};
b3Pos positions[8] = {
{ 0.0f, -10.2f, 2.0f }, { 0.0f, 9.2f, 2.0f },
{ 0.0f, -9.9f, 4.0f }, { 0.0f, 8.9f, 4.0f },
{ -9.8f, 2.0f, 0.0f },
{ 9.8f, 2.0f, 0.0f }, { 0.0f, 2.0f, -9.8f }, { 0.0f, 2.0f, 9.8f },
};
for (int i = 0; i < 8; ++i)
{
bodyDef.position = positions[i];
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
}
}
}
~HollowBox() override
{
b3DestroyMesh( m_mesh );
}
void Render() override
{
Sample::Render();
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
}
void Step() override
{
Sample::Step();
}
static Sample* Create( SampleContext* context )
{
return new HollowBox( context );
}
b3MeshData* m_mesh;
};
static int sampleHollowBox = RegisterSample( "Mesh", "Hollow Box", HollowBox::Create );
2026-10-30 14:56:07 -07:00
// Ghost collision scenes. Each ground is a profile in the xy-plane extruded along z and finely
// tessellated, so a sliding body crosses many internal edges. A manifold that pushes on the body
// is a ghost when its normal points outside the span of face normals under the body. Those are the
// ones that block or launch a body.
class GhostCollisions : public Sample
{
public:
enum Scene
{
e_flat,
e_concaveRamp,
e_cliff,
e_crest,
e_bumps,
e_tJunction,
e_shapeSeam,
e_internalEdges,
e_sceneCount
};
enum BodyType
{
e_box,
e_capsule,
e_uprightCapsule,
e_sphere,
e_cylinder,
e_bodyTypeCount
};
// Mesh variants of the PEEL InternalEdges presets
struct PeelPreset
{
int columnCount;
float scaleX;
float startZ;
};
static constexpr PeelPreset m_peelPresets[] = {
{ 4, 0.1f, -55.0f },
{ 4, 0.1f, -54.0f },
{ 4, 0.01f, -55.0f },
{ 16, 0.01f, -54.0f },
};
static constexpr float m_halfLength = 20.0f;
static constexpr float m_halfWidth = 3.0f;
static constexpr float m_startX = -16.0f;
static constexpr int m_markerCapacity = 256;
explicit GhostCollisions( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 90.0f, 20.0f, 30.0f, { 0.0f, 1.0f, 0.0f } );
}
m_cylinder = b3CreateCylinder( 0.4f, 0.4f, -0.2f, 16 );
m_peelCylinder = b3CreateCylinder( 2.0f, 1.0f, -1.0f, 16 );
m_groundId = b3_nullBodyId;
m_bodyId = b3_nullBodyId;
m_shapeId = b3_nullShapeId;
CreateGround();
Launch();
}
~GhostCollisions() override
{
DestroyMeshes();
b3DestroyHull( m_cylinder );
b3DestroyHull( m_peelCylinder );
}
void DestroyMeshes()
{
for ( int i = 0; i < m_meshCount; ++i )
{
b3DestroyMesh( m_meshes[i] );
m_meshes[i] = nullptr;
}
m_meshCount = 0;
}
// The profile runs in +x with the solid below it. The mirror flips x in the data and the mesh
// scale flips it back, so the world geometry is the same either way.
void AddStrip( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices, const b3Vec2* profile, int count,
float zCell ) const
{
std::vector<b3Vec2> points;
points.push_back( profile[0] );
for ( int k = 0; k + 1 < count; ++k )
{
b3Vec2 a = profile[k];
b3Vec2 b = profile[k + 1];
float length = sqrtf( ( b.x - a.x ) * ( b.x - a.x ) + ( b.y - a.y ) * ( b.y - a.y ) );
int n = b3MaxInt( 1, (int)ceilf( length / m_cellSize - 0.01f ) );
for ( int s = 1; s <= n; ++s )
{
float t = (float)s / (float)n;
points.push_back( { a.x + t * ( b.x - a.x ), a.y + t * ( b.y - a.y ) } );
}
}
int zCount = b3MaxInt( 1, (int)ceilf( 2.0f * m_halfWidth / zCell - 0.01f ) );
AddGrid( vertices, indices, points, m_halfWidth, zCount, true );
}
void AddGrid( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices, const std::vector<b3Vec2>& points,
float halfWidth, int zCount, bool alternate ) const
{
float sign = m_mirror ? -1.0f : 1.0f;
int stride = zCount + 1;
int base = (int)vertices.size();
for ( b3Vec2 p : points )
{
for ( int j = 0; j <= zCount; ++j )
{
float z = -halfWidth + 2.0f * halfWidth * (float)j / (float)zCount;
vertices.push_back( { sign * p.x, p.y, z } );
}
}
bool mirror = m_mirror;
auto emit = [&]( int i1, int i2, int i3 ) {
indices.push_back( i1 );
indices.push_back( mirror ? i3 : i2 );
indices.push_back( mirror ? i2 : i3 );
};
int pointCount = (int)points.size();
for ( int i = 0; i + 1 < pointCount; ++i )
{
for ( int j = 0; j < zCount; ++j )
{
int a = base + i * stride + j;
int b = a + 1;
int c = a + stride + 1;
int d = a + stride;
// Alternate the diagonal like cooked level geometry
if ( alternate && ( ( i + j ) & 1 ) )
{
emit( a, b, d );
emit( b, c, d );
}
else
{
emit( a, b, c );
emit( c, d, a );
}
}
}
}
void AddMesh( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices, bool frictionless = false )
{
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.001f;
meshDef.identifyEdges = true;
b3MeshData* mesh = b3CreateMesh( &meshDef, nullptr, 0 );
m_meshes[m_meshCount++] = mesh;
b3Vec3 scale = m_mirror ? b3Vec3{ -1.0f, 1.0f, 1.0f } : b3Vec3_one;
b3ShapeDef shapeDef = b3DefaultShapeDef();
if ( frictionless )
{
shapeDef.baseMaterial.friction = 0.0f;
}
b3CreateMeshShape( m_groundId, &shapeDef, mesh, scale );
}
void CreateGround()
{
if ( B3_IS_NON_NULL( m_groundId ) )
{
b3DestroyBody( m_groundId );
}
DestroyMeshes();
b3BodyDef bodyDef = b3DefaultBodyDef();
m_groundId = b3CreateBody( m_worldId, &bodyDef );
float h = m_halfLength;
m_profile.clear();
switch ( m_scene )
{
case e_concaveRamp:
{
float slope = 8.0f * B3_PI / 180.0f;
m_profile = { { -h, h * tanf( slope ) }, { 0.0f, 0.0f }, { h, 0.0f } };
}
break;
case e_cliff:
m_profile = { { -h, 3.0f }, { 0.0f, 3.0f }, { 0.0f, 0.0f }, { h, 0.0f } };
break;
case e_crest:
m_profile = { { -h, 0.0f }, { -10.0f, 0.0f }, { 0.0f, 1.0f }, { 10.0f, 0.0f }, { h, 0.0f } };
break;
case e_bumps:
// Gentle folds of a few degrees, the range that only flat edge suppression misses
for ( int i = 0; i <= 80; ++i )
{
float x = -h + 0.5f * (float)i;
float y = x < -10.0f ? 0.0f : 0.05f * ( 1.0f - cosf( B3_PI * x ) );
m_profile.push_back( { x, y } );
}
break;
case e_internalEdges:
// PEEL's curved mesh: 32 rows 4 m apart, each step up 1.25 times the last, sliding toward +x
for ( int i = 31; i >= 0; --i )
{
m_profile.push_back( { 62.0f - 4.0f * (float)i, 1.0f + 0.1f * powf( 1.25f, (float)i ) } );
}
break;
default:
m_profile = { { -h, 0.0f }, { h, 0.0f } };
break;
}
std::vector<b3Vec3> vertices;
std::vector<int32_t> indices;
b3Vec2 left[2] = { { -h, 0.0f }, { 0.0f, 0.0f } };
b3Vec2 right[2] = { { 0.0f, 0.0f }, { h, 0.0f } };
if ( m_scene == e_tJunction )
{
// Fine and coarse strips share only every other vertex along x = 0
AddStrip( vertices, indices, left, 2, 0.5f * m_cellSize );
AddStrip( vertices, indices, right, 2, m_cellSize );
AddMesh( vertices, indices );
}
else if ( m_scene == e_shapeSeam )
{
// Two mesh shapes meeting at x = 0, so the seam spans two contacts
AddStrip( vertices, indices, left, 2, m_cellSize );
AddMesh( vertices, indices );
vertices.clear();
indices.clear();
AddStrip( vertices, indices, right, 2, m_cellSize );
AddMesh( vertices, indices );
}
else if ( m_scene == e_internalEdges )
{
// The mesh has no friction and the ground plane under it does, as in PEEL
const PeelPreset& preset = m_peelPresets[m_peelPreset];
int zCount = preset.columnCount - 1;
float halfWidth = 20.0f * preset.scaleX * (float)zCount;
AddGrid( vertices, indices, m_profile, halfWidth, zCount, false );
AddMesh( vertices, indices, true );
b3BoxHull box = b3MakeOffsetBoxHull( 140.0f, 1.0f, 30.0f, { 65.0f, -1.0f, 0.0f } );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateHullShape( m_groundId, &shapeDef, &box.base );
}
else
{
AddStrip( vertices, indices, m_profile.data(), (int)m_profile.size(), m_cellSize );
AddMesh( vertices, indices );
}
}
void Launch()
{
if ( B3_IS_NON_NULL( m_bodyId ) )
{
b3DestroyBody( m_bodyId );
}
// PEEL drops its bodies from rest, sized in meters rather than tenths
bool peel = m_scene == e_internalEdges;
float boxHalf = peel ? 1.0f : 0.2f;
float capsuleRadius = peel ? 1.0f : 0.2f;
float capsuleHalf = peel ? 2.0f : 0.4f;
float sphereRadius = peel ? 1.0f : 0.25f;
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
b3Quat yaw = b3MakeQuatFromAxisAngle( b3Vec3_axisY, m_yaw * B3_PI / 180.0f );
if ( peel )
{
bodyDef.position = { m_peelPresets[m_peelPreset].startZ, 80.0f, 0.0f };
bodyDef.rotation = yaw;
}
else
{
int segment = 0;
for ( int k = 0; k + 1 < (int)m_profile.size(); ++k )
{
if ( m_profile[k].x <= m_startX && m_startX < m_profile[k + 1].x )
{
segment = k;
break;
}
}
b3Vec2 a = m_profile[segment];
b3Vec2 b = m_profile[segment + 1];
float length = sqrtf( ( b.x - a.x ) * ( b.x - a.x ) + ( b.y - a.y ) * ( b.y - a.y ) );
b3Vec2 d = { ( b.x - a.x ) / length, ( b.y - a.y ) / length };
b3Vec2 n = { -d.y, d.x };
float y = a.y + ( m_startX - a.x ) * d.y / d.x;
float lift = boxHalf;
if ( m_bodyType == e_sphere )
{
lift = sphereRadius;
}
else if ( m_bodyType == e_uprightCapsule )
{
lift = capsuleHalf + capsuleRadius;
}
lift += 0.01f;
bodyDef.position = { m_startX + lift * n.x, y + lift * n.y, 0.0f };
b3Quat slope = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, atan2f( d.y, d.x ) );
bodyDef.rotation = b3MulQuat( slope, yaw );
bodyDef.linearVelocity = { m_speed * d.x, m_speed * d.y, 0.0f };
}
bodyDef.motionLocks.angularX = m_fixedRotation;
bodyDef.motionLocks.angularY = m_fixedRotation;
bodyDef.motionLocks.angularZ = m_fixedRotation;
bodyDef.enableSleep = false;
bodyDef.name = "slider";
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.baseMaterial.friction = m_friction;
shapeDef.enableSpeculativeContact = m_speculative;
switch ( m_bodyType )
{
case e_box:
{
b3BoxHull box = peel ? b3MakeBoxHull( 1.0f, 1.0f, 1.0f ) : b3MakeBoxHull( 0.4f, 0.2f, 0.4f );
m_shapeId = b3CreateHullShape( m_bodyId, &shapeDef, &box.base );
}
break;
case e_capsule:
{
b3Capsule capsule = { { 0.0f, 0.0f, -capsuleHalf }, { 0.0f, 0.0f, capsuleHalf }, capsuleRadius };
m_shapeId = b3CreateCapsuleShape( m_bodyId, &shapeDef, &capsule );
}
break;
case e_uprightCapsule:
{
b3Capsule capsule = { { 0.0f, -capsuleHalf, 0.0f }, { 0.0f, capsuleHalf, 0.0f }, capsuleRadius };
m_shapeId = b3CreateCapsuleShape( m_bodyId, &shapeDef, &capsule );
}
break;
case e_sphere:
{
b3Sphere sphere = { b3Vec3_zero, sphereRadius };
m_shapeId = b3CreateSphereShape( m_bodyId, &shapeDef, &sphere );
}
break;
default:
m_shapeId = b3CreateHullShape( m_bodyId, &shapeDef, peel ? m_peelCylinder : m_cylinder );
break;
}
m_runGhostCount = 0;
m_runSteps = 0;
}
float SegmentAngle( int k ) const
{
b3Vec2 a = m_profile[k];
b3Vec2 b = m_profile[k + 1];
return atan2f( b.x - a.x, a.y - b.y );
}
// The normal points from the ground to the body. A hull face can be the reference face, so any
// normal within the span of face normals under the body is valid.
bool IsGhost( b3Vec3 normal, float xMin, float xMax ) const
{
float tolerance = 5.0f * B3_PI / 180.0f;
if ( b3AbsFloat( normal.z ) > sinf( tolerance ) )
{
return true;
}
float lower = B3_PI;
float upper = -B3_PI;
int segmentCount = (int)m_profile.size() - 1;
for ( int k = 0; k < segmentCount; ++k )
{
float x0 = b3MinFloat( m_profile[k].x, m_profile[k + 1].x );
float x1 = b3MaxFloat( m_profile[k].x, m_profile[k + 1].x );
if ( x1 < xMin || xMax < x0 )
{
continue;
}
float angle = SegmentAngle( k );
lower = b3MinFloat( lower, angle );
upper = b3MaxFloat( upper, angle );
}
float angle = atan2f( normal.y, normal.x );
return angle < lower - tolerance || upper + tolerance < angle;
}
void Step() override
{
Sample::Step();
b3Pos center = b3Body_GetWorldCenter( m_bodyId );
bool done = center.x > m_halfLength - 1.0f || center.y < -3.0f;
if ( m_scene == e_internalEdges )
{
// Run out onto the ground plane, or give up on a body knocked off the side
done = center.x > 100.0f || center.y < -3.0f || m_runSteps > 900;
}
if ( done )
{
m_runCount += 1;
Launch();
center = b3Body_GetWorldCenter( m_bodyId );
}
if ( m_didStep )
{
m_runSteps += 1;
b3AABB bounds = b3Shape_GetAABB( m_shapeId );
float xMin = (float)bounds.lowerBound.x - 0.1f;
float xMax = (float)bounds.upperBound.x + 0.1f;
b3ContactData contacts[8];
int contactCount = b3Body_GetContactData( m_bodyId, contacts, 8 );
for ( int i = 0; i < contactCount; ++i )
{
bool bodyIsA = B3_ID_EQUALS( contacts[i].shapeIdA, m_shapeId );
b3ShapeId otherId = bodyIsA ? contacts[i].shapeIdB : contacts[i].shapeIdA;
if ( b3Shape_GetType( otherId ) != b3_meshShape )
{
continue;
}
for ( int j = 0; j < contacts[i].manifoldCount; ++j )
{
const b3Manifold* manifold = contacts[i].manifolds + j;
// Speculative points that never pushed are harmless
float impulse = 0.0f;
for ( int k = 0; k < manifold->pointCount; ++k )
{
impulse += manifold->points[k].totalNormalImpulse;
}
if ( manifold->pointCount == 0 || impulse <= 0.0f )
{
continue;
}
b3Vec3 normal = bodyIsA ? b3Neg( manifold->normal ) : manifold->normal;
if ( IsGhost( normal, xMin, xMax ) == false )
{
continue;
}
m_ghostCount += 1;
m_runGhostCount += 1;
b3Vec3 anchor = bodyIsA ? manifold->points[0].anchorA : manifold->points[0].anchorB;
m_markers[m_markerIndex] = { center + anchor, normal };
m_markerIndex = ( m_markerIndex + 1 ) % m_markerCapacity;
m_markerCount = b3MinInt( m_markerCount + 1, m_markerCapacity );
}
}
}
for ( int i = 0; i < m_markerCount; ++i )
{
b3Pos p = m_markers[i].point;
DrawPoint( p, 8.0f, MakeColor( b3_colorRed ) );
DrawLine( p, p + 0.5f * m_markers[i].normal, MakeColor( b3_colorRed ) );
}
b3Vec3 velocity = b3Body_GetLinearVelocity( m_bodyId );
DrawTextLine( "ghost manifolds: %d this run, %d total over %d runs", m_runGhostCount, m_ghostCount, m_runCount + 1 );
DrawTextLine( "speed: %.2f m/s, vertical velocity: %.2f m/s", b3Length( velocity ), velocity.y );
}
bool DrawControls() override
{
const char* sceneNames[] = { "Flat", "Concave Ramp", "Cliff", "Crest", "Bumps", "T-Junction", "Shape Seam",
"PEEL Internal Edges" };
static_assert( IM_ARRAYSIZE( sceneNames ) == e_sceneCount );
const char* bodyNames[] = { "Box", "Capsule", "Upright Capsule", "Sphere", "Cylinder" };
static_assert( IM_ARRAYSIZE( bodyNames ) == e_bodyTypeCount );
const char* presetNames[] = { "4 x 0.1, start -55", "4 x 0.1, start -54", "4 x 0.01, start -55",
"16 x 0.01, start -54" };
static_assert( IM_ARRAYSIZE( presetNames ) == IM_ARRAYSIZE( m_peelPresets ) );
bool rebuild = false;
bool relaunch = false;
int scene = (int)m_scene;
if ( ImGui::Combo( "Scene", &scene, sceneNames, IM_ARRAYSIZE( sceneNames ) ) )
{
m_scene = (Scene)scene;
rebuild = true;
if ( m_scene == e_internalEdges )
{
m_camera->SetView( 60.0f, 20.0f, 160.0f, { 0.0f, 30.0f, 0.0f } );
}
else
{
m_camera->SetView( 90.0f, 20.0f, 30.0f, { 0.0f, 1.0f, 0.0f } );
}
}
if ( m_scene == e_internalEdges )
{
rebuild |= ImGui::Combo( "Preset", &m_peelPreset, presetNames, IM_ARRAYSIZE( presetNames ) );
}
int bodyType = (int)m_bodyType;
if ( ImGui::Combo( "Body", &bodyType, bodyNames, IM_ARRAYSIZE( bodyNames ) ) )
{
m_bodyType = (BodyType)bodyType;
relaunch = true;
}
rebuild |= ImGui::Checkbox( "Mirror Mesh", &m_mirror );
rebuild |= ImGui::SliderFloat( "Cell Size", &m_cellSize, 0.1f, 1.0f, "%.2f m" );
relaunch |= ImGui::SliderFloat( "Speed", &m_speed, 0.0f, 15.0f, "%.1f m/s" );
relaunch |= ImGui::SliderFloat( "Yaw", &m_yaw, 0.0f, 45.0f, "%.0f deg" );
relaunch |= ImGui::SliderFloat( "Friction", &m_friction, 0.0f, 1.0f, "%.2f" );
relaunch |= ImGui::Checkbox( "Fixed Rotation", &m_fixedRotation );
relaunch |= ImGui::Checkbox( "Speculative", &m_speculative );
relaunch |= ImGui::Button( "Launch" );
if ( ImGui::Button( "Reset Counters" ) )
{
m_ghostCount = 0;
m_runGhostCount = 0;
m_runCount = 0;
m_markerCount = 0;
m_markerIndex = 0;
}
if ( rebuild )
{
CreateGround();
m_markerCount = 0;
m_markerIndex = 0;
}
if ( rebuild || relaunch )
{
Launch();
}
return true;
}
static Sample* Create( SampleContext* context )
{
return new GhostCollisions( context );
}
struct Marker
{
b3Pos point;
b3Vec3 normal;
};
b3MeshData* m_meshes[2] = {};
int m_meshCount = 0;
std::vector<b3Vec2> m_profile;
b3HullData* m_cylinder;
b3HullData* m_peelCylinder;
b3BodyId m_groundId;
b3BodyId m_bodyId;
b3ShapeId m_shapeId;
Scene m_scene = e_flat;
BodyType m_bodyType = e_box;
bool m_mirror = false;
float m_cellSize = 0.25f;
float m_speed = 6.0f;
float m_yaw = 0.0f;
float m_friction = 0.0f;
bool m_fixedRotation = false;
bool m_speculative = true;
int m_peelPreset = 0;
int m_runSteps = 0;
int m_ghostCount = 0;
int m_runGhostCount = 0;
int m_runCount = 0;
Marker m_markers[m_markerCapacity];
int m_markerCount = 0;
int m_markerIndex = 0;
};
static int sampleGhostCollisions = RegisterSample( "Mesh", "Ghost Collisions", GhostCollisions::Create );