2026-05-10 13:36:52 -07:00
|
|
|
// SPDX-FileCopyrightText: 2025 Erin Catto
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
2026-09-20 15:54:52 -07:00
|
|
|
#include "gfx/debug_adapter.h"
|
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
|
|
|
|
2026-09-20 15:54:52 -07:00
|
|
|
|
|
|
|
|
// Similar to the MeasureSupportedBounce unit test
|
|
|
|
|
class SphereStackRestitution : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr float m_impactSpeed = 5.0f;
|
|
|
|
|
static constexpr int m_maxStackCount = 3;
|
|
|
|
|
|
|
|
|
|
explicit SphereStackRestitution( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 20.0f, 10.0f, 12.0f, { 0.0f, 2.5f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.position = { 0.0f, -1.0f, 0.0f };
|
|
|
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.baseMaterial.friction = 0.0f;
|
|
|
|
|
shapeDef.baseMaterial.restitution = 0.0f;
|
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 40.0f, 1.0f, 40.0f );
|
|
|
|
|
b3ShapeId groundShapeId = b3CreateHullShape( groundId, &shapeDef, &box.base );
|
|
|
|
|
SetGroundShape( groundShapeId );
|
|
|
|
|
|
|
|
|
|
CreateScene();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3BodyId CreateBall( float y, float velocityY, float restitution, bool hitEvents )
|
|
|
|
|
{
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.position = { 0.0f, y, 0.0f };
|
|
|
|
|
bodyDef.linearVelocity = { 0.0f, velocityY, 0.0f };
|
|
|
|
|
bodyDef.enableSleep = false;
|
|
|
|
|
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.baseMaterial.friction = 0.0f;
|
|
|
|
|
shapeDef.baseMaterial.restitution = restitution;
|
|
|
|
|
shapeDef.enableHitEvents = hitEvents;
|
|
|
|
|
b3Sphere sphere = { b3Vec3_zero, 0.5f };
|
|
|
|
|
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
|
|
|
|
|
|
|
|
|
return bodyId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateScene()
|
|
|
|
|
{
|
|
|
|
|
for ( int i = 0; i < m_bodyCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
b3DestroyBody( m_bodyIds[i] );
|
|
|
|
|
}
|
|
|
|
|
m_bodyCount = 0;
|
|
|
|
|
|
|
|
|
|
if ( m_gravity )
|
|
|
|
|
{
|
|
|
|
|
b3World_SetGravity( m_worldId, { 0.0f, -10.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
b3World_SetGravity( m_worldId, b3Vec3_zero );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ( int i = 0; i < m_stackCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
m_bodyIds[m_bodyCount] = CreateBall( 0.5f + 1.0f * i, 0.0f, 0.0f, false );
|
|
|
|
|
m_bodyCount += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start half a step of travel above contact so the impact lands mid step, matching the test
|
|
|
|
|
m_startHeight = 0.5f + 1.0f * m_stackCount + 0.5f * m_impactSpeed * ( 1.0f / 60.0f );
|
|
|
|
|
|
|
|
|
|
m_impactorId = CreateBall( m_startHeight, -m_impactSpeed, m_restitution, true );
|
|
|
|
|
m_bodyIds[m_bodyCount] = m_impactorId;
|
|
|
|
|
m_bodyCount += 1;
|
|
|
|
|
|
|
|
|
|
m_startEnergy = MeasureEnergy( m_worldId, m_bodyIds, m_bodyCount ).Total();
|
|
|
|
|
m_peakEnergy = m_startEnergy;
|
|
|
|
|
|
|
|
|
|
m_coefficient = 0.0f;
|
|
|
|
|
m_latched = false;
|
|
|
|
|
m_hit = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DrawControls() override
|
|
|
|
|
{
|
|
|
|
|
bool rebuild = false;
|
|
|
|
|
|
|
|
|
|
ImGui::PushItemWidth( 6.0f * ImGui::GetFontSize() );
|
|
|
|
|
|
|
|
|
|
if ( ImGui::SliderFloat( "Restitution", &m_restitution, 0.0f, 1.0f, "%.2f" ) )
|
|
|
|
|
{
|
|
|
|
|
rebuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ImGui::SliderInt( "Stack", &m_stackCount, 0, m_maxStackCount ) )
|
|
|
|
|
{
|
|
|
|
|
rebuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
|
|
|
|
|
if ( ImGui::Checkbox( "Gravity", &m_gravity ) )
|
|
|
|
|
{
|
|
|
|
|
rebuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ImGui::Button( "Reset" ) )
|
|
|
|
|
{
|
|
|
|
|
rebuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( rebuild )
|
|
|
|
|
{
|
|
|
|
|
CreateScene();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Step();
|
|
|
|
|
|
|
|
|
|
b3ContactEvents events = b3World_GetContactEvents( m_worldId );
|
|
|
|
|
if ( events.hitCount > 0 )
|
|
|
|
|
{
|
|
|
|
|
m_hit = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float vy = b3Body_GetLinearVelocity( m_impactorId ).y;
|
|
|
|
|
|
|
|
|
|
// The rebound only means something once the impact has happened and the impactor is leaving
|
|
|
|
|
if ( m_latched == false && m_hit && vy > 0.0f )
|
|
|
|
|
{
|
|
|
|
|
m_coefficient = vy / m_impactSpeed;
|
|
|
|
|
m_latched = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float supportSpeed = 0.0f;
|
|
|
|
|
for ( int i = 0; i < m_stackCount; ++i )
|
|
|
|
|
{
|
|
|
|
|
supportSpeed = b3MaxFloat( supportSpeed, b3Length( b3Body_GetLinearVelocity( m_bodyIds[i] ) ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DrawLine( { -2.0f, m_startHeight, 0.0f }, { 2.0f, m_startHeight, 0.0f }, MakeColor( b3_colorRed ) );
|
|
|
|
|
|
|
|
|
|
MechanicalEnergy energy = MeasureEnergy( m_worldId, m_bodyIds, m_bodyCount );
|
|
|
|
|
float total = energy.Total();
|
|
|
|
|
m_peakEnergy = b3MaxFloat( m_peakEnergy, total );
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "impactor vy = %.3f m/s", vy );
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "coefficient = %.4f live (target %.2f)", vy / m_impactSpeed, m_restitution );
|
|
|
|
|
|
|
|
|
|
if ( m_latched )
|
|
|
|
|
{
|
|
|
|
|
DrawTextLine( "coefficient = %.4f at first rebound", m_coefficient );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "support speed = %.4f m/s", supportSpeed );
|
|
|
|
|
|
|
|
|
|
float scale = m_startEnergy != 0.0f ? 100.0f / m_startEnergy : 0.0f;
|
|
|
|
|
DrawTextLine( "kinetic = %.3f J linear + %.3f J angular", energy.linear, energy.angular );
|
|
|
|
|
DrawTextLine( "potential = %.3f J", energy.potential );
|
|
|
|
|
DrawTextLine( "total = %.3f J (%.2f%% of start, peak %.2f%%)", total, scale * total, scale * m_peakEnergy );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new SphereStackRestitution( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3BodyId m_bodyIds[m_maxStackCount + 1] = {};
|
|
|
|
|
b3BodyId m_impactorId = b3_nullBodyId;
|
|
|
|
|
int m_bodyCount = 0;
|
|
|
|
|
int m_stackCount = 2;
|
|
|
|
|
float m_restitution = 1.0f;
|
|
|
|
|
float m_startHeight = 0.0f;
|
|
|
|
|
float m_coefficient = 0.0f;
|
|
|
|
|
float m_startEnergy = 0.0f;
|
|
|
|
|
float m_peakEnergy = 0.0f;
|
|
|
|
|
bool m_gravity = false;
|
|
|
|
|
bool m_latched = false;
|
|
|
|
|
bool m_hit = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int sampleSphereStackRestitution = RegisterSample( "Shapes", "Sphere Stack Restitution", SphereStackRestitution::Create );
|
|
|
|
|
|
|
|
|
|
// Similar to the MeasureFlatBounce and SpinTest unit tests
|
|
|
|
|
class BoxRestitution : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr float m_impactSpeed = 5.0f;
|
|
|
|
|
|
|
|
|
|
explicit BoxRestitution( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 20.0f, 10.0f, 14.0f, { 0.0f, 3.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Gravity would bias the measured coefficient
|
|
|
|
|
b3World_SetGravity( m_worldId, b3Vec3_zero );
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.position = { 0.0f, -1.0f, 0.0f };
|
|
|
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.baseMaterial.friction = 0.0f;
|
|
|
|
|
shapeDef.baseMaterial.restitution = 0.0f;
|
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 40.0f, 1.0f, 40.0f );
|
|
|
|
|
b3ShapeId groundShapeId = b3CreateHullShape( groundId, &shapeDef, &box.base );
|
|
|
|
|
SetGroundShape( groundShapeId );
|
|
|
|
|
|
|
|
|
|
CreateScene();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateScene()
|
|
|
|
|
{
|
|
|
|
|
if ( B3_IS_NON_NULL( m_boxId ) )
|
|
|
|
|
{
|
|
|
|
|
b3DestroyBody( m_boxId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start half a step of travel above contact so the impact lands mid step, matching the test
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.position = { 0.0f, 0.25f + 0.5f * m_impactSpeed * ( 1.0f / 60.0f ), 0.0f };
|
|
|
|
|
bodyDef.linearVelocity = { 0.0f, -m_impactSpeed, 0.0f };
|
|
|
|
|
bodyDef.angularVelocity = { 0.0f, 0.0f, m_spin };
|
|
|
|
|
bodyDef.enableSleep = false;
|
|
|
|
|
m_boxId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.density = 1.0f;
|
|
|
|
|
shapeDef.baseMaterial.friction = 0.0f;
|
|
|
|
|
shapeDef.baseMaterial.restitution = m_restitution;
|
|
|
|
|
shapeDef.enableHitEvents = true;
|
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 1.0f, 0.25f, 1.0f );
|
|
|
|
|
b3CreateHullShape( m_boxId, &shapeDef, &box.base );
|
|
|
|
|
|
|
|
|
|
m_startEnergy = MeasureEnergy( m_worldId, &m_boxId, 1 ).Total();
|
|
|
|
|
m_peakEnergy = m_startEnergy;
|
|
|
|
|
|
|
|
|
|
m_coefficient = 0.0f;
|
|
|
|
|
m_latched = false;
|
|
|
|
|
m_hit = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DrawControls() override
|
|
|
|
|
{
|
|
|
|
|
bool rebuild = false;
|
|
|
|
|
|
|
|
|
|
ImGui::PushItemWidth( 6.0f * ImGui::GetFontSize() );
|
|
|
|
|
|
|
|
|
|
if ( ImGui::SliderFloat( "Restitution", &m_restitution, 0.0f, 1.0f, "%.2f" ) )
|
|
|
|
|
{
|
|
|
|
|
rebuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ImGui::SliderFloat( "Spin", &m_spin, 0.0f, 2.0f, "%.2f" ) )
|
|
|
|
|
{
|
|
|
|
|
rebuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
|
|
|
|
|
if ( ImGui::Button( "Reset" ) )
|
|
|
|
|
{
|
|
|
|
|
rebuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( rebuild )
|
|
|
|
|
{
|
|
|
|
|
CreateScene();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Step();
|
|
|
|
|
|
|
|
|
|
b3ContactEvents events = b3World_GetContactEvents( m_worldId );
|
|
|
|
|
if ( events.hitCount > 0 )
|
|
|
|
|
{
|
|
|
|
|
m_hit = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float vy = b3Body_GetLinearVelocity( m_boxId ).y;
|
|
|
|
|
b3Vec3 w = b3Body_GetAngularVelocity( m_boxId );
|
|
|
|
|
|
|
|
|
|
if ( m_latched == false && m_hit && vy > 0.0f )
|
|
|
|
|
{
|
|
|
|
|
m_coefficient = vy / m_impactSpeed;
|
|
|
|
|
m_latched = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vec4 yellow = MakeColor( b3_colorYellow );
|
|
|
|
|
DrawPoint( b3Body_GetWorldPoint( m_boxId, { -1.0f, -0.25f, -1.0f } ), 8.0f, yellow );
|
|
|
|
|
DrawPoint( b3Body_GetWorldPoint( m_boxId, { 1.0f, -0.25f, -1.0f } ), 8.0f, yellow );
|
|
|
|
|
DrawPoint( b3Body_GetWorldPoint( m_boxId, { -1.0f, -0.25f, 1.0f } ), 8.0f, yellow );
|
|
|
|
|
DrawPoint( b3Body_GetWorldPoint( m_boxId, { 1.0f, -0.25f, 1.0f } ), 8.0f, yellow );
|
|
|
|
|
|
|
|
|
|
MechanicalEnergy energy = MeasureEnergy( m_worldId, &m_boxId, 1 );
|
|
|
|
|
float total = energy.Total();
|
|
|
|
|
m_peakEnergy = b3MaxFloat( m_peakEnergy, total );
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "vy = %.3f m/s", vy );
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "coefficient = %.4f live (target %.2f)", vy / m_impactSpeed, m_restitution );
|
|
|
|
|
|
|
|
|
|
if ( m_latched )
|
|
|
|
|
{
|
|
|
|
|
DrawTextLine( "coefficient = %.4f at first rebound", m_coefficient );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The spin is about z, so any x or y component is residual from the sequential point solve
|
|
|
|
|
DrawTextLine( "spin in = %.2f, spin out = %.4f (ideal %.2f at e = 1)", m_spin, w.z, -m_spin );
|
|
|
|
|
DrawTextLine( "off axis spin = %.4f rad/s", sqrtf( w.x * w.x + w.y * w.y ) );
|
|
|
|
|
|
|
|
|
|
float scale = m_startEnergy != 0.0f ? 100.0f / m_startEnergy : 0.0f;
|
|
|
|
|
DrawTextLine( "kinetic = %.3f J linear + %.3f J angular", energy.linear, energy.angular );
|
|
|
|
|
DrawTextLine( "potential = %.3f J", energy.potential );
|
|
|
|
|
DrawTextLine( "total = %.3f J (%.2f%% of start, peak %.2f%%)", total, scale * total, scale * m_peakEnergy );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BoxRestitution( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3BodyId m_boxId = b3_nullBodyId;
|
|
|
|
|
float m_restitution = 0.9f;
|
|
|
|
|
float m_spin = 0.0f;
|
|
|
|
|
float m_coefficient = 0.0f;
|
|
|
|
|
float m_startEnergy = 0.0f;
|
|
|
|
|
float m_peakEnergy = 0.0f;
|
|
|
|
|
bool m_latched = false;
|
|
|
|
|
bool m_hit = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int sampleBoxRestitution = RegisterSample( "Shapes", "Box Restitution", BoxRestitution::Create );
|
|
|
|
|
|
|
|
|
|
// Similar to the ImpulseTest unit test. The hit event marks the bounce and carries the approach speed,
|
|
|
|
|
// so the impulse the contact reports can be checked against reversing that speed at the coefficient.
|
|
|
|
|
// The total runs a little over because the contact also carries the weight for the part of the step
|
|
|
|
|
// it is active.
|
|
|
|
|
class BounceImpulse : public Sample
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit BounceImpulse( SampleContext* context )
|
|
|
|
|
: Sample( context )
|
|
|
|
|
{
|
|
|
|
|
if ( context->restart == false )
|
|
|
|
|
{
|
|
|
|
|
m_camera->SetView( 20.0f, 10.0f, 36.0f, { 0.0f, 8.0f, 0.0f } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3World_SetGravity( m_worldId, { 0.0f, -10.0f, 0.0f } );
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.position = { 0.0f, -1.0f, 0.0f };
|
|
|
|
|
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.baseMaterial.friction = 0.0f;
|
|
|
|
|
shapeDef.baseMaterial.restitution = 0.0f;
|
|
|
|
|
b3BoxHull box = b3MakeBoxHull( 40.0f, 1.0f, 40.0f );
|
|
|
|
|
b3ShapeId groundShapeId = b3CreateHullShape( groundId, &shapeDef, &box.base );
|
|
|
|
|
SetGroundShape( groundShapeId );
|
|
|
|
|
|
|
|
|
|
CreateScene();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateScene()
|
|
|
|
|
{
|
|
|
|
|
if ( B3_IS_NON_NULL( m_ballId ) )
|
|
|
|
|
{
|
|
|
|
|
b3DestroyBody( m_ballId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3BodyDef bodyDef = b3DefaultBodyDef();
|
|
|
|
|
bodyDef.type = b3_dynamicBody;
|
|
|
|
|
bodyDef.position = { 0.0f, 0.5f + m_dropHeight, 0.0f };
|
|
|
|
|
bodyDef.enableSleep = false;
|
|
|
|
|
m_ballId = b3CreateBody( m_worldId, &bodyDef );
|
|
|
|
|
|
|
|
|
|
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
|
|
|
|
shapeDef.baseMaterial.friction = 0.0f;
|
|
|
|
|
shapeDef.baseMaterial.restitution = m_restitution;
|
|
|
|
|
shapeDef.enableHitEvents = true;
|
|
|
|
|
b3Sphere sphere = { b3Vec3_zero, 0.5f };
|
|
|
|
|
b3CreateSphereShape( m_ballId, &shapeDef, &sphere );
|
|
|
|
|
|
|
|
|
|
m_mass = b3Body_GetMass( m_ballId );
|
|
|
|
|
|
|
|
|
|
m_approachSpeed = 0.0f;
|
|
|
|
|
m_totalImpulse = 0.0f;
|
|
|
|
|
m_expectedImpulse = 0.0f;
|
|
|
|
|
m_contactSteps = 0;
|
|
|
|
|
m_hit = false;
|
|
|
|
|
m_bouncing = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DrawControls() override
|
|
|
|
|
{
|
|
|
|
|
bool rebuild = false;
|
|
|
|
|
|
|
|
|
|
ImGui::PushItemWidth( 6.0f * ImGui::GetFontSize() );
|
|
|
|
|
|
|
|
|
|
if ( ImGui::SliderFloat( "Restitution", &m_restitution, 0.0f, 1.0f, "%.2f" ) )
|
|
|
|
|
{
|
|
|
|
|
rebuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ImGui::SliderFloat( "Height", &m_dropHeight, 0.5f, 50.0f, "%.1f" ) )
|
|
|
|
|
{
|
|
|
|
|
rebuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
|
|
|
|
|
if ( ImGui::Button( "Reset" ) )
|
|
|
|
|
{
|
|
|
|
|
rebuild = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( rebuild )
|
|
|
|
|
{
|
|
|
|
|
CreateScene();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Step() override
|
|
|
|
|
{
|
|
|
|
|
Sample::Step();
|
|
|
|
|
|
|
|
|
|
if ( m_didStep )
|
|
|
|
|
{
|
|
|
|
|
b3ContactEvents events = b3World_GetContactEvents( m_worldId );
|
|
|
|
|
if ( m_hit == false && events.hitCount > 0 )
|
|
|
|
|
{
|
|
|
|
|
m_hit = true;
|
|
|
|
|
m_bouncing = true;
|
|
|
|
|
m_approachSpeed = events.hitEvents[0].approachSpeed;
|
|
|
|
|
m_expectedImpulse = ( 1.0f + m_restitution ) * m_mass * m_approachSpeed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Keep summing until the contact lets go, a slow bounce can take a few steps to leave
|
|
|
|
|
if ( m_bouncing )
|
|
|
|
|
{
|
|
|
|
|
b3ContactData contactData[4];
|
|
|
|
|
int contactCount = b3Body_GetContactData( m_ballId, contactData, 4 );
|
|
|
|
|
for ( int c = 0; c < contactCount; ++c )
|
|
|
|
|
{
|
|
|
|
|
for ( int m = 0; m < contactData[c].manifoldCount; ++m )
|
|
|
|
|
{
|
|
|
|
|
const b3Manifold* manifold = contactData[c].manifolds + m;
|
|
|
|
|
for ( int p = 0; p < manifold->pointCount; ++p )
|
|
|
|
|
{
|
|
|
|
|
m_totalImpulse += manifold->points[p].totalNormalImpulse;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( contactCount > 0 )
|
|
|
|
|
{
|
|
|
|
|
m_contactSteps += 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_bouncing = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float startHeight = 0.5f + m_dropHeight;
|
|
|
|
|
DrawLine( { -2.0f, startHeight, 0.0f }, { 2.0f, startHeight, 0.0f }, MakeColor( b3_colorRed ) );
|
|
|
|
|
|
|
|
|
|
DrawTextLine( "vy = %.3f m/s", b3Body_GetLinearVelocity( m_ballId ).y );
|
|
|
|
|
|
|
|
|
|
if ( m_hit )
|
|
|
|
|
{
|
|
|
|
|
DrawTextLine( "approach speed = %.3f m/s", m_approachSpeed );
|
|
|
|
|
DrawTextLine( "total impulse = %.2f N s over %d steps", m_totalImpulse, m_contactSteps );
|
|
|
|
|
DrawTextLine( "expected impulse = %.2f N s", m_expectedImpulse );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DrawTextLine( "waiting for the hit" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Sample* Create( SampleContext* context )
|
|
|
|
|
{
|
|
|
|
|
return new BounceImpulse( context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b3BodyId m_ballId = b3_nullBodyId;
|
|
|
|
|
float m_restitution = 0.5f;
|
|
|
|
|
float m_dropHeight = 20.0f;
|
|
|
|
|
float m_mass = 0.0f;
|
|
|
|
|
float m_approachSpeed = 0.0f;
|
|
|
|
|
float m_totalImpulse = 0.0f;
|
|
|
|
|
float m_expectedImpulse = 0.0f;
|
|
|
|
|
int m_contactSteps = 0;
|
|
|
|
|
bool m_hit = false;
|
|
|
|
|
bool m_bouncing = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int sampleBounceImpulse = RegisterSample( "Shapes", "Bounce Impulse", BounceImpulse::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 );
|