2026-05-10 13:36:52 -07:00
|
|
|
// SPDX-FileCopyrightText: 2025 Erin Catto
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
#include "gfx/draw.h"
|
2026-05-10 13:36:52 -07:00
|
|
|
#include "human.h"
|
|
|
|
|
#include "imgui.h"
|
|
|
|
|
#include "mesh_loader.h"
|
|
|
|
|
#include "sample.h"
|
2026-06-06 10:06:05 -07:00
|
|
|
#include "utils.h"
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
#include "box3d/box3d.h"
|
|
|
|
|
|
|
|
|
|
class InclinedPlane : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit InclinedPlane( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( -55.0f, 30.0f, 60.0f, { 0.0f, 7.5f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 50.0f );
|
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
|
|
|
bodyDef.position = { 0.0f, 7.5f, -5.0f };
|
|
|
|
|
bodyDef.rotation = b3MakeQuatFromAxisAngle( { 1.0f, 0.0f, 0.0f }, 40.0f * B3_DEG_TO_RAD );
|
|
|
|
|
b3BodyId planeBody = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3BoxHull planeBox = b3MakeBoxHull( 16.0f, 0.5f, 10.0f );
|
2026-06-06 10:06:05 -07:00
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
2026-05-10 13:36:52 -07:00
|
|
|
shapeDef.baseMaterial.friction = 1.0f;
|
|
|
|
|
b3CreateHullShape( planeBody, &shapeDef, &planeBox.base );
|
|
|
|
|
|
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
for ( int index = 0; index < m_boxCount; ++index )
|
|
|
|
|
{
|
2026-07-18 22:43:03 -07:00
|
|
|
bodyDef.position = { -10.0f + 5.0f * index, 14.25f, -10.6f };
|
2026-05-10 13:36:52 -07:00
|
|
|
b3BodyId boxBody = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
shapeDef.baseMaterial.friction = ( index + 1 ) * ( index + 1 ) * 0.04f;
|
|
|
|
|
b3CreateHullShape( boxBody, &shapeDef, &box.base );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new InclinedPlane( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr int m_boxCount = 5;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleInclinedPlane = RegisterSample( "Shapes", "Inclined Plane", InclinedPlane::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class RollingResistance : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit RollingResistance( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( m_context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( -140.0f, 17.0f, 60.0f, { 0.0f, 7.5f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 22:05:11 -07:00
|
|
|
AddGroundBox( 80.0f );
|
2026-06-06 10:06:05 -07:00
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
|
|
|
|
|
bodyDef.position = { 0.0f, 2.0f, -20.0f };
|
|
|
|
|
bodyDef.rotation = b3MakeQuatFromAxisAngle( { 1.0f, 0.0f, 0.0f }, 10.0f * B3_DEG_TO_RAD );
|
|
|
|
|
b3BodyId planeBody = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
b3BoxHull plane = b3MakeBoxHull( 32.0f, 0.5f, 15.0f );
|
|
|
|
|
b3CreateHullShape( planeBody, &shapeDef, &plane.base );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
b3Sphere sphere = { b3Vec3_zero, 1.0f };
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
for ( int index = 0; index < m_count; ++index )
|
|
|
|
|
{
|
|
|
|
|
bodyDef.position = { -25.0f + 5.0f * index, 8.0f, -24.0f };
|
|
|
|
|
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
shapeDef.baseMaterial.rollingResistance = 0.05f * index;
|
|
|
|
|
b3CreateSphereShape( body, &shapeDef, &sphere );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3Capsule capsule = { { -1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f }, 0.5f };
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
for ( int index = 0; index < m_count; ++index )
|
|
|
|
|
{
|
|
|
|
|
bodyDef.position = { 2.0f + 5.0f * index, 8.0f, -24.0f };
|
|
|
|
|
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
shapeDef.baseMaterial.rollingResistance = 0.05f * index;
|
|
|
|
|
b3CreateCapsuleShape( body, &shapeDef, &capsule );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr int m_count = 5;
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new RollingResistance( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleRollingResistance = RegisterSample( "Shapes", "Rolling Resistance", RollingResistance::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class HighResistance : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit HighResistance( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( m_context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 5.0f, 40.0f, { 0.0f, 7.5f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 50.0f );
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
|
|
|
|
|
b3Capsule capsule = { { 0.0f, -1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 0.5f };
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, B3_DEG_TO_RAD * 30.0f );
|
|
|
|
|
for ( int index = 0; index < m_count; ++index )
|
|
|
|
|
{
|
|
|
|
|
bodyDef.position = { -22.0f + 5.0f * index, 1.5f, 0.0f };
|
|
|
|
|
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
shapeDef.baseMaterial.rollingResistance = 0.2f * index;
|
|
|
|
|
b3CreateCapsuleShape( body, &shapeDef, &capsule );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new HighResistance( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr int m_count = 10;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleHighResistance = RegisterSample( "Shapes", "High Resistance", HighResistance::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class IsotropicFriction : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit IsotropicFriction( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
2026-06-18 20:51:16 -07:00
|
|
|
m_camera->SetView( 45.0f, 30.0f, 150.0f, b3Pos_zero );
|
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
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
2026-05-10 13:36:52 -07:00
|
|
|
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
2026-06-06 10:06:05 -07:00
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
2026-05-10 13:36:52 -07:00
|
|
|
shapeDef.baseMaterial.friction = 0.6f;
|
|
|
|
|
for ( int index = 0; index < m_boxCount; ++index )
|
|
|
|
|
{
|
|
|
|
|
float alpha = B3_PI / 16.0f * index;
|
|
|
|
|
b3CosSin cs = b3ComputeCosSin( alpha );
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Pos position = { 15.0f * cs.cosine, 1.0f, 15.0f * cs.sine };
|
2026-05-10 13:36:52 -07:00
|
|
|
b3Quat orientation = b3MakeQuatFromAxisAngle( { 0.0f, 1.0f, 0.0f }, -alpha );
|
|
|
|
|
b3Vec3 velocity = { 25.0f * cs.cosine, 0.0f, 25.0f * cs.sine };
|
|
|
|
|
|
|
|
|
|
bodyDef.position = position;
|
|
|
|
|
bodyDef.rotation = orientation;
|
|
|
|
|
bodyDef.linearVelocity = velocity;
|
|
|
|
|
b3BodyId boxBody = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3CreateHullShape( boxBody, &shapeDef, &box.base );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new IsotropicFriction( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr int m_boxCount = 32;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleIsotropicFriction = RegisterSample( "Shapes", "Isotropic Friction", IsotropicFriction::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
// todo what is the point of this?
|
|
|
|
|
class SlideTwist : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit SlideTwist( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_camera->SetView( -30.0f, 17.0f, 30.0f, { 0.0f, 5.0f, 0.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 50.0f );
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
b3Quat orientation = b3MakeQuatFromAxisAngle( b3Vec3_axisX, 20.0f * B3_DEG_TO_RAD );
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.baseMaterial.friction = 1.0f;
|
|
|
|
|
|
|
|
|
|
bodyDef.position = { 0.0f, 4.0f, 0.0f };
|
|
|
|
|
bodyDef.rotation = orientation;
|
|
|
|
|
b3BodyId planeBody = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
b3BoxHull plane = b3MakeBoxHull( 10.0f, 0.5f, 10.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
shapeDef.baseMaterial.friction = 0.6f;
|
2026-06-06 10:06:05 -07:00
|
|
|
b3CreateHullShape( planeBody, &shapeDef, &plane.base );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.position = { 0.0f, 5.0f, 0.0f };
|
|
|
|
|
bodyDef.rotation = orientation;
|
|
|
|
|
bodyDef.angularVelocity = 25.0f * b3RotateVector( orientation, b3Vec3_axisY );
|
|
|
|
|
b3BodyId boxBody = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3BoxHull mBox = b3MakeBoxHull( 1.0f, 0.5f, 1.0f );
|
|
|
|
|
shapeDef.baseMaterial.friction = 0.3f;
|
|
|
|
|
b3CreateHullShape( boxBody, &shapeDef, &mBox.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new SlideTwist( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleSlideTwist = RegisterSample( "Shapes", "Slide Twist", SlideTwist::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class Restitution : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
enum ShapeType
|
|
|
|
|
{
|
|
|
|
|
e_sphereShape = 0,
|
|
|
|
|
e_boxShape
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
explicit Restitution( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 25.0f, 85.0f, { 0.0f, 20.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 50.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
m_shapeType = e_sphereShape;
|
|
|
|
|
|
|
|
|
|
CreateBodies();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateBodies()
|
|
|
|
|
{
|
|
|
|
|
for ( int i = 0; i < m_count; ++i )
|
|
|
|
|
{
|
|
|
|
|
if ( B3_IS_NON_NULL( m_bodyIds[i] ) )
|
|
|
|
|
{
|
|
|
|
|
b3DestroyBody( m_bodyIds[i] );
|
|
|
|
|
m_bodyIds[i] = b3_nullBodyId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3Sphere sphere = { b3Vec3_zero, 0.5f };
|
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
|
|
|
|
|
float dr = 1.0f / ( m_count > 1 ? m_count - 1 : 1 );
|
|
|
|
|
float x = -1.0f * ( m_count - 1 );
|
|
|
|
|
float dx = 2.0f;
|
|
|
|
|
|
|
|
|
|
for ( int i = 0; i < m_count; ++i )
|
|
|
|
|
{
|
|
|
|
|
bodyDef.position = { x, 40.0f, 0.0f };
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
m_bodyIds[i] = bodyId;
|
|
|
|
|
|
|
|
|
|
if ( m_shapeType == e_sphereShape )
|
|
|
|
|
{
|
|
|
|
|
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shapeDef.baseMaterial.restitution += dr;
|
|
|
|
|
x += dx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
bool DrawControls() override
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
if ( ImGui::RadioButton( "Sphere", m_shapeType == e_sphereShape ) )
|
|
|
|
|
{
|
|
|
|
|
m_shapeType = e_sphereShape;
|
|
|
|
|
CreateBodies();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ImGui::RadioButton( "Box", m_shapeType == e_boxShape ) )
|
|
|
|
|
{
|
|
|
|
|
m_shapeType = e_boxShape;
|
|
|
|
|
CreateBodies();
|
|
|
|
|
}
|
|
|
|
|
|
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 Restitution( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr int m_count = 40;
|
|
|
|
|
|
|
|
|
|
b3BodyId m_bodyIds[m_count] = {};
|
|
|
|
|
ShapeType m_shapeType;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleRestitution = RegisterSample( "Shapes", "Restitution", Restitution::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
// This shows an optimization when creating many static shapes you can skip having them invoke collision, assuming
|
|
|
|
|
// dynamic bodies are added after the static bodies.
|
|
|
|
|
class StaticInvoke : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit StaticInvoke( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 25.0f, 10.0f, { 0.0f, 1.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 20.0f );
|
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
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.position = { 0.25f, 1.0f, 0.0f };
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
2026-05-10 13:36:52 -07:00
|
|
|
shapeDef.baseMaterial.rollingResistance = 0.2f;
|
|
|
|
|
b3Sphere sphere = { b3Vec3_zero, 0.5f };
|
|
|
|
|
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
|
|
|
|
|
|
|
|
|
m_invoke = false;
|
|
|
|
|
m_bodyId = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateStatic()
|
|
|
|
|
{
|
|
|
|
|
if ( B3_IS_NON_NULL( m_bodyId ) )
|
|
|
|
|
{
|
|
|
|
|
b3DestroyBody( m_bodyId );
|
|
|
|
|
m_bodyId = b3_nullBodyId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.position = { 0.0f, 0.5f, 0.0f };
|
|
|
|
|
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
b3Sphere sphere = { b3Vec3_zero, 0.5f };
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.invokeContactCreation = m_invoke;
|
|
|
|
|
|
|
|
|
|
b3CreateSphereShape( m_bodyId, &shapeDef, &sphere );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
bool DrawControls() override
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
if ( ImGui::RadioButton( "Invoke", m_invoke == true ) )
|
|
|
|
|
{
|
|
|
|
|
m_invoke = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ImGui::RadioButton( "Passive", m_invoke == false ) )
|
|
|
|
|
{
|
|
|
|
|
m_invoke = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( B3_IS_NULL( m_bodyId ) )
|
|
|
|
|
{
|
|
|
|
|
if ( ImGui::Button( "Create" ) )
|
|
|
|
|
{
|
|
|
|
|
CreateStatic();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ( ImGui::Button( "Destroy" ) )
|
|
|
|
|
{
|
|
|
|
|
b3DestroyBody( m_bodyId );
|
|
|
|
|
m_bodyId = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
return true;
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Step();
|
|
|
|
|
|
|
|
|
|
if ( m_stepCount == 20 )
|
|
|
|
|
{
|
|
|
|
|
CreateStatic();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new StaticInvoke( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3BodyId m_bodyId;
|
|
|
|
|
bool m_invoke;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleStaticInvoke = RegisterSample( "Shapes", "Static Invoke", StaticInvoke::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class ConveyorBelt : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit ConveyorBelt( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 25.0f, 40.0f, { 0.0f, 1.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 20.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
// Platform
|
|
|
|
|
{
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.position = { -5.0f, 5.0f, 0.0f };
|
|
|
|
|
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisY, 0.2f );
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 10.0f, 0.25f, 2.0f );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.baseMaterial.friction = 0.8f;
|
|
|
|
|
shapeDef.baseMaterial.tangentVelocity = { 2.0f, 0.0f, 0.0f };
|
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Boxes
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
b3BoxHull cube = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
|
|
|
|
for ( int i = 0; i < 5; ++i )
|
|
|
|
|
{
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.position = { -10.0f + 2.0f * i, 7.0f, 0.0f };
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, &cube.base );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new ConveyorBelt( context );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleConveyorBelt = RegisterSample( "Shapes", "Conveyor Belt", ConveyorBelt::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class ConveyorMesh : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit ConveyorMesh( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 65.0f, 25.0f, 28.0f, { 0.0f, 1.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 20.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
// Mesh
|
2026-07-22 15:55:02 -07:00
|
|
|
m_meshData = CreateMeshData( "data/meshes/conveyor.obj", 1.0f, false, true, true, true );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-07-22 15:55:02 -07:00
|
|
|
if ( m_meshData != nullptr )
|
|
|
|
|
{
|
2026-05-10 13:36:52 -07:00
|
|
|
int triangleCount = m_meshData->triangleCount;
|
|
|
|
|
|
|
|
|
|
uint8_t* materialIndices = (uint8_t*)( (intptr_t)m_meshData + m_meshData->materialOffset );
|
|
|
|
|
|
|
|
|
|
memset( materialIndices, 0, triangleCount * sizeof( uint8_t ) );
|
|
|
|
|
m_velocities[0] = { 0.0f, 0.0f, 0.0f };
|
|
|
|
|
|
|
|
|
|
// +x -z
|
|
|
|
|
materialIndices[0] = 1;
|
|
|
|
|
materialIndices[4] = 1;
|
|
|
|
|
m_velocities[1] = { 0.7f, 0.0f, -0.2f };
|
|
|
|
|
|
|
|
|
|
// +x +z
|
|
|
|
|
materialIndices[9] = 2;
|
|
|
|
|
materialIndices[12] = 2;
|
|
|
|
|
m_velocities[2] = { 0.6f, 0.0f, 0.4f };
|
|
|
|
|
|
|
|
|
|
// +z
|
|
|
|
|
materialIndices[21] = 3;
|
|
|
|
|
materialIndices[38] = 3;
|
|
|
|
|
m_velocities[3] = { 0.0f, 0.0f, 1.3f };
|
|
|
|
|
|
|
|
|
|
// -x +z
|
|
|
|
|
materialIndices[43] = 4;
|
|
|
|
|
materialIndices[46] = 4;
|
|
|
|
|
m_velocities[4] = { -0.6f, 0.0f, 0.4f };
|
|
|
|
|
|
|
|
|
|
// -x -z
|
|
|
|
|
materialIndices[30] = 5;
|
|
|
|
|
materialIndices[33] = 5;
|
|
|
|
|
m_velocities[5] = { -0.75f, 0.0f, -0.4f };
|
|
|
|
|
|
|
|
|
|
// -z
|
|
|
|
|
materialIndices[18] = 6;
|
|
|
|
|
materialIndices[24] = 6;
|
|
|
|
|
m_velocities[6] = { 0.0f, 0.0f, -1.3f };
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
b3HexColor colors[7] = {
|
|
|
|
|
b3_colorGreen, b3_colorGreenYellow, b3_colorHoneyDew, b3_colorHotPink,
|
|
|
|
|
b3_colorIndianRed, b3_colorIndigo, b3_colorIvory,
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
b3SurfaceMaterial materials[7];
|
|
|
|
|
for ( int i = 0; i < 7; ++i )
|
|
|
|
|
{
|
|
|
|
|
materials[i] = b3DefaultSurfaceMaterial();
|
|
|
|
|
materials[i].friction = 0.8f;
|
|
|
|
|
materials[i].tangentVelocity = 2.0f * m_velocities[i];
|
2026-06-06 10:06:05 -07:00
|
|
|
materials[i].customColor = colors[i];
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_meshTransform.p = { 0.0f, 0.5f, 6.0f };
|
|
|
|
|
m_meshTransform.q = b3MakeQuatFromAxisAngle( b3Vec3_axisY, 0.5f * B3_PI );
|
|
|
|
|
// m_meshTransform.q = b3Quat_identity;
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.position = m_meshTransform.p;
|
|
|
|
|
bodyDef.rotation = m_meshTransform.q;
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.materials = materials;
|
|
|
|
|
shapeDef.materialCount = 7;
|
|
|
|
|
b3CreateMeshShape( bodyId, &shapeDef, m_meshData, b3Vec3_one );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// High number of sides to stress the collision code
|
|
|
|
|
// Normally the number of sides should be 16 or less.
|
|
|
|
|
m_cylinderHull = b3CreateCylinder( 0.3f, 0.15f, 0.0f, 32 );
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
|
|
|
|
|
// Cylinders
|
|
|
|
|
for ( int i = 0; i < 20; ++i )
|
|
|
|
|
{
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.position = { -8.5f + 0.9f * i, 1.5f, -5.5f };
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, m_cylinderHull );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
// Boxes
|
|
|
|
|
b3BoxHull cube = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
|
|
|
|
for ( int i = 0; i < 5; ++i )
|
|
|
|
|
{
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.position = { -8.5f + 3.0f * i, 2.0f, -7.0f };
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3CreateHullShape( bodyId, &shapeDef, &cube.base );
|
|
|
|
|
}
|
|
|
|
|
b3Sphere sphere = { b3Vec3_zero, 0.25f };
|
|
|
|
|
shapeDef.baseMaterial.rollingResistance = 0.1f;
|
|
|
|
|
for ( int i = 0; i < 10; ++i )
|
|
|
|
|
{
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.position = { -8.5f + 2.0f * i, 2.0f, -6.0f };
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
for ( int i = 0; i < 10; ++i )
|
|
|
|
|
{
|
|
|
|
|
Human human = {};
|
|
|
|
|
CreateHuman( &human, m_worldId, { -8.0f + 2.0f * i, 3.0f, -5.5f }, 10.0f, 1.0f, 1.0f, 0, nullptr, true );
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~ConveyorMesh() override
|
|
|
|
|
{
|
2026-07-22 15:55:02 -07:00
|
|
|
DestroyMeshData( m_meshData );
|
2026-05-10 13:36:52 -07:00
|
|
|
b3DestroyHull( m_cylinderHull );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Render() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Render();
|
|
|
|
|
|
2026-07-22 15:55:02 -07:00
|
|
|
if ( m_meshData == nullptr )
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
int triangleCount = m_meshData->triangleCount;
|
|
|
|
|
const b3MeshTriangle* triangles = b3GetMeshTriangles( m_meshData );
|
|
|
|
|
const b3Vec3* vertices = b3GetMeshVertices( m_meshData );
|
|
|
|
|
const uint8_t* materialIndices = b3GetMeshMaterialIndices( m_meshData );
|
|
|
|
|
|
|
|
|
|
for ( int i = 0; i < triangleCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
const b3MeshTriangle* t = triangles + i;
|
|
|
|
|
b3Vec3 v1 = vertices[t->index1];
|
|
|
|
|
b3Vec3 v2 = vertices[t->index2];
|
|
|
|
|
b3Vec3 v3 = vertices[t->index3];
|
|
|
|
|
|
|
|
|
|
b3Vec3 n = b3Cross( v2 - v1, v3 - v1 );
|
|
|
|
|
n = b3Normalize( n );
|
|
|
|
|
|
|
|
|
|
if ( n.y < 0.9f )
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Pos p = b3TransformWorldPoint( m_meshTransform, 1.0f / 3.0f * ( v1 + v2 + v3 ) );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-06-18 20:51:16 -07:00
|
|
|
DrawString3D( p, MakeColor( b3_colorAqua ), "%d", i );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
int materialIndex = materialIndices[i];
|
|
|
|
|
|
|
|
|
|
b3Vec3 v = b3RotateVector( m_meshTransform.q, m_velocities[materialIndex] );
|
2026-06-06 10:06:05 -07:00
|
|
|
DrawLine( p, p + v, MakeColor( b3_colorBlueViolet ) );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-06-18 20:51:16 -07:00
|
|
|
DrawAxes( b3WorldTransform_identity, 0.5f );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new ConveyorMesh( context );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 20:51:16 -07:00
|
|
|
b3WorldTransform m_meshTransform;
|
2026-05-10 13:36:52 -07:00
|
|
|
b3MeshData* m_meshData;
|
2026-06-11 22:39:26 -07:00
|
|
|
b3HullData* m_cylinderHull;
|
2026-05-10 13:36:52 -07:00
|
|
|
b3Vec3 m_velocities[7];
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleConveyorMesh = RegisterSample( "Shapes", "Conveyor Mesh", ConveyorMesh::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class Wind : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
enum ShapeType
|
|
|
|
|
{
|
|
|
|
|
e_sphereShape = 0,
|
|
|
|
|
e_capsuleShape,
|
|
|
|
|
e_boxShape
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
explicit Wind( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
, m_bodyIds{}
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 0.0f, 0.0f, 5.0f, { 0.0f, 1.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 20.0f );
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
{
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
m_groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
m_shapeType = e_boxShape;
|
|
|
|
|
m_wind = { 6.0f, 0.0f, 0.0f };
|
2026-05-10 13:36:52 -07:00
|
|
|
m_drag = 1.0f;
|
|
|
|
|
m_lift = 0.75f;
|
|
|
|
|
m_count = 10;
|
2026-06-06 10:06:05 -07:00
|
|
|
m_noise = { 0.0f, 0.0f, 0.0f };
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
// Need this to be false for debug draw to work
|
|
|
|
|
m_stepWhilePaused = false;
|
|
|
|
|
|
|
|
|
|
CreateScene();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateScene()
|
|
|
|
|
{
|
|
|
|
|
for ( int i = 0; i < m_maxCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
if ( B3_IS_NON_NULL( m_bodyIds[i] ) )
|
|
|
|
|
{
|
|
|
|
|
b3DestroyBody( m_bodyIds[i] );
|
|
|
|
|
m_bodyIds[i] = b3_nullBodyId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float radius = 0.1f;
|
2026-06-06 10:06:05 -07:00
|
|
|
float verticalOffset = 2.0f;
|
|
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, radius };
|
2026-06-06 10:06:05 -07:00
|
|
|
b3Capsule capsule = { { -radius, 0.0f, 0.0f }, { radius, 0.0f, 0.0f }, 0.5f * radius };
|
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 1.25f * radius, 0.75f * radius, 0.125f * radius );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
b3SphericalJointDef jointDef = b3DefaultSphericalJointDef();
|
|
|
|
|
jointDef.base.bodyIdA = m_groundId;
|
2026-06-06 10:06:05 -07:00
|
|
|
jointDef.base.localFrameA.p = { 0.0f, verticalOffset, 0.0f };
|
2026-05-10 13:36:52 -07:00
|
|
|
jointDef.base.drawScale = 0.1f;
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.density = 20.0f;
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.gravityScale = 0.5f;
|
|
|
|
|
bodyDef.enableSleep = false;
|
|
|
|
|
|
|
|
|
|
for ( int i = 0; i < m_count; ++i )
|
|
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
bodyDef.position = { ( 2.0f * i + 1.0f ) * radius, verticalOffset, 0.0f };
|
2026-05-10 13:36:52 -07:00
|
|
|
m_bodyIds[i] = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
if ( m_shapeType == e_sphereShape )
|
|
|
|
|
{
|
|
|
|
|
b3CreateSphereShape( m_bodyIds[i], &shapeDef, &sphere );
|
|
|
|
|
}
|
|
|
|
|
else if ( m_shapeType == e_capsuleShape )
|
|
|
|
|
{
|
|
|
|
|
b3CreateCapsuleShape( m_bodyIds[i], &shapeDef, &capsule );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
b3CreateHullShape( m_bodyIds[i], &shapeDef, &box.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jointDef.base.bodyIdB = m_bodyIds[i];
|
2026-06-06 10:06:05 -07:00
|
|
|
jointDef.base.localFrameB.p = { -radius, 0.0f, 0.0f };
|
2026-05-10 13:36:52 -07:00
|
|
|
b3CreateSphericalJoint( m_worldId, &jointDef );
|
|
|
|
|
|
|
|
|
|
jointDef.base.bodyIdA = m_bodyIds[i];
|
2026-06-06 10:06:05 -07:00
|
|
|
jointDef.base.localFrameA.p = { radius, 0.0f, 0.0f };
|
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
|
|
|
{
|
|
|
|
|
const char* shapeTypes[] = { "Circle", "Capsule", "Box" };
|
|
|
|
|
int shapeType = int( m_shapeType );
|
|
|
|
|
if ( ImGui::Combo( "Shape", &shapeType, shapeTypes, IM_ARRAYSIZE( shapeTypes ) ) )
|
|
|
|
|
{
|
|
|
|
|
m_shapeType = ShapeType( shapeType );
|
|
|
|
|
CreateScene();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
ImGui::SliderFloat( "Wind", &m_wind.x, -50.0f, 50.0f, "%.1f" );
|
2026-05-10 13:36:52 -07:00
|
|
|
ImGui::SliderFloat( "Drag", &m_drag, 0.0f, 1.0f, "%.2f" );
|
|
|
|
|
ImGui::SliderFloat( "Lift", &m_lift, 0.0f, 4.0f, "%.2f" );
|
|
|
|
|
if ( ImGui::SliderInt( "Count", &m_count, 1, m_maxCount, "%d" ) )
|
|
|
|
|
{
|
|
|
|
|
CreateScene();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
return true;
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
bool shouldStep = m_context->pause == false || m_context->singleStep > 0;
|
|
|
|
|
Sample::Step();
|
|
|
|
|
|
|
|
|
|
if ( shouldStep == true )
|
|
|
|
|
{
|
|
|
|
|
float speed;
|
|
|
|
|
b3Vec3 direction = b3GetLengthAndNormalize( &speed, m_wind );
|
|
|
|
|
b3Vec3 wind = b3MulSV( speed, b3Add( direction, m_noise ) );
|
|
|
|
|
|
|
|
|
|
for ( int i = 0; i < m_count; ++i )
|
|
|
|
|
{
|
|
|
|
|
b3ShapeId shapeIds[1];
|
|
|
|
|
int count = b3Body_GetShapes( m_bodyIds[i], shapeIds, 1 );
|
|
|
|
|
for ( int j = 0; j < count; ++j )
|
|
|
|
|
{
|
|
|
|
|
b3Shape_ApplyWind( shapeIds[j], wind, m_drag, m_lift, 10.0f, true );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3Vec3 rand = RandomVec3( { -0.3f, -0.3f, -0.3f }, { 0.3f, 0.3f, 0.3f } );
|
|
|
|
|
m_noise = b3Lerp( m_noise, rand, 0.05f );
|
|
|
|
|
|
2026-06-18 20:51:16 -07:00
|
|
|
b3Pos p1 = { 0.0f, 0.5f, 0.0f };
|
|
|
|
|
b3Pos p2 = p1 + b3MulSV( 0.2f, wind );
|
2026-06-06 10:06:05 -07:00
|
|
|
DrawArrow( p1, p2, MakeColor( b3_colorFuchsia ) );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new Wind( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr int m_maxCount = 60;
|
|
|
|
|
|
|
|
|
|
ShapeType m_shapeType;
|
|
|
|
|
b3Vec3 m_wind;
|
|
|
|
|
float m_drag;
|
|
|
|
|
float m_lift;
|
|
|
|
|
b3Vec3 m_noise;
|
|
|
|
|
b3BodyId m_groundId;
|
|
|
|
|
b3BodyId m_bodyIds[m_maxCount];
|
|
|
|
|
int m_count;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleWind = RegisterSample( "Shapes", "Wind", Wind::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class WindDrop : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit WindDrop( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_camera->SetView( -45.0f, 15.0f, 20.0f, { 0.0f, 5.0f, 0.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 15.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
m_drag = 1.0f;
|
|
|
|
|
m_lift = 4.0f;
|
|
|
|
|
|
|
|
|
|
// Need this to be false for debug draw to work
|
|
|
|
|
m_stepWhilePaused = false;
|
|
|
|
|
|
|
|
|
|
float radius = 0.1f;
|
|
|
|
|
// b3BoxHull box = b3MakeBoxHull( 0.25f * radius, 1.25f * radius, 0.25f * radius );
|
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 4.0f * radius, 0.1f * radius, 4.0f * radius );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.density = 2.0f;
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.linearVelocity = { 0.0f, 0.0f, 0.0f };
|
|
|
|
|
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisX, 0.25f );
|
|
|
|
|
bodyDef.gravityScale = 0.5f;
|
|
|
|
|
|
|
|
|
|
bodyDef.position = { 0.0f, 10.0f, 0.0f };
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
m_shapeId = b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
bool shouldStep = m_context->pause == false || m_context->singleStep > 0;
|
|
|
|
|
Sample::Step();
|
|
|
|
|
|
|
|
|
|
if ( shouldStep == true )
|
|
|
|
|
{
|
|
|
|
|
b3Shape_ApplyWind( m_shapeId, b3Vec3_zero, m_drag, m_lift, 10.0f, true );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new WindDrop( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float m_drag;
|
|
|
|
|
float m_lift;
|
|
|
|
|
b3ShapeId m_shapeId;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleWindDrop = RegisterSample( "Shapes", "Wind Drop", WindDrop::Create );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
class WindFlap : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit WindFlap( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
2026-06-06 10:06:05 -07:00
|
|
|
m_camera->SetView( -35.0f, 15.0f, 65.0f, { 0.0f, 5.0f, 10.0f } );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
AddGroundBox( 50.0f );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
m_drag = 1.0f;
|
|
|
|
|
m_lift = 2.0f;
|
|
|
|
|
|
|
|
|
|
// Need this to be false for debug draw to work
|
|
|
|
|
m_stepWhilePaused = false;
|
|
|
|
|
|
|
|
|
|
float a = 0.4f;
|
|
|
|
|
// b3BoxHull box = b3MakeBoxHull( 0.25f * radius, 1.25f * radius, 0.25f * radius );
|
|
|
|
|
b3Capsule capsule = { { 0.0f, 0.0f, -a }, { 0.0f, 0.0f, a }, 0.25f * a };
|
2026-06-06 10:06:05 -07:00
|
|
|
// b3BoxHull box = b3MakeBoxHull( 2.0f * a, 0.01f, a );
|
|
|
|
|
b3Transform wingTransform1 = { b3Vec3_zero, b3MakeQuatFromAxisAngle( b3Vec3_axisX, 0.1f ) };
|
2026-05-10 13:36:52 -07:00
|
|
|
b3BoxHull box1 = b3MakeTransformedBoxHull( 2.0f * a, 0.01f, a, wingTransform1 );
|
2026-06-06 10:06:05 -07:00
|
|
|
b3Transform wingTransform2 = { b3Vec3_zero, b3MakeQuatFromAxisAngle( b3Vec3_axisX, 0.1f ) };
|
2026-05-10 13:36:52 -07:00
|
|
|
b3BoxHull box2 = b3MakeTransformedBoxHull( 2.0f * a, 0.01f, a, wingTransform2 );
|
|
|
|
|
|
|
|
|
|
float y = 20.0f;
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
2026-06-06 10:06:05 -07:00
|
|
|
// bodyDef.gravityScale = 0.5f;
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
shapeDef.density = 5.0f;
|
|
|
|
|
bodyDef.position = { -2.0f * a, y, 0.0f };
|
|
|
|
|
b3BodyId wingBodyId1 = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
m_shapeId1 = b3CreateHullShape( wingBodyId1, &shapeDef, &box1.base );
|
|
|
|
|
|
|
|
|
|
bodyDef.position = { 2.0f * a, y, 0.0f };
|
|
|
|
|
b3BodyId wingBodyId2 = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
m_shapeId2 = b3CreateHullShape( wingBodyId2, &shapeDef, &box2.base );
|
|
|
|
|
|
|
|
|
|
bodyDef.position = { 0.0f, y, 0.0f };
|
2026-06-06 10:06:05 -07:00
|
|
|
// bodyDef.type = b3_staticBody;
|
2026-05-10 13:36:52 -07:00
|
|
|
b3BodyId torsoBodyId = b3CreateBody( m_worldId, &bodyDef );
|
2026-06-06 10:06:05 -07:00
|
|
|
|
2026-05-10 13:36:52 -07:00
|
|
|
shapeDef.density = 10.0f;
|
|
|
|
|
m_torsoShapeId = b3CreateCapsuleShape( torsoBodyId, &shapeDef, &capsule );
|
|
|
|
|
|
|
|
|
|
b3RevoluteJointDef jointDef = b3DefaultRevoluteJointDef();
|
|
|
|
|
jointDef.base.drawScale = 0.1f;
|
|
|
|
|
jointDef.base.bodyIdA = torsoBodyId;
|
|
|
|
|
jointDef.base.localFrameA.p = { 0.0f, 0.0f, 0.0f };
|
|
|
|
|
jointDef.base.bodyIdB = wingBodyId1;
|
|
|
|
|
jointDef.base.localFrameB.p = { 2.0f * a, 0.0f, 0.0f };
|
|
|
|
|
jointDef.enableSpring = true;
|
|
|
|
|
jointDef.hertz = 6.0f;
|
|
|
|
|
jointDef.dampingRatio = 0.5f;
|
|
|
|
|
jointDef.enableLimit = true;
|
|
|
|
|
jointDef.lowerAngle = -30.0f * B3_PI / 180.0f;
|
|
|
|
|
jointDef.upperAngle = 30.0f * B3_PI / 180.0f;
|
|
|
|
|
m_jointId1 = b3CreateRevoluteJoint( m_worldId, &jointDef );
|
|
|
|
|
|
|
|
|
|
jointDef.base.bodyIdB = wingBodyId2;
|
|
|
|
|
jointDef.base.localFrameB.p = { -2.0f * a, 0.0f, 0.0f };
|
|
|
|
|
m_jointId2 = b3CreateRevoluteJoint( m_worldId, &jointDef );
|
|
|
|
|
|
|
|
|
|
b3FilterJointDef filterDef = b3DefaultFilterJointDef();
|
|
|
|
|
filterDef.base.bodyIdA = wingBodyId1;
|
|
|
|
|
filterDef.base.bodyIdB = wingBodyId2;
|
|
|
|
|
b3CreateFilterJoint( m_worldId, &filterDef );
|
|
|
|
|
|
|
|
|
|
m_time = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
bool shouldStep = m_context->pause == false || m_context->singleStep > 0;
|
|
|
|
|
Sample::Step();
|
|
|
|
|
|
|
|
|
|
if ( shouldStep == true )
|
|
|
|
|
{
|
|
|
|
|
float maxSpeed = 10.0f;
|
|
|
|
|
bool wake = false;
|
|
|
|
|
b3Shape_ApplyWind( m_shapeId1, b3Vec3_zero, m_drag, m_lift, maxSpeed, wake );
|
|
|
|
|
b3Shape_ApplyWind( m_shapeId2, b3Vec3_zero, m_drag, m_lift, maxSpeed, wake );
|
2026-06-06 10:06:05 -07:00
|
|
|
// b3Shape_ApplyWind( m_torsoShapeId, b3Vec3_zero, m_drag, m_lift, maxSpeed, wake );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
float angle = b3Sin( 10.0f * m_time );
|
2026-06-06 10:06:05 -07:00
|
|
|
b3RevoluteJoint_SetTargetAngle( m_jointId1, angle );
|
|
|
|
|
b3RevoluteJoint_SetTargetAngle( m_jointId2, -angle );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
m_time += m_context->hertz > 0.0f ? 1.0f / m_context->hertz : 0.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new WindFlap( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float m_time;
|
|
|
|
|
float m_drag;
|
|
|
|
|
float m_lift;
|
|
|
|
|
b3ShapeId m_shapeId1;
|
|
|
|
|
b3ShapeId m_shapeId2;
|
|
|
|
|
b3ShapeId m_torsoShapeId;
|
|
|
|
|
b3JointId m_jointId1;
|
|
|
|
|
b3JointId m_jointId2;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 10:06:05 -07:00
|
|
|
static int sampleWindFlap = RegisterSample( "Shapes", "Wind Flap", WindFlap::Create );
|