2026-05-10 13:36:52 -07:00
|
|
|
// SPDX-FileCopyrightText: 2025 Erin Catto
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
#include "benchmarks.h"
|
2026-06-06 10:06:05 -07:00
|
|
|
#include "gfx/debug_adapter.h"
|
|
|
|
|
#include "gfx/draw.h"
|
2026-05-10 13:36:52 -07:00
|
|
|
#include "imgui.h"
|
|
|
|
|
#include "sample.h"
|
2026-05-10 23:50:16 -07:00
|
|
|
#include "utils.h"
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
#include "box3d/box3d.h"
|
|
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
|
2026-10-02 20:47:05 -07:00
|
|
|
#ifdef BOX3D_INTERNAL_BENCHMARKS
|
|
|
|
|
#include "sat_benchmark.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
inline bool operator<( b3BodyId a, b3BodyId b )
|
|
|
|
|
{
|
|
|
|
|
uint64_t ua = b3StoreBodyId( a );
|
|
|
|
|
uint64_t ub = b3StoreBodyId( b );
|
|
|
|
|
return ua < ub;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class BenchmarkLargePyramid : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkLargePyramid( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_camera->SetView( 40.0f, -10.0f, 110.0f, { 0.0f, 40.0f, 0.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-08-11 18:08:34 -07:00
|
|
|
m_shadowSplitFar = 180.0f;
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
CreateLargePyramid( m_worldId );
|
2026-06-06 10:06:05 -07:00
|
|
|
|
|
|
|
|
SetGroundShape( GetGroundShapeId() );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkLargePyramid( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleLargePyramid = RegisterSample( "Benchmark", "Large Pyramid", BenchmarkLargePyramid::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class BenchmarkWidePyramid : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkWidePyramid( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 5.0f, 80.0f, { 0.0f, 18.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CreateWidePyramid( m_worldId );
|
2026-06-06 10:06:05 -07:00
|
|
|
|
|
|
|
|
SetGroundShape( GetGroundShapeId() );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkWidePyramid( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleWidePyramid = RegisterSample( "Benchmark", "Wide Pyramid", BenchmarkWidePyramid::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class BenchmarkManyPyramids : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkManyPyramids( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_camera->SetView( -10.0f, 10.0f, 120.0f, { 0.0f, 5.0f, 0.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CreateManyPyramids( m_worldId );
|
|
|
|
|
|
|
|
|
|
// float frictionTorque = 5.0f;
|
|
|
|
|
// float hertz = 1.0f;
|
|
|
|
|
// float dampingRatio = 0.7f;
|
|
|
|
|
// bool colorize = true;
|
|
|
|
|
|
|
|
|
|
// Human human = {};
|
|
|
|
|
// b3Vec3 position = m_isDebug ? b3Vec3{ 0.0f, 20.0f, 0.0f } : b3Vec3{ 5.0f, 20.0f, 53.0f };
|
|
|
|
|
// CreateHuman( &human, m_worldId, position, frictionTorque, hertz, dampingRatio, 0, nullptr, colorize );
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
SetGroundShape( GetGroundShapeId() );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkManyPyramids( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleManyPyramids = RegisterSample( "Benchmark", "Many Pyramids", BenchmarkManyPyramids::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class BenchmarkRain : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkRain( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
m_camera->SetView( 25.0f, 10.0f, 70.0f, b3Pos_zero );
|
2026-06-06 10:06:05 -07:00
|
|
|
GetGuiDraw()->drawJoints = false;
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3Capacity capacity = {};
|
|
|
|
|
GetRainCapacity( &capacity );
|
|
|
|
|
CreateWorld( &capacity );
|
|
|
|
|
|
|
|
|
|
CreateRain( m_worldId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkRain() override
|
|
|
|
|
{
|
|
|
|
|
DestroyRain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
StepRain( m_worldId, m_stepCount );
|
|
|
|
|
|
|
|
|
|
// This is for testing adjustable worker count
|
|
|
|
|
// if (m_stepCount == 200)
|
|
|
|
|
//{
|
|
|
|
|
// m_scheduler->Initialize( 3 );
|
|
|
|
|
// b3World_SetWorkerCount( m_worldId, 3 );
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
Sample::Step();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Render() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Render();
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Transform t = { { 0.0f, 0.1f, 0.0f }, b3Quat_identity };
|
|
|
|
|
DrawAxes( b3MakeWorldTransform( t ), 2.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkRain( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleRain = RegisterSample( "Benchmark", "Rain", BenchmarkRain::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
class BenchmarkLargeWorld : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkLargeWorld( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Vec3_zero );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CreateRain( m_worldId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkLargeWorld() override
|
|
|
|
|
{
|
|
|
|
|
DestroyRain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
StepRain( m_worldId, m_stepCount );
|
|
|
|
|
|
|
|
|
|
// This is for testing adjustable worker count
|
|
|
|
|
// if (m_stepCount == 200)
|
|
|
|
|
//{
|
|
|
|
|
// m_scheduler->Initialize( 3 );
|
|
|
|
|
// b3World_SetWorkerCount( m_worldId, 3 );
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
Sample::Step();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Render() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Render();
|
|
|
|
|
b3Transform transform = { { 0.0f, 0.1f, 0.0f }, b3Quat_identity };
|
2026-06-06 10:06:05 -07:00
|
|
|
DrawAxes( transform, 2.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkLargeWorld( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleLargeWorld = RegisterSample( "Benchmark", "Large World", BenchmarkLargeWorld::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
class BenchmarkJointGrid : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkJointGrid( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
m_camera->SetView( -25.0f, 25.0f, 94.0f, { 30.0f, -30.0f, 30.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CreateJointGrid( m_worldId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Render() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Render();
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Transform t = { { 0.0f, 0.1f, 0.0f }, b3Quat_identity };
|
|
|
|
|
DrawAxes( b3MakeWorldTransform( t ), 4.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
// transform.p.y = 0.0f;
|
|
|
|
|
// transform.p.z = -5.0f;
|
|
|
|
|
|
|
|
|
|
// for (int i = 0; i < 100; ++i)
|
|
|
|
|
//{
|
|
|
|
|
// transform.p.x = -50.0f;
|
|
|
|
|
// for (int j = 0; j < 100; ++j)
|
|
|
|
|
// {
|
|
|
|
|
// AddSphere( m_scene, transform, 0.5f, b3_colorYellowGreen );
|
|
|
|
|
// transform.p.x += 1.0f;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// transform.p.y -= 1.0f;
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkJointGrid( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleJointGrid = RegisterSample( "Benchmark", "Joint Grid", BenchmarkJointGrid::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class FallingBoxes : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit FallingBoxes( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_camera->SetView( 45.0f, 10.0f, 80.0f, { 0.0f, 20.0f, 0.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 100.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
constexpr int n = m_isDebug ? 4 : 50;
|
2026-06-06 10:06:05 -07:00
|
|
|
float a = 0.5f;
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
2026-06-06 10:06:05 -07:00
|
|
|
b3BoxHull box = b3MakeCubeHull( a );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < n; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int j = 0; j < 8; ++j )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int k = 0; k < 8; ++k )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
bodyDef.position = { -16.0f * a + 4.0f * a * j, 4.0f * a * i + 5.0f * a, -16.0f * a + 4.0f * a * k };
|
2026-05-10 13:36:52 -07:00
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new FallingBoxes( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleFallingBoxes = RegisterSample( "Benchmark", "Falling Boxes", FallingBoxes::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class CandyCups : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit CandyCups( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
float radius = m_isDebug ? 20.0f : 70.0f;
|
2026-06-18 20:51:16 -07:00
|
|
|
m_camera->SetView( 45.0f, 20.0f, radius, b3Pos_zero );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 60.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
constexpr int n = m_isDebug ? 4 : 16;
|
|
|
|
|
constexpr int m = m_isDebug ? 4 : 16;
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
2026-06-06 10:06:05 -07:00
|
|
|
m_convex = CreateConvex( 0.6f, 0.0f, 0.95f, 1.0f );
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < n; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int j = 0; j < m; ++j )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int k = 0; k < m; ++k )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
bodyDef.position = { -10.0f + 2.5f * j, 1.0f * i, -10.0f + 2.5f * k };
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, m_convex );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~CandyCups() override
|
|
|
|
|
{
|
|
|
|
|
b3DestroyHull( m_convex );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 22:39:26 -07:00
|
|
|
b3HullData* CreateConvex( float radius1, float height1, float radius2, float height2 ) const
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
constexpr int sideCount = 8;
|
2026-05-10 13:36:52 -07:00
|
|
|
const float deltaAlpha = 2.0f * B3_PI / sideCount;
|
|
|
|
|
|
|
|
|
|
int vertexCount = 2 * sideCount;
|
2026-06-06 10:06:05 -07:00
|
|
|
b3Vec3 vertexBase[2 * sideCount];
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
float alpha = 0.0f;
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int sideIndex = 0; sideIndex < sideCount; ++sideIndex )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
b3CosSin cs = b3ComputeCosSin( alpha );
|
|
|
|
|
|
|
|
|
|
float x1 = radius1 * cs.cosine;
|
|
|
|
|
float z1 = radius1 * cs.sine;
|
|
|
|
|
float x2 = radius2 * cs.cosine;
|
|
|
|
|
float z2 = radius2 * cs.sine;
|
|
|
|
|
|
|
|
|
|
vertexBase[2 * sideIndex + 0] = { x1, height1, z1 };
|
|
|
|
|
vertexBase[2 * sideIndex + 1] = { x2, height2, z2 };
|
|
|
|
|
alpha += deltaAlpha;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return b3CreateHull( vertexBase, vertexCount, vertexCount );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new CandyCups( context );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 22:39:26 -07:00
|
|
|
b3HullData* m_convex;
|
2026-05-10 13:36:52 -07:00
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleSmallConvexes = RegisterSample( "Benchmark", "Candy Cups", CandyCups::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class BenchmarkExplosion : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkExplosion( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
float radius = m_isDebug ? 15.0f : 30.0f;
|
|
|
|
|
m_camera->SetView( 45.0f, 20.0f, radius, { 0.0f, 0.0f, 0.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
m_gridMesh = b3CreateGridMesh( 40, 40, 1.0f, 0, true );
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3CreateMeshShape( groundId, &shapeDef, m_gridMesh, b3Vec3_one );
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
float hy = 1.0f;
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
b3Transform transform;
|
2026-06-06 10:06:05 -07:00
|
|
|
transform.p = { 0.0f, hy, -20.0f };
|
2026-05-10 13:36:52 -07:00
|
|
|
transform.q = b3Quat_identity;
|
2026-06-06 10:06:05 -07:00
|
|
|
b3BoxHull wallBox = b3MakeTransformedBoxHull( 20.0f, hy, 0.1f, transform );
|
2026-05-10 13:36:52 -07:00
|
|
|
b3CreateHullShape( groundId, &shapeDef, &wallBox.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
b3Transform transform;
|
2026-06-06 10:06:05 -07:00
|
|
|
transform.p = { 0.0f, hy, 20.0f };
|
2026-05-10 13:36:52 -07:00
|
|
|
transform.q = b3Quat_identity;
|
2026-06-06 10:06:05 -07:00
|
|
|
b3BoxHull wallBox = b3MakeTransformedBoxHull( 20.0f, hy, 0.1f, transform );
|
2026-05-10 13:36:52 -07:00
|
|
|
b3CreateHullShape( groundId, &shapeDef, &wallBox.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
b3Transform transform;
|
2026-06-06 10:06:05 -07:00
|
|
|
transform.p = { -20.0f, hy, 0.0f };
|
2026-05-10 13:36:52 -07:00
|
|
|
transform.q = b3Quat_identity;
|
2026-06-06 10:06:05 -07:00
|
|
|
b3BoxHull wallBox = b3MakeTransformedBoxHull( 0.1f, hy, 20.0f, transform );
|
2026-05-10 13:36:52 -07:00
|
|
|
b3CreateHullShape( groundId, &shapeDef, &wallBox.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
b3Transform transform;
|
2026-06-06 10:06:05 -07:00
|
|
|
transform.p = { 20.0f, hy, 0.0f };
|
2026-05-10 13:36:52 -07:00
|
|
|
transform.q = b3Quat_identity;
|
2026-06-06 10:06:05 -07:00
|
|
|
b3BoxHull wallBox = b3MakeTransformedBoxHull( 0.1f, hy, 20.0f, transform );
|
2026-05-10 13:36:52 -07:00
|
|
|
b3CreateHullShape( groundId, &shapeDef, &wallBox.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Using 15 sides rather than 16 to avoid manifold degeneracies
|
|
|
|
|
m_cylinder = b3CreateCylinder( 0.5f, 0.2f, 0.0f, 15 );
|
|
|
|
|
|
|
|
|
|
constexpr int n = m_isDebug ? 3 : 16;
|
|
|
|
|
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
shapeDef.explosionScale = 2.0f;
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = -n; i <= n; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int k = -n; k <= n; ++k )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
bodyDef.position = { 1.0f * i, 0.0f, 1.0f * k };
|
|
|
|
|
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, m_cylinder );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_impulse = 1000.0f;
|
|
|
|
|
|
|
|
|
|
m_stepWhilePaused = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkExplosion() override
|
|
|
|
|
{
|
|
|
|
|
b3DestroyHull( m_cylinder );
|
|
|
|
|
b3DestroyMesh( m_gridMesh );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Explode()
|
|
|
|
|
{
|
|
|
|
|
b3ExplosionDef def = b3DefaultExplosionDef();
|
|
|
|
|
def.radius = 16.0f;
|
|
|
|
|
def.position = { 0.0f, -4.0f, 0.0f };
|
|
|
|
|
def.impulsePerArea = m_impulse;
|
|
|
|
|
|
|
|
|
|
b3World_Explode( m_worldId, &def );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
bool DrawControls() override
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
ImGui::SliderFloat( "Magnitude", &m_impulse, 0.0f, 2000.0f, "%.0f" );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
if ( ImGui::Button( "Explode" ) )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
Explode();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
return true;
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkExplosion( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3MeshData* m_gridMesh;
|
2026-06-11 22:39:26 -07:00
|
|
|
b3HullData* m_cylinder;
|
2026-05-10 13:36:52 -07:00
|
|
|
float m_impulse;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleExplosion = RegisterSample( "Benchmark", "Explosion", BenchmarkExplosion::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class BenchmarkHeightField : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
struct Context
|
|
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Pos point;
|
2026-05-10 13:36:52 -07:00
|
|
|
b3Vec3 normal;
|
|
|
|
|
float fraction;
|
|
|
|
|
bool hit;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-18 20:51:16 -07:00
|
|
|
static float CastCallback( b3ShapeId shapeId, b3Pos point, b3Vec3 normal, float fraction, uint64_t userMaterialId,
|
2026-05-10 13:36:52 -07:00
|
|
|
int triangleIndex, int childIndex, void* context )
|
|
|
|
|
{
|
|
|
|
|
(void)shapeId;
|
|
|
|
|
(void)userMaterialId;
|
|
|
|
|
(void)triangleIndex;
|
|
|
|
|
(void)childIndex;
|
|
|
|
|
|
|
|
|
|
Context* rayContext = (Context*)context;
|
|
|
|
|
rayContext->point = point;
|
|
|
|
|
rayContext->normal = normal;
|
|
|
|
|
rayContext->fraction = fraction;
|
|
|
|
|
rayContext->hit = true;
|
|
|
|
|
return fraction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicit BenchmarkHeightField( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
m_camera->SetView( 0.0f, 20.0f, 50.0f, b3Pos_zero );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_columnCount = 50;
|
|
|
|
|
m_rowCount = 50;
|
|
|
|
|
m_radius = 0.1f;
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.position = { -0.5f * m_columnCount, 0.0f, -0.5f * m_rowCount };
|
|
|
|
|
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
m_heightField = b3CreateWave( 50, 50, b3Vec3_one, 0.02f, 0.04f, true );
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
b3CreateHeightFieldShape( body, &shapeDef, m_heightField );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkHeightField() override
|
|
|
|
|
{
|
|
|
|
|
b3DestroyHeightField( m_heightField );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
bool DrawControls() override
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
ImGui::SliderFloat( "Radius", &m_radius, 0.0f, 1.0f, "%.1f" );
|
|
|
|
|
return true;
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Render() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Render();
|
|
|
|
|
|
2026-06-18 20:51:16 -07:00
|
|
|
DrawLine( b3Pos_zero, b3OffsetPos( b3Pos_zero, 0.4f * b3Vec3_axisX ), MakeColor( b3_colorRed ) );
|
|
|
|
|
DrawLine( b3Pos_zero, b3OffsetPos( b3Pos_zero, 0.4f * b3Vec3_axisY ), MakeColor( b3_colorGreen ) );
|
|
|
|
|
DrawLine( b3Pos_zero, b3OffsetPos( b3Pos_zero, 0.4f * b3Vec3_axisZ ), MakeColor( b3_colorBlue ) );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
int hitCount = 0;
|
|
|
|
|
int iterationCount = 0;
|
|
|
|
|
int innerIterationCount = 0;
|
|
|
|
|
|
|
|
|
|
float delta = m_isDebug ? 2.0f * 0.95f : 0.4f;
|
|
|
|
|
|
|
|
|
|
float spanX = 0.94f * 0.5f * m_columnCount;
|
|
|
|
|
float spanZ = 0.96f * 0.5f * m_rowCount;
|
|
|
|
|
|
|
|
|
|
uint64_t startTick = b3GetTicks();
|
|
|
|
|
|
|
|
|
|
b3Vec3 rayTranslation = { 80000.0f, -80000.0f, 8.0f };
|
|
|
|
|
|
|
|
|
|
int castCount = 0;
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( float x = -spanX; x <= spanX; x += delta )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( float z = -spanZ; z <= spanZ; z += delta )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Pos rayOrigin = { x, 2.0f, z };
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
b3RayResult result = {};
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_radius == 0.0f )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
result = b3World_CastRayClosest( m_worldId, rayOrigin, rayTranslation, b3DefaultQueryFilter() );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Context context = {};
|
2026-06-18 20:51:16 -07:00
|
|
|
b3ShapeProxy proxy = { &b3Vec3_zero, 1, m_radius };
|
|
|
|
|
b3World_CastShape( m_worldId, rayOrigin, &proxy, rayTranslation, b3DefaultQueryFilter(), CastCallback,
|
|
|
|
|
&context );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context.hit )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
result.point = context.point;
|
|
|
|
|
result.normal = context.normal;
|
|
|
|
|
result.fraction = context.fraction;
|
|
|
|
|
result.hit = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
castCount += 1;
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( result.hit )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
hitCount += 1;
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_isDebug )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Pos point = result.point;
|
2026-06-06 10:06:05 -07:00
|
|
|
DrawLine( point, point + 0.5f * result.normal, MakeColor( b3_colorGreen ) );
|
|
|
|
|
DrawPoint( point, 10.0f, MakeColor( b3_colorGreen ) );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_radius > 0.0f )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
b3WorldTransform t = b3WorldTransform_identity;
|
|
|
|
|
t.p = rayOrigin + result.fraction * rayTranslation;
|
|
|
|
|
DrawSphereEx( t, m_radius, MakeColorAlpha( b3_colorPurple, 0.5f ), 0.0f, 0.5f,
|
2026-06-06 10:06:05 -07:00
|
|
|
TRANSPARENT_SHADOW_NONE );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Pos rayEnd = rayOrigin + result.fraction * rayTranslation;
|
2026-06-06 10:06:05 -07:00
|
|
|
DrawLine( rayOrigin, rayEnd, MakeColor( b3_colorYellow ) );
|
|
|
|
|
DrawPoint( rayOrigin, 2.0f, MakeColor( b3_colorRed ) );
|
|
|
|
|
DrawPoint( rayEnd, 2.0f, MakeColor( b3_colorRed ) );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
2026-05-10 23:50:16 -07:00
|
|
|
else if ( m_isDebug )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Pos rayEnd = rayOrigin + rayTranslation;
|
2026-06-06 10:06:05 -07:00
|
|
|
DrawLine( rayOrigin, rayEnd, MakeColor( b3_colorYellow ) );
|
|
|
|
|
DrawPoint( rayOrigin, 2.0f, MakeColor( b3_colorRed ) );
|
|
|
|
|
DrawPoint( rayEnd, 2.0f, MakeColor( b3_colorRed ) );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float milliseconds = b3GetMilliseconds( startTick );
|
2026-07-24 21:48:23 -07:00
|
|
|
int tickCount = (int)(b3GetTicks() - startTick);
|
2026-05-10 13:36:52 -07:00
|
|
|
float aveIterations = float( iterationCount ) / float( castCount );
|
|
|
|
|
float aveInner = float( innerIterationCount ) / float( castCount );
|
|
|
|
|
|
|
|
|
|
float aveCastTime = 1000.0f * milliseconds / float( castCount );
|
|
|
|
|
|
2026-07-24 21:48:23 -07:00
|
|
|
DrawTextLine( "count = %d, hit count = %d, iterations = %d, inner = %d, ticks = %d", castCount, hitCount, iterationCount,
|
2026-05-10 13:36:52 -07:00
|
|
|
innerIterationCount, tickCount );
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "ave iterations = %.1f, ave inner = %.1f, ave cast us %.3f", aveIterations, aveInner, aveCastTime );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkHeightField( context );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-20 23:35:04 -07:00
|
|
|
b3HeightFieldData* m_heightField;
|
2026-05-10 13:36:52 -07:00
|
|
|
int m_columnCount;
|
|
|
|
|
int m_rowCount;
|
|
|
|
|
float m_radius;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleHeightFieldBenchmark = RegisterSample( "Benchmark", "Height Field", BenchmarkHeightField::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
class BenchmarkFallingTrees : public Sample
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
public:
|
2026-05-10 23:50:16 -07:00
|
|
|
explicit BenchmarkFallingTrees( SampleContext* context )
|
2026-05-10 13:36:52 -07:00
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_camera->SetView( 20.0f, 0.0f, 140.0f, { 0.0f, 15.0f, 0.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
m_gridSize = 100;
|
|
|
|
|
CreateTrees100( m_worldId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkFallingTrees() override
|
|
|
|
|
{
|
|
|
|
|
DestroyTrees();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Generate()
|
|
|
|
|
{
|
|
|
|
|
ResetProfile();
|
|
|
|
|
|
|
|
|
|
CreateWorld( nullptr );
|
|
|
|
|
|
|
|
|
|
DestroyTrees();
|
|
|
|
|
|
|
|
|
|
if ( m_gridSize == 100 )
|
|
|
|
|
{
|
|
|
|
|
CreateTrees100( m_worldId );
|
|
|
|
|
}
|
|
|
|
|
else if ( m_gridSize == 50 )
|
|
|
|
|
{
|
|
|
|
|
CreateTrees50( m_worldId );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CreateTrees25( m_worldId );
|
|
|
|
|
}
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
bool DrawControls() override
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( ImGui::RadioButton( "100cm", &m_gridSize, 100 ) )
|
|
|
|
|
{
|
|
|
|
|
Generate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ImGui::RadioButton( "50cm", &m_gridSize, 50 ) )
|
|
|
|
|
{
|
|
|
|
|
Generate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ImGui::RadioButton( "25cm", &m_gridSize, 25 ) )
|
|
|
|
|
{
|
|
|
|
|
Generate();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
return true;
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
return new BenchmarkFallingTrees( context );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
2026-05-10 23:50:16 -07:00
|
|
|
|
|
|
|
|
int m_gridSize;
|
2026-05-10 13:36:52 -07:00
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleFallingTrees = RegisterSample( "Benchmark", "Falling Trees", BenchmarkFallingTrees::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-10-06 09:51:48 -07:00
|
|
|
class BenchmarkMeshDrop : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkMeshDrop( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 30.0f, 140.0f, b3Pos_zero );
|
|
|
|
|
GetGuiDraw()->forceScale = 0.1f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CreateMeshDropBenchmark( m_worldId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkMeshDrop() override
|
|
|
|
|
{
|
|
|
|
|
DestroyMeshDropBenchmark();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkMeshDrop( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int sampleMeshDropBenchmark = RegisterSample( "Benchmark", "Mesh Drop", BenchmarkMeshDrop::Create );
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
struct ShapeUserData
|
|
|
|
|
{
|
|
|
|
|
int row;
|
|
|
|
|
bool active;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class BenchmarkSensor : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkSensor( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_camera->SetView( 0.0f, 0.0f, 250.0f, { 0.0f, 110.0f, 0.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3World_SetCustomFilterCallback( m_worldId, FilterFcn, this );
|
|
|
|
|
|
|
|
|
|
m_activeSensor.row = 0;
|
|
|
|
|
m_activeSensor.active = true;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
float gridSize = 3.0f;
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
// These destroy anything they touch, including themselves.
|
|
|
|
|
b3BoxHull box = b3MakeCubeHull( 0.48f * gridSize );
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.isSensor = true;
|
|
|
|
|
shapeDef.enableSensorEvents = true;
|
|
|
|
|
shapeDef.userData = &m_activeSensor;
|
2026-06-06 10:06:05 -07:00
|
|
|
shapeDef.baseMaterial.customColor = b3MakeDebugColor( (b3HexColor)0x505050u, b3_debugMaterialMetallic );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
float y = 0.0f;
|
|
|
|
|
float x = -40.0f * gridSize;
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < 81; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
bodyDef.position = { x, y, 0.0f };
|
|
|
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
2026-05-10 13:36:52 -07:00
|
|
|
b3CreateHullShape( groundId, &shapeDef, &box.base );
|
|
|
|
|
x += gridSize;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
{
|
|
|
|
|
g_randomSeed = 42;
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
float shift = 5.0f;
|
|
|
|
|
float xCenter = 0.5f * shift * m_columnCount;
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
b3BoxHull box = b3MakeCubeHull( 0.5f );
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.isSensor = true;
|
|
|
|
|
shapeDef.enableSensorEvents = true;
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
float yStart = 10.0f;
|
|
|
|
|
m_filterRow = m_rowCount >> 1;
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
for ( int j = 0; j < m_rowCount; ++j )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_passiveSensors[j].row = j;
|
|
|
|
|
m_passiveSensors[j].active = false;
|
|
|
|
|
shapeDef.userData = m_passiveSensors + j;
|
|
|
|
|
|
|
|
|
|
if ( j == m_filterRow )
|
|
|
|
|
{
|
|
|
|
|
shapeDef.enableCustomFiltering = true;
|
|
|
|
|
shapeDef.baseMaterial.customColor = b3_colorFuchsia;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
shapeDef.enableCustomFiltering = false;
|
|
|
|
|
shapeDef.baseMaterial.customColor = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float y = j * shift + yStart;
|
|
|
|
|
for ( int i = 0; i < m_columnCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
float x = i * shift - xCenter;
|
|
|
|
|
bodyDef.position = { x, y, 0.0f };
|
|
|
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3CreateHullShape( groundId, &shapeDef, &box.base );
|
|
|
|
|
}
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_maxBeginCount = 0;
|
|
|
|
|
m_maxEndCount = 0;
|
|
|
|
|
m_lastStepCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateRow( float y )
|
|
|
|
|
{
|
|
|
|
|
float shift = 5.0f;
|
|
|
|
|
float xCenter = 0.5f * shift * m_columnCount;
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.gravityScale = 0.0f;
|
|
|
|
|
bodyDef.linearVelocity = { 0.0f, -5.0f };
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.enableSensorEvents = true;
|
|
|
|
|
|
|
|
|
|
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f };
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < m_columnCount; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
// stagger bodies to avoid bunching up events into a single update
|
|
|
|
|
float yOffset = RandomFloatRange( -1.0f, 1.0f );
|
|
|
|
|
bodyDef.position = { shift * i - xCenter, y + yOffset, 0.0f };
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Step();
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_stepCount == m_lastStepCount )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::set<b3BodyId> zombies;
|
|
|
|
|
|
|
|
|
|
b3SensorEvents events = b3World_GetSensorEvents( m_worldId );
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < events.beginCount; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
b3SensorBeginTouchEvent* event = events.beginEvents + i;
|
|
|
|
|
|
|
|
|
|
// shapes on begin touch are always valid
|
|
|
|
|
|
|
|
|
|
ShapeUserData* userData = static_cast<ShapeUserData*>( b3Shape_GetUserData( event->sensorShapeId ) );
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( userData->active )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
zombies.emplace( b3Shape_GetBody( event->visitorShapeId ) );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Check custom filter correctness
|
|
|
|
|
assert( userData->row != m_filterRow );
|
|
|
|
|
|
|
|
|
|
// Modify color while overlapped with a sensor
|
|
|
|
|
b3SurfaceMaterial surfaceMaterial = b3Shape_GetSurfaceMaterial( event->visitorShapeId );
|
|
|
|
|
surfaceMaterial.customColor = b3_colorLime;
|
|
|
|
|
b3Shape_SetSurfaceMaterial( event->visitorShapeId, surfaceMaterial );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < events.endCount; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
b3SensorEndTouchEvent* event = events.endEvents + i;
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( b3Shape_IsValid( event->visitorShapeId ) == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restore color to default
|
|
|
|
|
b3SurfaceMaterial surfaceMaterial = b3Shape_GetSurfaceMaterial( event->visitorShapeId );
|
|
|
|
|
surfaceMaterial.customColor = 0;
|
|
|
|
|
b3Shape_SetSurfaceMaterial( event->visitorShapeId, surfaceMaterial );
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( b3BodyId bodyId : zombies )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
b3DestroyBody( bodyId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int delay = 0x1F;
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( ( m_stepCount & delay ) == 0 )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
CreateRow( 10.0f + m_rowCount * 5.0f );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_lastStepCount = m_stepCount;
|
|
|
|
|
|
|
|
|
|
m_maxBeginCount = b3MaxInt( events.beginCount, m_maxBeginCount );
|
|
|
|
|
m_maxEndCount = b3MaxInt( events.endCount, m_maxEndCount );
|
|
|
|
|
DrawTextLine( "max begin touch events = %d", m_maxBeginCount );
|
|
|
|
|
DrawTextLine( "max end touch events = %d", m_maxEndCount );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Filter( b3ShapeId idA, b3ShapeId idB )
|
|
|
|
|
{
|
|
|
|
|
ShapeUserData* userData = nullptr;
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( b3Shape_IsSensor( idA ) )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
userData = (ShapeUserData*)b3Shape_GetUserData( idA );
|
|
|
|
|
}
|
2026-05-10 23:50:16 -07:00
|
|
|
else if ( b3Shape_IsSensor( idB ) )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
userData = (ShapeUserData*)b3Shape_GetUserData( idB );
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( userData != nullptr )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
return userData->active == true || userData->row != m_filterRow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool FilterFcn( b3ShapeId idA, b3ShapeId idB, void* context )
|
|
|
|
|
{
|
|
|
|
|
BenchmarkSensor* self = (BenchmarkSensor*)context;
|
|
|
|
|
return self->Filter( idA, idB );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkSensor( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr int m_columnCount = 40;
|
|
|
|
|
static constexpr int m_rowCount = 40;
|
|
|
|
|
int m_maxBeginCount;
|
|
|
|
|
int m_maxEndCount;
|
|
|
|
|
ShapeUserData m_passiveSensors[m_rowCount];
|
|
|
|
|
ShapeUserData m_activeSensor;
|
|
|
|
|
int m_lastStepCount;
|
|
|
|
|
int m_filterRow;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int benchmarkSensor = RegisterSample( "Benchmark", "Sensor", BenchmarkSensor::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class BenchmarkWasher : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkWasher( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_camera->SetView( 15.0f, 20.0f, 60.0, { 0.0f, 15.0f, 0.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3Capacity capacity = {};
|
|
|
|
|
GetWasherCapacity( &capacity );
|
|
|
|
|
CreateWorld( &capacity );
|
|
|
|
|
|
|
|
|
|
CreateWasher( m_worldId );
|
2026-06-06 10:06:05 -07:00
|
|
|
SetGroundShape( GetGroundShapeId() );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkWasher( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int benchmarkWasher = RegisterSample( "Benchmark", "Washer", BenchmarkWasher::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-05-16 23:27:25 -07:00
|
|
|
class BenchmarkLargeWorld : public Sample
|
2026-05-13 21:01:34 -07:00
|
|
|
{
|
|
|
|
|
public:
|
2026-05-16 23:27:25 -07:00
|
|
|
explicit BenchmarkLargeWorld( SampleContext* context )
|
2026-05-13 21:01:34 -07:00
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
m_camera->SetView( 0.0f, 10.0f, 250.0f, b3Pos_zero );
|
2026-05-13 21:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3Capacity capacity = {};
|
2026-05-16 23:27:25 -07:00
|
|
|
GetLargeWorldCapacity( &capacity );
|
2026-05-13 21:01:34 -07:00
|
|
|
CreateWorld( &capacity );
|
|
|
|
|
|
2026-05-16 23:27:25 -07:00
|
|
|
CreateLargeWorld( m_worldId );
|
2026-05-13 21:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
2026-05-16 23:27:25 -07:00
|
|
|
StepLargeWorld( m_worldId, m_stepCount );
|
2026-05-13 21:01:34 -07:00
|
|
|
Sample::Step();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
2026-05-16 23:27:25 -07:00
|
|
|
return new BenchmarkLargeWorld( context );
|
2026-05-13 21:01:34 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleLargeWorld = RegisterSample( "Benchmark", "Large World", BenchmarkLargeWorld::Create );
|
2026-05-13 21:01:34 -07:00
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
class BenchmarkHull : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkHull( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
m_camera->SetView( 0.0f, 15.0f, 5.0f, b3Pos_zero );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_randomSeed = 42;
|
|
|
|
|
|
2026-08-11 18:08:34 -07:00
|
|
|
for ( int i = 0; i < m_capacity; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
m_points[i] = RandomVec3( { -1.0f, -1.0f, -1.0f }, { 1.0f, 1.0f, 1.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 18:08:34 -07:00
|
|
|
m_hull = b3CreateHull( m_points, m_capacity, m_count );
|
2026-05-10 13:36:52 -07:00
|
|
|
m_scale = { -1.0f, 1.0f, 1.0f };
|
|
|
|
|
m_transformedHull = b3CloneAndTransformHull( m_hull, b3Transform_identity, m_scale );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkHull() override
|
|
|
|
|
{
|
|
|
|
|
b3DestroyHull( m_hull );
|
|
|
|
|
b3DestroyHull( m_transformedHull );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Render() override
|
|
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Transform t1 = { { -2.0f, 0.0f, 0.0f }, b3Quat_identity };
|
|
|
|
|
b3Transform t2 = { { 2.0f, 0.0f, 0.0f }, b3Quat_identity };
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-06-18 20:51:16 -07:00
|
|
|
DrawHull( b3MakeWorldTransform( t1 ), m_hull, MakeColor( b3_colorGreen ) );
|
|
|
|
|
DrawHull( b3MakeWorldTransform( t2 ), m_transformedHull, MakeColor( b3_colorYellow ) );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
Sample::Render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
uint64_t startTick = b3GetTicks();
|
|
|
|
|
float area = 0.0f;
|
|
|
|
|
int trials = m_isDebug ? 200 : 2000;
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < trials; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-08-11 18:08:34 -07:00
|
|
|
b3HullData* hull = b3CreateHull( m_points, m_capacity, m_count );
|
2026-05-10 13:36:52 -07:00
|
|
|
area += hull->surfaceArea;
|
|
|
|
|
b3DestroyHull( hull );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float createTime = b3GetMillisecondsAndReset( &startTick );
|
|
|
|
|
|
|
|
|
|
float scaledArea = 0.0f;
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < trials; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-11 22:39:26 -07:00
|
|
|
b3HullData* hull = b3CloneAndTransformHull( m_hull, b3Transform_identity, m_scale );
|
2026-05-10 13:36:52 -07:00
|
|
|
scaledArea += hull->surfaceArea;
|
|
|
|
|
b3DestroyHull( hull );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float cloneTime = b3GetMilliseconds( startTick );
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "trials = %d", trials );
|
|
|
|
|
DrawTextLine( "createTime (us) = %.2f, area = %.2f", 1000.0f * createTime / trials, area / trials );
|
|
|
|
|
DrawTextLine( "cloneTime (us) = %.2f, area = %.2f", 1000.0f * cloneTime / trials, scaledArea / trials );
|
|
|
|
|
DrawTextLine( "createTime / cloneTime = %.2f", createTime / cloneTime );
|
|
|
|
|
Sample::Step();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* sampleContext )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkHull( sampleContext );
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 18:08:34 -07:00
|
|
|
static constexpr int m_capacity = 100;
|
|
|
|
|
static constexpr int m_count = 16;
|
2026-06-11 22:39:26 -07:00
|
|
|
b3HullData* m_hull;
|
|
|
|
|
b3HullData* m_transformedHull;
|
2026-05-10 13:36:52 -07:00
|
|
|
b3Vec3 m_scale;
|
|
|
|
|
b3Vec3 m_points[m_capacity];
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleBenchmarkHull = RegisterSample( "Benchmark", "Hull", BenchmarkHull::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class BenchmarkChains : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkChains( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 15.0f, 50.0f, { 0.0f, 5.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
m_meshData = b3CreateWaveMesh( 80, 80, 1.0f, 0.5f, 0.05f, 0.01f );
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
b3CreateMeshShape( groundId, &shapeDef, m_meshData, b3Vec3_one );
|
|
|
|
|
|
|
|
|
|
float linkRadius = 0.125f;
|
|
|
|
|
float linkExtent = 0.25f;
|
|
|
|
|
b3Capsule capsule = { { 0.0f, -linkExtent, 0.0f }, { 0.0f, linkExtent, 0.0f }, linkRadius };
|
|
|
|
|
|
|
|
|
|
bodyDef.enableSleep = false;
|
|
|
|
|
|
|
|
|
|
int linkCount = 4;
|
|
|
|
|
|
|
|
|
|
b3SphericalJointDef jointDef = b3DefaultSphericalJointDef();
|
|
|
|
|
jointDef.base.localFrameA = { { 0.0f, -linkExtent, 0.0f }, b3Quat_identity };
|
|
|
|
|
jointDef.base.localFrameB = { { 0.0f, linkExtent, 0.0f }, b3Quat_identity };
|
|
|
|
|
jointDef.enableSpring = true;
|
|
|
|
|
jointDef.hertz = 1.0f;
|
|
|
|
|
jointDef.dampingRatio = 0.7f;
|
|
|
|
|
jointDef.enableMotor = true;
|
|
|
|
|
jointDef.maxMotorTorque = 1.0f;
|
|
|
|
|
|
|
|
|
|
int shapeIndex = 0;
|
|
|
|
|
|
|
|
|
|
float x = -1.0f * m_gridCount;
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int rowIndex = 0; rowIndex < m_gridCount; ++rowIndex )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
float z = -1.0f * m_gridCount;
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int columnIndex = 0; columnIndex < m_gridCount; ++columnIndex )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < linkCount; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
bodyDef.position = { x, ( 1.0f - 2.0f * i ) * linkExtent + 3.0f, z };
|
|
|
|
|
|
|
|
|
|
bodyDef.type = i == 0 ? b3_staticBody : b3_dynamicBody;
|
|
|
|
|
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3ShapeId shapeId = b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( i == linkCount - 1 )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
m_shapeIds[shapeIndex] = shapeId;
|
|
|
|
|
shapeIndex += 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( i > 0 )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
jointDef.base.bodyIdB = bodyId;
|
|
|
|
|
b3CreateSphericalJoint( m_worldId, &jointDef );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jointDef.base.bodyIdA = bodyId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
z += 2.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
x += 2.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_noise = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkChains() override
|
|
|
|
|
{
|
|
|
|
|
b3DestroyMesh( m_meshData );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
b3Vec3 baseWind = { 20.0f, 0.0f, 0.0f };
|
|
|
|
|
float speed;
|
|
|
|
|
b3Vec3 direction = b3GetLengthAndNormalize( &speed, baseWind );
|
|
|
|
|
b3Vec3 wind = b3MulSV( speed, b3Add( direction, m_noise ) );
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < m_gridCount * m_gridCount; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
b3Shape_ApplyWind( m_shapeIds[i], wind, 1.0f, 1.0f, 20.0f, false );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3Vec3 rand = RandomVec3( { -0.3f, -0.3f, -0.3f }, { 0.3f, 0.3f, 0.3f } );
|
|
|
|
|
m_noise = b3Lerp( m_noise, rand, 0.05f );
|
|
|
|
|
|
|
|
|
|
Sample::Step();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkChains( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr int m_gridCount = m_isDebug ? 10 : 25;
|
|
|
|
|
b3ShapeId m_shapeIds[m_gridCount * m_gridCount];
|
|
|
|
|
b3MeshData* m_meshData;
|
|
|
|
|
b3Vec3 m_noise;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleBenchmarkChains = RegisterSample( "Benchmark", "Chains", BenchmarkChains::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class BenchmarkDestruction : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkDestruction( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_context->restart == false )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_small )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_camera->SetView( 0.0f, 40.0f, 20.0f, { 0.0f, 0.0f, 0.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 40.0f, 30.0f, { 0.0f, 0.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bodyCapacity = m_gridCount * m_gridCount * m_gridCount;
|
|
|
|
|
|
|
|
|
|
b3Capacity capacity = {};
|
|
|
|
|
capacity.dynamicShapeCount = bodyCapacity;
|
|
|
|
|
capacity.dynamicBodyCount = bodyCapacity;
|
|
|
|
|
capacity.contactCount = m_small ? 10000 : 50000;
|
|
|
|
|
CreateWorld( &capacity );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
m_gridMesh = b3CreateGridMesh( 40, 40, 1.0f, 0, true );
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3CreateMeshShape( groundId, &shapeDef, m_gridMesh, b3Vec3_one );
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
#if 0
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
b3Transform transform;
|
|
|
|
|
transform.p = { 0.0f, 5.0f, -20.0f };
|
|
|
|
|
transform.q = b3Quat_identity;
|
|
|
|
|
b3BoxHull wallBox = b3MakeTransformedBoxHull( 20.0f, 5.0f, 0.1f, transform );
|
|
|
|
|
shapeDef.name = "wall1";
|
|
|
|
|
b3CreateHullShape( groundId, &shapeDef, &wallBox.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
b3Transform transform;
|
|
|
|
|
transform.p = { 0.0f, 5.0f, 20.0f };
|
|
|
|
|
transform.q = b3Quat_identity;
|
|
|
|
|
b3BoxHull wallBox = b3MakeTransformedBoxHull( 20.0f, 5.0f, 0.1f, transform );
|
|
|
|
|
shapeDef.name = "wall2";
|
|
|
|
|
b3CreateHullShape( groundId, &shapeDef, &wallBox.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
b3Transform transform;
|
|
|
|
|
transform.p = { -20.0f, 5.0f, 0.0f };
|
|
|
|
|
transform.q = b3Quat_identity;
|
|
|
|
|
b3BoxHull wallBox = b3MakeTransformedBoxHull( 0.1f, 5.0f, 20.0f, transform );
|
|
|
|
|
shapeDef.name = "wall3";
|
|
|
|
|
b3CreateHullShape( groundId, &shapeDef, &wallBox.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
b3Transform transform;
|
|
|
|
|
transform.p = { 20.0f, 5.0f, 0.0f };
|
|
|
|
|
transform.q = b3Quat_identity;
|
|
|
|
|
b3BoxHull wallBox = b3MakeTransformedBoxHull( 0.1f, 5.0f, 20.0f, transform );
|
|
|
|
|
shapeDef.name = "wall4";
|
|
|
|
|
b3CreateHullShape( groundId, &shapeDef, &wallBox.base );
|
|
|
|
|
}
|
2026-06-06 10:06:05 -07:00
|
|
|
#endif
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
m_bodyIds = (b3BodyId*)malloc( bodyCapacity * sizeof( b3BodyId ) );
|
|
|
|
|
memset( m_bodyIds, 0, bodyCapacity * sizeof( b3BodyId ) );
|
|
|
|
|
m_bodyCount = 0;
|
|
|
|
|
|
|
|
|
|
m_explosionDef = b3DefaultExplosionDef();
|
|
|
|
|
m_explosionDef.radius = m_extent;
|
|
|
|
|
m_explosionDef.falloff = 0.5f * m_extent;
|
|
|
|
|
m_explosionDef.position = { 0.0f, 2.0f * m_extent, 0.0f };
|
|
|
|
|
m_explosionDef.impulsePerArea = m_small ? 200.0f : 1000.0f;
|
|
|
|
|
|
|
|
|
|
m_spawnMilliseconds = 0.0f;
|
|
|
|
|
m_destroyMilliseconds = 0.0f;
|
|
|
|
|
|
|
|
|
|
Spawn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkDestruction() override
|
|
|
|
|
{
|
|
|
|
|
b3DestroyMesh( m_gridMesh );
|
|
|
|
|
free( m_bodyIds );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DestroyBodies()
|
|
|
|
|
{
|
|
|
|
|
uint64_t ticks = b3GetTicks();
|
|
|
|
|
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < m_bodyCount; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
b3DestroyBody( m_bodyIds[i] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_destroyMilliseconds = b3GetMilliseconds( ticks );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Spawn()
|
|
|
|
|
{
|
|
|
|
|
uint64_t ticks = b3GetTicks();
|
|
|
|
|
|
|
|
|
|
float a = m_extent / m_gridCount;
|
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 0.8f * a, 0.8f * a, 0.8f * a );
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
|
|
|
|
|
int randomRange = m_small ? 3 : 2;
|
|
|
|
|
m_bodyCount = 0;
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int i = 0; i < m_gridCount; ++i )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int j = 0; j < m_gridCount; ++j )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
for ( int k = 0; k < m_gridCount; ++k )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( RandomIntRange( 1, randomRange ) == 1 )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bodyDef.position = { ( 2.0f * i - m_gridCount + 1.0f ) * a, ( 2.0f * j + 1.0f ) * a,
|
|
|
|
|
( 2.0f * k - m_gridCount + 1.0f ) * a };
|
|
|
|
|
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
|
|
|
|
|
|
|
|
|
m_bodyIds[m_bodyCount] = bodyId;
|
|
|
|
|
m_bodyCount += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3World_Explode( m_worldId, &m_explosionDef );
|
|
|
|
|
|
|
|
|
|
m_spawnMilliseconds = b3GetMilliseconds( ticks );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Render() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Render();
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "spawn = %.2f ms", m_spawnMilliseconds );
|
|
|
|
|
DrawTextLine( "destroy = %.2f ms", m_destroyMilliseconds );
|
|
|
|
|
|
|
|
|
|
float r = m_explosionDef.radius;
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Sphere sphere1 = { b3Vec3_zero, r };
|
|
|
|
|
DrawWireSphere( { m_explosionDef.position, b3Quat_identity }, &sphere1, 24, MakeColor( b3_colorAqua ) );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
float rf = r + m_explosionDef.falloff;
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Sphere sphere2 = { b3Vec3_zero, rf };
|
|
|
|
|
DrawWireSphere( { m_explosionDef.position, b3Quat_identity }, &sphere2, 24, MakeColor( b3_colorCornsilk ) );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Step();
|
|
|
|
|
|
|
|
|
|
int spawnStep = m_small ? 80 : 140;
|
2026-05-10 23:50:16 -07:00
|
|
|
if ( m_stepCount % spawnStep == 0 )
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
DestroyBodies();
|
|
|
|
|
Spawn();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkDestruction( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr bool m_small = m_isDebug;
|
|
|
|
|
static constexpr int m_gridCount = m_small ? 6 : 20;
|
|
|
|
|
static constexpr float m_extent = m_small ? 0.75f : 2.5f;
|
|
|
|
|
|
|
|
|
|
b3BodyId* m_bodyIds;
|
|
|
|
|
int m_bodyCount;
|
|
|
|
|
b3ExplosionDef m_explosionDef;
|
|
|
|
|
b3MeshData* m_gridMesh;
|
|
|
|
|
float m_spawnMilliseconds;
|
|
|
|
|
float m_destroyMilliseconds;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleDestruction = RegisterSample( "Benchmark", "Destruction", BenchmarkDestruction::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class BenchmarkJunkyard : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkJunkyard( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
m_camera->SetView( 45.0f, 30.0f, 125.0f, b3Pos_zero );
|
2026-06-06 10:06:05 -07:00
|
|
|
GetGuiDraw()->drawJoints = false;
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-07-18 22:43:03 -07:00
|
|
|
b3Capacity capacity = {};
|
|
|
|
|
GetJunkyardCapacity( &capacity );
|
|
|
|
|
CreateWorld( &capacity );
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
CreateJunkyard( m_worldId );
|
2026-06-06 10:06:05 -07:00
|
|
|
|
|
|
|
|
SetGroundShape( GetGroundShapeId() );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
2026-09-16 22:28:39 -07:00
|
|
|
if ( m_context->pause == false || m_context->singleStep > 0 )
|
2026-06-18 20:51:16 -07:00
|
|
|
{
|
|
|
|
|
StepJunkyard( m_worldId, m_stepCount );
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
Sample::Step();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkJunkyard( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleJunkyard = RegisterSample( "Benchmark", "Junkyard", BenchmarkJunkyard::Create );
|
2026-07-10 23:21:37 -07:00
|
|
|
|
|
|
|
|
class BenchmarkConvexPile : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkConvexPile( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 45.0f, 20.0f, 150.0f, { 0.0f, 15.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 18:08:34 -07:00
|
|
|
// Watched from 150 m out, so the settled pile sits between 100 and 180 m
|
|
|
|
|
// of view depth and the guessed range stops less than halfway into it.
|
|
|
|
|
m_shadowSplitFar = 180.0f;
|
|
|
|
|
|
2026-07-18 22:43:03 -07:00
|
|
|
b3Capacity capacity = {};
|
|
|
|
|
GetConvexPileCapacity( &capacity );
|
|
|
|
|
CreateWorld( &capacity );
|
|
|
|
|
|
2026-07-10 23:21:37 -07:00
|
|
|
CreateConvexPile( m_worldId );
|
|
|
|
|
|
|
|
|
|
SetGroundShape( GetGroundShapeId() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkConvexPile( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int sampleConvexPile = RegisterSample( "Benchmark", "Convex Pile", BenchmarkConvexPile::Create );
|
2026-09-16 22:28:39 -07:00
|
|
|
|
|
|
|
|
class BenchmarkSpinner : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkSpinner( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( m_context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 12.0f, 45.0f, { 0.0f, 12.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3Capacity capacity = {};
|
|
|
|
|
GetSpinnerCapacity( &capacity );
|
|
|
|
|
CreateWorld( &capacity );
|
|
|
|
|
|
|
|
|
|
CreateSpinner( m_worldId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkSpinner() override
|
|
|
|
|
{
|
|
|
|
|
DestroySpinner();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Step();
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "spinner angle = %.2f", GetSpinnerAngle() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkSpinner( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int benchmarkSpinner = RegisterSample( "Benchmark", "Spinner", BenchmarkSpinner::Create );
|
|
|
|
|
|
|
|
|
|
class BenchmarkSleep : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkSleep( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( m_context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( -10.0f, 25.0f, 100.0f, { 0.0f, 3.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3Capacity capacity = {};
|
|
|
|
|
GetSleepCapacity( &capacity );
|
|
|
|
|
CreateWorld( &capacity );
|
|
|
|
|
|
|
|
|
|
CreateSleep( m_worldId );
|
|
|
|
|
SetGroundShape( GetGroundShapeId() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
if ( m_context->pause == false || m_context->singleStep > 0 )
|
|
|
|
|
{
|
|
|
|
|
StepSleep( m_worldId, m_stepCount );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sample::Step();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkSleep( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int benchmarkSleep = RegisterSample( "Benchmark", "Sleep", BenchmarkSleep::Create );
|
2026-10-02 20:47:05 -07:00
|
|
|
|
|
|
|
|
#ifdef BOX3D_INTERNAL_BENCHMARKS
|
|
|
|
|
|
|
|
|
|
// GJK versus SAT on hull pairs held inside the speculative margin. Times are per query and smoothed over frames.
|
|
|
|
|
class BenchmarkSeparatingAxis : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BenchmarkSeparatingAxis( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( m_context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 15.0f, 4.0f, b3Pos_zero );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_data = CreateSatBenchmark( (SatHullType)m_typeA, (SatHullType)m_typeB );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BenchmarkSeparatingAxis() override
|
|
|
|
|
{
|
|
|
|
|
DestroySatBenchmark( m_data );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HasSolverControls() const override
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HasProfile() const override
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DrawControls() override
|
|
|
|
|
{
|
|
|
|
|
const char* hullNames[] = { "Complex", "Rock", "Cylinder" };
|
|
|
|
|
static_assert( IM_ARRAYSIZE( hullNames ) == satHull_count );
|
|
|
|
|
|
|
|
|
|
ImGui::PushItemWidth( 8.0f * ImGui::GetFontSize() );
|
|
|
|
|
|
|
|
|
|
bool changed = ImGui::Combo( "Hull A", &m_typeA, hullNames, IM_ARRAYSIZE( hullNames ) );
|
|
|
|
|
changed = ImGui::Combo( "Hull B", &m_typeB, hullNames, IM_ARRAYSIZE( hullNames ) ) || changed;
|
|
|
|
|
if ( changed )
|
|
|
|
|
{
|
|
|
|
|
DestroySatBenchmark( m_data );
|
|
|
|
|
m_data = CreateSatBenchmark( (SatHullType)m_typeA, (SatHullType)m_typeB );
|
|
|
|
|
m_resetAverage = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_resetAverage = ImGui::Checkbox( "Inscribed Sphere", &m_inscribedSphere ) || m_resetAverage;
|
|
|
|
|
m_resetAverage = ImGui::Checkbox( "Warm Start GJK", &m_warmStart ) || m_resetAverage;
|
|
|
|
|
ImGui::SliderInt( "Pair", &m_pairIndex, 0, SAT_PAIR_COUNT - 1 );
|
|
|
|
|
|
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Render() override
|
|
|
|
|
{
|
|
|
|
|
b3Transform transformB = m_data->transforms[m_pairIndex];
|
|
|
|
|
b3WorldTransform worldA = b3MakeWorldTransform( b3Transform_identity );
|
|
|
|
|
b3WorldTransform worldB = b3MakeWorldTransform( transformB );
|
|
|
|
|
|
|
|
|
|
DrawHull( worldA, m_data->hullA, MakeColor( b3_colorGreen ) );
|
|
|
|
|
DrawHull( worldB, m_data->hullB, MakeColor( b3_colorCyan ) );
|
|
|
|
|
|
|
|
|
|
b3DistanceInput input = {};
|
|
|
|
|
input.proxyA = { b3GetHullPoints( m_data->hullA ), m_data->hullA->vertexCount, 0.0f };
|
|
|
|
|
input.proxyB = { b3GetHullPoints( m_data->hullB ), m_data->hullB->vertexCount, 0.0f };
|
|
|
|
|
input.transform = transformB;
|
|
|
|
|
input.useRadii = false;
|
|
|
|
|
|
|
|
|
|
b3SimplexCache cache = {};
|
|
|
|
|
b3DistanceOutput output = b3ShapeDistance( &input, &cache, nullptr, 0 );
|
|
|
|
|
|
|
|
|
|
b3Pos pA = b3TransformWorldPoint( worldA, output.pointA );
|
|
|
|
|
b3Pos pB = b3TransformWorldPoint( worldA, output.pointB );
|
|
|
|
|
DrawPoint( pA, 8.0f, MakeColor( b3_colorRed ) );
|
|
|
|
|
DrawPoint( pB, 8.0f, MakeColor( b3_colorYellow ) );
|
|
|
|
|
DrawLine( pA, pB, MakeColor( b3_colorWhite ) );
|
|
|
|
|
|
|
|
|
|
Sample::Render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
EnableSatInscribedSphere( m_data, m_inscribedSphere );
|
|
|
|
|
|
|
|
|
|
int repeatCount = m_isDebug ? 1 : 10;
|
|
|
|
|
SatBenchmarkResult result = RunSatBenchmark( m_data, repeatCount, m_warmStart );
|
|
|
|
|
|
|
|
|
|
float scale = 1000.0f / result.queryCount;
|
|
|
|
|
float distanceTime = scale * result.distanceMs;
|
|
|
|
|
float satTime = scale * result.satMs;
|
|
|
|
|
|
|
|
|
|
if ( m_resetAverage )
|
|
|
|
|
{
|
|
|
|
|
m_distanceTime = distanceTime;
|
|
|
|
|
m_satTime = satTime;
|
|
|
|
|
m_resetAverage = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
float alpha = 0.05f;
|
|
|
|
|
m_distanceTime = b3LerpFloat( m_distanceTime, distanceTime, alpha );
|
|
|
|
|
m_satTime = b3LerpFloat( m_satTime, satTime, alpha );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "pairs = %d, queries per frame = %d", SAT_PAIR_COUNT, result.queryCount );
|
|
|
|
|
DrawTextLine( "GJK (us) = %.3f, iterations = %.2f", m_distanceTime, result.averageIterations );
|
|
|
|
|
DrawTextLine( "SAT (us) = %.3f", m_satTime );
|
|
|
|
|
DrawTextLine( "SAT / GJK = %.2f", m_satTime / m_distanceTime );
|
|
|
|
|
DrawTextLine( "target dist = %.4f, avg dist = %.4f, avg sep = %.4f", m_data->targetDistance,
|
|
|
|
|
result.averageDistance, result.averageSeparation );
|
|
|
|
|
|
|
|
|
|
if ( result.earlyReturnCount > 0 )
|
|
|
|
|
{
|
|
|
|
|
DrawTextLine( "SAT early returns = %d", result.earlyReturnCount );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sample::Step();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BenchmarkSeparatingAxis( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SatBenchmarkData* m_data;
|
|
|
|
|
int m_typeA = satHull_complex;
|
|
|
|
|
int m_typeB = satHull_complex;
|
|
|
|
|
int m_pairIndex = 0;
|
|
|
|
|
bool m_inscribedSphere = true;
|
|
|
|
|
bool m_warmStart = false;
|
|
|
|
|
bool m_resetAverage = true;
|
|
|
|
|
float m_distanceTime = 0.0f;
|
|
|
|
|
float m_satTime = 0.0f;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int sampleSeparatingAxis = RegisterSample( "Benchmark", "SAT vs GJK", BenchmarkSeparatingAxis::Create );
|
|
|
|
|
|
|
|
|
|
#endif
|