box3d/samples/sample_bodies.cpp

1335 lines
38 KiB
C++
Raw Normal View History

2026-05-10 13:36:52 -07:00
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT
#include "gfx/draw.h"
#include "gfx/keycodes.h"
2026-05-10 13:36:52 -07:00
#include "imgui.h"
#include "sample.h"
2026-05-10 13:36:52 -07:00
#include "box3d/box3d.h"
class BodyType : public Sample
{
public:
explicit BodyType( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 0.0f, 30.0f, 30.0f, { 0.0f, 1.5f, 0.0f } );
}
m_type = b3_dynamicBody;
m_isEnabled = true;
b3BodyId groundId = AddGroundBox( 20.0f );
2026-05-10 13:36:52 -07:00
// Define attachment
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = { -2.0f, 3.0f, 0.0f };
bodyDef.name = "attach1";
m_attachmentId = b3CreateBody( m_worldId, &bodyDef );
b3BoxHull box = b3MakeBoxHull( 0.5f, 2.0f, 0.5f );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 1.0f;
b3CreateHullShape( m_attachmentId, &shapeDef, &box.base );
}
// Define second attachment
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = m_type;
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = { 3.0f, 3.0f };
bodyDef.name = "attach2";
m_secondAttachmentId = b3CreateBody( m_worldId, &bodyDef );
b3BoxHull box = b3MakeBoxHull( 0.5f, 2.0f, 0.5f );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 1.0f;
b3CreateHullShape( m_secondAttachmentId, &shapeDef, &box.base );
}
// Define platform
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = m_type;
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = { -4.0f, 5.0f };
bodyDef.name = "platform";
m_platformId = b3CreateBody( m_worldId, &bodyDef );
b3BoxHull box = b3MakeTransformedBoxHull(
0.5f, 4.0f, 0.5f, { { 4.0f, 0.0f, 0.0f }, b3MakeQuatFromAxisAngle( b3Vec3_axisZ, 0.5f * B3_PI ) } );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 2.0f;
b3CreateHullShape( m_platformId, &shapeDef, &box.base );
b3RevoluteJointDef revoluteDef = b3DefaultRevoluteJointDef();
b3Pos pivot = { -2.0f, 5.0f, 0.0f };
2026-05-10 13:36:52 -07:00
revoluteDef.base.bodyIdA = m_attachmentId;
revoluteDef.base.bodyIdB = m_platformId;
revoluteDef.base.localFrameA.p = b3Body_GetLocalPoint( m_attachmentId, pivot );
revoluteDef.base.localFrameB.p = b3Body_GetLocalPoint( m_platformId, pivot );
revoluteDef.maxMotorTorque = 50.0f;
revoluteDef.enableMotor = true;
b3CreateRevoluteJoint( m_worldId, &revoluteDef );
pivot = { 3.0f, 5.0f };
revoluteDef.base.bodyIdA = m_secondAttachmentId;
revoluteDef.base.bodyIdB = m_platformId;
revoluteDef.base.localFrameA.p = b3Body_GetLocalPoint( m_secondAttachmentId, pivot );
revoluteDef.base.localFrameB.p = b3Body_GetLocalPoint( m_platformId, pivot );
revoluteDef.maxMotorTorque = 50.0f;
revoluteDef.enableMotor = true;
b3CreateRevoluteJoint( m_worldId, &revoluteDef );
b3PrismaticJointDef prismaticDef = b3DefaultPrismaticJointDef();
b3Pos anchor = { 0.0f, 5.0f, 0.0f };
2026-05-10 13:36:52 -07:00
prismaticDef.base.bodyIdA = groundId;
prismaticDef.base.bodyIdB = m_platformId;
prismaticDef.base.localFrameA.p = b3Body_GetLocalPoint( groundId, anchor );
prismaticDef.base.localFrameB.p = b3Body_GetLocalPoint( m_platformId, anchor );
prismaticDef.maxMotorForce = 1000.0f;
prismaticDef.motorSpeed = 0.0f;
prismaticDef.enableMotor = true;
prismaticDef.lowerTranslation = -10.0f;
prismaticDef.upperTranslation = 10.0f;
prismaticDef.enableLimit = true;
b3CreatePrismaticJoint( m_worldId, &prismaticDef );
m_speed = 3.0f;
}
// Create a payload
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = { -3.0f, 8.0f };
bodyDef.name = "crate1";
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3BoxHull box = b3MakeBoxHull( 0.75f, 0.75f, 0.75f );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 2.0f;
b3CreateHullShape( bodyId, &shapeDef, &box.base );
}
// Create a second payload
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = m_type;
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = { 2.0f, 8.0f };
bodyDef.name = "crate2";
m_secondPayloadId = b3CreateBody( m_worldId, &bodyDef );
b3BoxHull box = b3MakeBoxHull( 0.75f, 0.75f, 0.75f );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 2.0f;
b3CreateHullShape( m_secondPayloadId, &shapeDef, &box.base );
}
// Create a separate body on the ground
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = m_type;
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = { 8.0f, 0.2f };
bodyDef.name = "debris";
m_touchingBodyId = b3CreateBody( m_worldId, &bodyDef );
b3Capsule capsule = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, 0.25f };
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 2.0f;
b3CreateCapsuleShape( m_touchingBodyId, &shapeDef, &capsule );
}
// Create a separate floating body
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = m_type;
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = { -8.0f, 12.0f };
bodyDef.gravityScale = 0.0f;
bodyDef.name = "floater";
m_floatingBodyId = b3CreateBody( m_worldId, &bodyDef );
b3Sphere sphere = { { 0.0f, 0.5f, 0.0f }, 0.25f };
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 2.0f;
b3CreateSphereShape( m_floatingBodyId, &shapeDef, &sphere );
}
}
bool DrawControls() override
2026-05-10 13:36:52 -07:00
{
if ( ImGui::RadioButton( "Static", m_type == b3_staticBody ) )
{
m_type = b3_staticBody;
b3Body_SetType( m_platformId, b3_staticBody );
b3Body_SetType( m_secondAttachmentId, b3_staticBody );
b3Body_SetType( m_secondPayloadId, b3_staticBody );
b3Body_SetType( m_touchingBodyId, b3_staticBody );
b3Body_SetType( m_floatingBodyId, b3_staticBody );
}
if ( ImGui::RadioButton( "Kinematic", m_type == b3_kinematicBody ) )
{
m_type = b3_kinematicBody;
b3Body_SetType( m_platformId, b3_kinematicBody );
b3Body_SetLinearVelocity( m_platformId, { -m_speed, 0.0f } );
b3Body_SetAngularVelocity( m_platformId, b3Vec3_zero );
b3Body_SetType( m_secondAttachmentId, b3_kinematicBody );
b3Body_SetLinearVelocity( m_secondAttachmentId, b3Vec3_zero );
b3Body_SetAngularVelocity( m_secondAttachmentId, b3Vec3_zero );
b3Body_SetType( m_secondPayloadId, b3_kinematicBody );
b3Body_SetType( m_touchingBodyId, b3_kinematicBody );
b3Body_SetType( m_floatingBodyId, b3_kinematicBody );
}
if ( ImGui::RadioButton( "Dynamic", m_type == b3_dynamicBody ) )
{
m_type = b3_dynamicBody;
b3Body_SetType( m_platformId, b3_dynamicBody );
b3Body_SetType( m_secondAttachmentId, b3_dynamicBody );
b3Body_SetType( m_secondPayloadId, b3_dynamicBody );
b3Body_SetType( m_touchingBodyId, b3_dynamicBody );
b3Body_SetType( m_floatingBodyId, b3_dynamicBody );
}
if ( ImGui::Checkbox( "Enable", &m_isEnabled ) )
{
if ( m_isEnabled )
{
b3Body_Enable( m_attachmentId );
b3Body_Enable( m_secondPayloadId );
b3Body_Enable( m_floatingBodyId );
}
else
{
b3Body_Disable( m_attachmentId );
b3Body_Disable( m_secondPayloadId );
b3Body_Disable( m_floatingBodyId );
}
}
return true;
2026-05-10 13:36:52 -07:00
}
void Step() override
{
// Drive the kinematic body.
if ( m_type == b3_kinematicBody )
{
b3Pos p = b3Body_GetPosition( m_platformId );
2026-05-10 13:36:52 -07:00
b3Vec3 v = b3Body_GetLinearVelocity( m_platformId );
if ( ( p.x < -14.0f && v.x < 0.0f ) || ( p.x > 6.0f && v.x > 0.0f ) )
{
v.x = -v.x;
b3Body_SetLinearVelocity( m_platformId, v );
}
}
Sample::Step();
}
static Sample* Create( SampleContext* context )
{
return new BodyType( context );
}
b3BodyId m_attachmentId;
b3BodyId m_secondAttachmentId;
b3BodyId m_platformId;
b3BodyId m_secondPayloadId;
b3BodyId m_touchingBodyId;
b3BodyId m_floatingBodyId;
b3BodyType m_type;
float m_speed;
bool m_isEnabled;
};
static int sampleBodyType = RegisterSample( "Bodies", "Body Type", BodyType::Create );
2026-05-10 13:36:52 -07:00
class SpinningBooks : public Sample
{
public:
explicit SpinningBooks( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 0.0f, 30.0f, 10.0f, { 0.0f, 1.0f, 0.0f } );
}
AddGroundBox( 10.0f );
2026-05-10 13:36:52 -07:00
b3BoxHull box = b3MakeBoxHull( 0.35f, 0.08f, 0.5f );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.gravityScale = 0.0f;
b3ShapeDef shapeDef = b3DefaultShapeDef();
bodyDef.position = { -2.0f, 2.0f, 0.0f };
bodyDef.angularVelocity = { 5.0f, 0.01f, 0.01f };
b3BodyId body1 = b3CreateBody( m_worldId, &bodyDef );
b3CreateHullShape( body1, &shapeDef, &box.base );
bodyDef.position = { 0.0f, 2.0f, 0.0f };
bodyDef.angularVelocity = { 0.01f, 5.0f, 0.01f };
b3BodyId body2 = b3CreateBody( m_worldId, &bodyDef );
b3CreateHullShape( body2, &shapeDef, &box.base );
bodyDef.position = { 2.0f, 2.0f, 0.0f };
bodyDef.angularVelocity = { 0.01f, 0.01f, -5.0f };
b3BodyId body3 = b3CreateBody( m_worldId, &bodyDef );
b3CreateHullShape( body3, &shapeDef, &box.base );
}
static Sample* Create( SampleContext* context )
{
return new SpinningBooks( context );
}
};
static int sampleBook = RegisterSample( "Bodies", "Spinning Book", SpinningBooks::Create );
2026-05-10 13:36:52 -07:00
// Dzhanibekov effect
class GyroscopicTorque : public Sample
{
public:
explicit GyroscopicTorque( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 0.0f, 20.0f, 4.0f, { 0.0f, 2.0f, 0.0f } );
}
AddGroundBox( 20.0f );
2026-05-10 13:36:52 -07:00
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = { 0.0f, 2.0f, 0.0f };
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisX, -0.5f * B3_PI );
bodyDef.gravityScale = 0.0f;
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
2026-05-10 13:36:52 -07:00
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.updateBodyMass = false;
b3HullData* cylinder = b3CreateCylinder( 0.6f, 0.15f, 0.0f, 32 );
2026-05-10 13:36:52 -07:00
b3BoxHull box = b3MakeBoxHull( 1.0f, 0.05f, 0.1f );
b3CreateHullShape( m_bodyId, &shapeDef, cylinder );
b3CreateHullShape( m_bodyId, &shapeDef, &box.base );
b3Body_ApplyMassFromShapes( m_bodyId );
// Set the angular velocity after creating the shapes and the local center of mass is fixed.
b3Body_SetAngularVelocity( m_bodyId, { 0.01f, 0.01f, 10.0f } );
2026-05-10 13:36:52 -07:00
b3DestroyHull( cylinder );
}
void Step() override
{
Sample::Step();
b3Pos c = b3Body_GetWorldCenter( m_bodyId );
DrawTextLine( "center %.3g %.3g %.3g", c.x, c.y, c.z );
}
2026-05-10 13:36:52 -07:00
static Sample* Create( SampleContext* sampleContext )
{
return new GyroscopicTorque( sampleContext );
}
b3BodyId m_bodyId;
2026-05-10 13:36:52 -07:00
};
static int sampleGyroscopicTorque = RegisterSample( "Bodies", "Gyroscopic Torque", GyroscopicTorque::Create );
2026-05-10 13:36:52 -07:00
// Spinning tops. Ported from PEEL.
// Each top is tilted and spun about its symmetry axis. Gravity torque about the tip is what makes
// them precess instead of toppling. The first top carries a diagnostic that compares the measured
// precession rate against the classical heavy top solution.
class GyroscopicPrecession : public Sample
{
public:
explicit GyroscopicPrecession( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 40.0f, 30.0f, 75.0f, { 0.0f, 2.0f, 0.0f } );
}
AddGroundBox( 40.0f );
// Top shape: a wide n-gon rim up top and a point at the origin, so it balances on its tip.
constexpr int numSegs = 7;
constexpr float r = 2.0f;
constexpr float h = 2.0f;
b3Vec3 hullPoints[numSegs + 1];
const float dphi = 2.0f * B3_PI / numSegs;
for ( int i = 0; i < numSegs; ++i )
{
hullPoints[i] = { r * cosf( i * dphi ), h, r * sinf( i * dphi ) };
}
hullPoints[numSegs] = b3Vec3_zero;
b3HullData* hull = b3CreateHull( hullPoints, numSegs + 1, numSegs + 1 );
// Capsule for testing alternate shapes. Keep this.
//b3Capsule capsule = {{0.0f, -0.6f, 0.0f}, {0.0f, 0.6f, 0.0f }, 0.4f};
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.baseMaterial.rollingResistance = 0.1f;
// Tilt the top, then spin it about its own symmetry axis. Gravity does the rest.
b3Quat rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, 15.0f * B3_PI / 180.0f );
b3Vec3 angularVelocity = b3RotateVector( rotation, { 0.0f, 75.0f, 0.0f } );
constexpr int count = 8;
constexpr float separation = 6.0f;
for ( int x = 0; x < count; ++x )
{
for ( int z = 0; z < count; ++z )
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = { ( x - count / 2 ) * separation, h, ( z - count / 2 ) * separation };
bodyDef.rotation = rotation;
// The spin rate exceeds the default cap, so bypass it as the test intends.
bodyDef.allowFastRotation = true;
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateHullShape( bodyId, &shapeDef, hull );
//b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
b3Body_SetAngularVelocity( bodyId, angularVelocity );
if ( x == 0 && z == 0 )
{
m_topId = bodyId;
}
}
}
b3DestroyHull( hull );
// Mass properties of the measured top. The tip sits at the body origin, so the pivot distance is
// just the height of the center of mass, and the symmetry axis is the local up axis.
b3MassData massData = b3Body_GetMassData( m_topId );
m_mass = massData.mass;
m_pivotDistance = massData.center.y;
m_spinInertia = massData.inertia.cy.y;
// Transverse inertia belongs about the pivot, not the center of mass
float transverse = 0.5f * ( massData.inertia.cx.x + massData.inertia.cz.z );
m_transverseInertia = transverse + m_mass * m_pivotDistance * m_pivotDistance;
m_gravity = b3Length( b3World_GetGravity( m_worldId ) );
m_azimuth = 0.0f;
m_actualAngle = 0.0f;
m_expectedAngle = 0.0f;
m_elapsed = 0.0f;
m_groundTime = 0.0f;
m_rate = 0.0f;
m_measuring = false;
}
// Steady precession rate of a heavy symmetric top about the vertical. The slow root of
// I1 * W^2 * cos(tilt) - I3 * spin * W + M * g * d = 0. Goldstein 5.7.
// Collapses to torque over spin momentum for a fast top.
float ExpectedRate( float spin, float cosTilt ) const
{
float momentum = m_spinInertia * spin;
float torque = m_mass * m_gravity * m_pivotDistance;
if ( momentum <= FLT_EPSILON )
{
return 0.0f;
}
float a = m_transverseInertia * cosTilt;
float discriminant = momentum * momentum - 4.0f * a * torque;
if ( a <= FLT_EPSILON || discriminant < 0.0f )
{
// Axis at or past horizontal, or spinning too slowly to precess steadily
return torque / momentum;
}
return ( momentum - sqrtf( discriminant ) ) / ( 2.0f * a );
}
void Step() override
{
Sample::Step();
bool isAwake = b3Body_IsAwake( m_topId );
if ( isAwake == false )
{
DrawTextLine( "top is sleeping" );
return;
}
b3Pos tip = b3Body_GetPosition( m_topId );
b3Quat quat = b3Body_GetRotation( m_topId );
b3Vec3 axis = b3RotateVector( quat, b3Vec3_axisY );
b3Vec3 omega = b3Body_GetAngularVelocity( m_topId );
float spin = b3Dot( omega, axis );
float cosTilt = b3ClampFloat( axis.y, -1.0f, 1.0f );
float expected = ExpectedRate( spin, cosTilt );
if ( m_didStep )
{
float timeStep = 1.0f / m_context->hertz;
// Gravity exerts no torque about the center of mass while the top is airborne, so the pivot
// solution only applies once the tip lands. Give the landing impulse time to wash out too,
// otherwise it biases the average for the rest of the run.
if ( tip.y < 0.05f )
{
m_groundTime += timeStep;
}
if ( m_measuring == false )
{
if ( m_groundTime > 0.5f )
{
m_measuring = true;
m_azimuth = b3Atan2( -axis.z, axis.x );
}
}
else
{
// Right handed angle of the symmetry axis about the world up axis
float azimuth = b3Atan2( -axis.z, axis.x );
float delta = b3UnwindAngle( azimuth - m_azimuth );
m_azimuth = azimuth;
m_actualAngle += delta;
m_expectedAngle += expected * timeStep;
m_elapsed += timeStep;
// Nutation swings the instantaneous rate between zero and twice the mean, so filter it
// with a one second time constant.
float alpha = timeStep / ( timeStep + 1.0f );
m_rate += alpha * ( delta / timeStep - m_rate );
}
}
DrawLine( tip, b3OffsetPos( tip, 5.0f * axis ), MakeColor( b3_colorYellow ) );
DrawTextLine( "spin %.1f rad/s, tilt %.1f deg", spin, ( 180.0f / B3_PI ) * acosf( cosTilt ) );
DrawTextLine( "precession: expected %.4f rad/s, actual %.4f rad/s", expected, m_rate );
if ( m_elapsed > 0.0f )
{
// Average both sides over the same window. The spin decays, so the expected rate drifts with it.
float expectedAverage = m_expectedAngle / m_elapsed;
float actualAverage = m_actualAngle / m_elapsed;
float error = expectedAverage != 0.0f ? 100.0f * ( actualAverage - expectedAverage ) / expectedAverage : 0.0f;
DrawTextLine( "%.1f s average: expected %.4f, actual %.4f, error %+.1f%%", m_elapsed, expectedAverage, actualAverage,
error );
}
else
{
DrawTextLine( "waiting for the tip to land" );
}
}
static Sample* Create( SampleContext* context )
{
return new GyroscopicPrecession( context );
}
b3BodyId m_topId;
float m_mass;
float m_gravity;
float m_pivotDistance;
float m_spinInertia;
float m_transverseInertia;
float m_azimuth;
float m_actualAngle;
float m_expectedAngle;
float m_elapsed;
float m_groundTime;
float m_rate;
bool m_measuring;
};
static int sampleGyroscopicPrecession = RegisterSample( "Bodies", "Gyroscopic Precession", GyroscopicPrecession::Create );
2026-05-10 13:36:52 -07:00
class Weeble : public Sample
{
public:
explicit Weeble( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 25.0f, 25.0f, b3Pos_zero );
2026-05-10 13:36:52 -07:00
}
AddGroundBox( 30.0f );
2026-05-10 13:36:52 -07:00
b3BodyDef bodyDef = b3DefaultBodyDef();
2026-05-10 13:36:52 -07:00
bodyDef.type = b3_dynamicBody;
bodyDef.position = { 0.0f, 3.0f, 0.0f };
m_weebleId = b3CreateBody( m_worldId, &bodyDef );
b3Capsule capsule = { { 0.0f, -1.0f }, { 0.0f, 1.0f }, 1.0f };
b3ShapeDef shapeDef = b3DefaultShapeDef();
2026-05-10 13:36:52 -07:00
shapeDef.baseMaterial.rollingResistance = 0.1f;
b3CreateCapsuleShape( m_weebleId, &shapeDef, &capsule );
float mass = b3Body_GetMass( m_weebleId );
b3Matrix3 inertiaTensor = b3Body_GetLocalRotationalInertia( m_weebleId );
b3Vec3 offset = { 0.0f, -1.5f, 0.0f };
// See: https://en.wikipedia.org/wiki/Parallel_axis_theorem
inertiaTensor = b3AddMM( inertiaTensor, b3Steiner( mass, offset ) );
2026-05-10 13:36:52 -07:00
b3MassData massData = {
.mass = mass,
.center = offset,
.inertia = inertiaTensor,
};
b3Body_SetMassData( m_weebleId, massData );
m_explosionPosition = { 0.0f, -0.1f, 0.0f };
m_explosionRadius = 8.0f;
m_explosionMagnitude = 20000.0f;
2026-05-10 13:36:52 -07:00
}
bool DrawControls() override
2026-05-10 13:36:52 -07:00
{
if ( ImGui::Button( "Teleport" ) )
{
b3Body_SetTransform( m_weebleId, { 0.0f, 5.0f, 0.0f }, b3MakeQuatFromAxisAngle( b3Vec3_axisZ, 0.95f * B3_PI ) );
b3Body_SetAwake( m_weebleId, true );
}
if ( ImGui::Button( "Explode" ) )
{
b3ExplosionDef def = b3DefaultExplosionDef();
def.position = m_explosionPosition;
def.radius = m_explosionRadius;
def.falloff = 0.1f;
def.impulsePerArea = m_explosionMagnitude;
b3World_Explode( m_worldId, &def );
}
ImGui::PushItemWidth( 6.0f * ImGui::GetFontSize() );
2026-05-10 13:36:52 -07:00
ImGui::SliderFloat( "Magnitude", &m_explosionMagnitude, -100000.0f, 100000.0f, "%.0f" );
ImGui::PopItemWidth();
return true;
2026-05-10 13:36:52 -07:00
}
void Step() override
{
Sample::Step();
b3Sphere sphere = { b3Vec3_zero, m_explosionRadius };
DrawWireSphere( { m_explosionPosition, b3Quat_identity }, &sphere, 64, MakeColor( b3_colorAzure ) );
2026-05-10 13:36:52 -07:00
// This shows how to get the velocity of a point on a body
b3Vec3 localPoint = { 0.0f, 2.0f, 0.0f };
b3Pos worldPoint = b3Body_GetWorldPoint( m_weebleId, localPoint );
2026-05-10 13:36:52 -07:00
b3Vec3 v1 = b3Body_GetLocalPointVelocity( m_weebleId, localPoint );
b3Vec3 v2 = b3Body_GetWorldPointVelocity( m_weebleId, worldPoint );
b3Vec3 offset = { 0.05f, 0.0f };
DrawLine( worldPoint, worldPoint + v1, MakeColor( b3_colorRed ) );
DrawLine( worldPoint + offset, worldPoint + v2 + offset, MakeColor( b3_colorGreen ) );
}
static Sample* Create( SampleContext* context )
{
return new Weeble( context );
2026-05-10 13:36:52 -07:00
}
b3BodyId m_weebleId;
b3Pos m_explosionPosition;
2026-05-10 13:36:52 -07:00
float m_explosionRadius;
float m_explosionMagnitude;
};
static int sampleWeeble = RegisterSample( "Bodies", "Weeble", Weeble::Create );
2026-05-10 13:36:52 -07:00
class DisableBody : public Sample
{
public:
enum
{
e_count = 4
};
explicit DisableBody( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 45.0f, 25.0f, 10.0f, b3Pos_zero );
2026-05-10 13:36:52 -07:00
}
AddGroundBox( 20.0f );
2026-05-10 13:36:52 -07:00
b3BodyDef bodyDef = b3DefaultBodyDef();
2026-05-10 13:36:52 -07:00
b3ShapeDef shapeDef = b3DefaultShapeDef();
float linkRadius = 0.1f;
float linkLength = 5.0f * linkRadius;
b3Capsule capsule = { { 0.0f, 0.0f, 0.0f }, { 0.0f, -linkLength, 0.0f }, linkRadius };
2026-05-10 13:36:52 -07:00
b3BodyId parentId = {};
for ( int link = 0; link < e_count; ++link )
{
bodyDef.position = { 0.0f, ( float( e_count ) - link ) * linkLength + 1.0f, 0.0f };
2026-05-10 13:36:52 -07:00
bodyDef.type = B3_IS_NULL( parentId ) ? b3_kinematicBody : b3_dynamicBody;
b3BodyId childId = b3CreateBody( m_worldId, &bodyDef );
b3CreateCapsuleShape( childId, &shapeDef, &capsule );
m_bodyIds[link] = childId;
if ( B3_IS_NON_NULL( parentId ) )
{
b3WeldJointDef jointDef = b3DefaultWeldJointDef();
jointDef.base.bodyIdA = parentId;
jointDef.base.bodyIdB = childId;
jointDef.base.localFrameA.p = { 0.0f, -linkLength, 0.0f };
2026-05-10 13:36:52 -07:00
jointDef.angularHertz = 10.0f;
jointDef.angularDampingRatio = 1.0f;
b3CreateWeldJoint( m_worldId, &jointDef );
}
parentId = childId;
}
bodyDef.type = b3_dynamicBody;
bodyDef.position = { 3.0f, 3.0f, 0.0f };
m_ballId = b3CreateBody( m_worldId, &bodyDef );
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f };
b3CreateSphereShape( m_ballId, &shapeDef, &sphere );
}
bool DrawControls() override
2026-05-10 13:36:52 -07:00
{
{
bool enabled = b3Body_IsEnabled( m_bodyIds[2] );
if ( ImGui::Checkbox( "Enable Link", &enabled ) )
{
if ( enabled )
{
b3Body_Enable( m_bodyIds[2] );
}
else
{
b3Body_Disable( m_bodyIds[2] );
}
}
}
{
bool enabled = b3Body_IsEnabled( m_ballId );
if ( ImGui::Checkbox( "Enable Ball", &enabled ) )
{
if ( enabled )
{
b3Body_Enable( m_ballId );
}
else
{
b3Body_Disable( m_ballId );
}
}
}
return true;
2026-05-10 13:36:52 -07:00
}
void Step() override
{
b3Body_ApplyLinearImpulseToCenter( m_bodyIds[2], { 0.0f, 0.1f, 0.0f }, true );
Sample::Step();
}
static Sample* Create( SampleContext* context )
{
return new DisableBody( context );
}
b3BodyId m_bodyIds[e_count];
b3BodyId m_ballId;
};
static int sampleDisable = RegisterSample( "Bodies", "Disable", DisableBody::Create );
2026-05-10 13:36:52 -07:00
class BodyCast : public Sample
{
public:
explicit BodyCast( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 120.0f, 30.0f, 20.0f, { 0.0f, 1.5f, 0.0f } );
}
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_kinematicBody;
bodyDef.position = { 5.0f, 5.0f, 0.0f };
bodyDef.angularVelocity = { 0.1f, -0.1f, 0.1f };
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
m_cylinder = b3CreateCylinder( 2.0f, 0.5f, 0.0f, 16 );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateHullShape( m_bodyId, &shapeDef, m_cylinder );
m_transform.p = { -10.0f, 2.0f, 0.0f };
m_transform.q = b3MakeQuatFromAxisAngle( b3Normalize( { 1.0f, -2.0f, 3.0f } ), 0.75f * B3_PI );
m_baseTranslation = b3Pos_zero;
2026-05-10 13:36:52 -07:00
m_baseX = 0;
m_baseY = 0;
m_origin = b3Pos_zero;
2026-05-10 13:36:52 -07:00
m_tracking = false;
}
~BodyCast() override
{
b3DestroyHull( m_cylinder );
}
void MouseDown( b3Vec2 p, int button, int modifiers ) override
{
if ( button == 0 && modifiers == MOD_SHIFT )
2026-05-10 13:36:52 -07:00
{
PickRay pickRay = m_camera->BuildPickRay( p.x, p.y );
m_origin = pickRay.origin + 10.0f * b3Normalize( pickRay.translation );
m_baseTranslation = m_transform.p;
m_tracking = true;
}
else
{
m_tracking = false;
}
}
void MouseUp( b3Vec2 p, int button ) override
{
if ( button == 0 )
{
m_tracking = false;
}
}
2026-05-10 13:36:52 -07:00
void MouseMove( b3Vec2 p ) override
{
if ( m_tracking )
{
PickRay pickRay = m_camera->BuildPickRay( p.x, p.y );
b3Pos origin = pickRay.origin + 10.0f * b3Normalize( pickRay.translation );
m_transform.p = m_baseTranslation + b3SubPos( origin, m_origin );
2026-05-10 13:36:52 -07:00
}
}
void Render() override
{
Sample::Render();
DrawGroundGrid( 10 );
b3WorldTransform t = { { 0.0f, 0.1f, 0.0f }, b3Quat_identity };
DrawAxes( t, 4.0f );
2026-05-10 13:36:52 -07:00
DrawHull( m_transform, m_cylinder, MakeColor( b3_colorBlue ) );
2026-05-10 13:36:52 -07:00
}
void Step() override
{
// Cast ray
{
b3Pos origin = { -9.75f, 3.0f, -4.0f };
b3Vec3 translation = { 0.0f, 0.0f, 8.0f };
b3QueryFilter filter = b3DefaultQueryFilter();
float maxFraction = 1.0f;
b3BodyCastResult result = b3Body_CastRay( m_bodyId, origin, translation, filter, maxFraction, m_transform );
2026-05-10 13:36:52 -07:00
DrawLine( origin, origin + maxFraction * translation, MakeColor( b3_colorCyan ) );
2026-05-10 13:36:52 -07:00
if ( result.hit )
{
b3Pos hitPoint = result.point;
DrawLine( hitPoint, hitPoint + 0.2f * result.normal, MakeColor( b3_colorYellow ) );
DrawPoint( hitPoint, 10.0f, MakeColor( b3_colorYellow ) );
2026-05-10 13:36:52 -07:00
}
DrawPoint( origin, 10.0f, MakeColor( b3_colorGreen ) );
DrawPoint( origin + translation, 10.0f, MakeColor( b3_colorRed ) );
2026-05-10 13:36:52 -07:00
}
// Cast sphere
{
b3Pos origin = { -14.5f, 2.5f, 0.5f };
b3Sphere sphere = { b3Vec3_zero, 0.2f };
2026-05-10 13:36:52 -07:00
b3ShapeProxy proxy = { &sphere.center, 1, sphere.radius };
b3Vec3 translation = { 8.0f, 0.0f, 0.0f };
b3QueryFilter filter = b3DefaultQueryFilter();
float maxFraction = 1.0f;
bool canEncroach = true;
b3BodyCastResult result =
b3Body_CastShape( m_bodyId, origin, &proxy, translation, filter, maxFraction, canEncroach, m_transform );
2026-05-10 13:36:52 -07:00
b3Pos sphereCenter = b3OffsetPos( origin, sphere.center );
2026-05-10 13:36:52 -07:00
if ( result.hit )
{
b3WorldTransform t = { origin + result.fraction * translation, b3Quat_identity };
DrawSolidSphere( t, sphere, MakeColor( b3_colorGreen ) );
b3Pos hitPoint = result.point;
DrawLine( hitPoint, hitPoint + 0.2f * result.normal, MakeColor( b3_colorYellow ) );
2026-05-10 13:36:52 -07:00
}
else
{
b3WorldTransform t = { origin + maxFraction * translation, b3Quat_identity };
DrawSolidSphere( t, sphere, MakeColor( b3_colorWhite ) );
2026-05-10 13:36:52 -07:00
}
DrawLine( sphereCenter, sphereCenter + maxFraction * translation, MakeColor( b3_colorWhite ) );
DrawPoint( sphereCenter, 10.0f, MakeColor( b3_colorGreen ) );
DrawPoint( sphereCenter + maxFraction * translation, 10.0f, MakeColor( b3_colorRed ) );
2026-05-10 13:36:52 -07:00
}
// Overlap capsule
{
b3Pos origin = { -10.0f, 1.0f, 0.5f };
b3Capsule capsule = { { -.5f, 1.0f, 0.0f }, { 0.5f, 0.0f, 0.0f }, 0.5f };
2026-05-10 13:36:52 -07:00
b3ShapeProxy proxy = { &capsule.center1, 2, capsule.radius };
bool overlaps = b3Body_OverlapShape( m_bodyId, origin, &proxy, b3DefaultQueryFilter(), m_transform );
2026-05-10 13:36:52 -07:00
if ( overlaps )
{
DrawSolidCapsule( { origin, b3Quat_identity }, capsule, MakeColor( b3_colorGreen ) );
2026-05-10 13:36:52 -07:00
}
else
{
DrawSolidCapsule( { origin, b3Quat_identity }, capsule, MakeColor( b3_colorGray ) );
2026-05-10 13:36:52 -07:00
}
}
// Collide capsule
{
b3Pos origin = { -10.0f, 2.0f, -0.75f };
b3Capsule capsule = { { -0.25f, 0.0f, 0.0f }, { 0.25f, 1.0f, 0.0f }, 0.3f };
2026-05-10 13:36:52 -07:00
b3BodyPlaneResult bodyPlanes[4];
int count = b3Body_CollideMover( m_bodyId, bodyPlanes, 4, origin, &capsule, b3DefaultQueryFilter(), m_transform );
DrawSolidCapsule( { origin, b3Quat_identity }, capsule, MakeColor( b3_colorPurple ) );
2026-05-10 13:36:52 -07:00
for ( int i = 0; i < count; ++i )
{
b3PlaneResult result = bodyPlanes[i].result;
DrawPlane( result.plane.normal, b3ToPos( result.point ), MakeColor( b3_colorOrange ) );
2026-05-10 13:36:52 -07:00
}
}
}
static Sample* Create( SampleContext* context )
{
return new BodyCast( context );
}
b3HullData* m_cylinder;
2026-05-10 13:36:52 -07:00
b3BodyId m_bodyId;
b3WorldTransform m_transform;
2026-05-10 13:36:52 -07:00
b3Pos m_baseTranslation;
b3Pos m_origin;
2026-05-10 13:36:52 -07:00
int m_baseX;
int m_baseY;
bool m_tracking;
};
static int sampleBodyCast = RegisterSample( "Bodies", "Cast", BodyCast::Create );
2026-05-10 13:36:52 -07:00
// This shows how to drive a kinematic body to reach a target
class Kinematic : public Sample
{
public:
explicit Kinematic( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 0.0f, 30.0f, 10.0f, { 0.0f, 1.5f, 0.0f } );
}
AddGroundBox( 20.0f );
2026-05-10 13:36:52 -07:00
m_amplitude = 2.0f;
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_kinematicBody;
bodyDef.position.x = 2.0f * m_amplitude;
bodyDef.position.y = m_amplitude + 1.0f;
2026-05-10 13:36:52 -07:00
m_bodyId = b3CreateBody( m_worldId, &bodyDef );
b3BoxHull box = b3MakeBoxHull( 0.1f, 1.0f, 0.2f );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateHullShape( m_bodyId, &shapeDef, &box.base );
}
m_time = 0.0f;
}
void Step() override
{
float timeStep = m_context->hertz > 0.0f ? 1.0f / m_context->hertz : 0.0f;
if ( m_context->pause && m_context->singleStep == 0 )
{
timeStep = 0.0f;
}
float delay = 2.0f;
if ( timeStep > 0.0f && m_time > delay )
{
float t = m_time - delay;
b3Pos point;
2026-05-10 13:36:52 -07:00
point.x = 2.0f * m_amplitude * cosf( t );
point.y = m_amplitude * ( sinf( 2.0f * t ) + 1.0f ) + 1.0f;
2026-05-10 13:36:52 -07:00
point.z = 0.0f;
b3Quat rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, 2.0f * t );
b3Vec3 axis = b3RotateVector( rotation, { 0.0f, 1.0f, 0.0f } );
DrawLine( point - 0.5f * axis, point + 0.5f * axis, MakeColor( b3_colorPlum ) );
DrawPoint( point, 10.0f, MakeColor( b3_colorPlum ) );
2026-05-10 13:36:52 -07:00
b3Body_SetTargetTransform( m_bodyId, { point, rotation }, timeStep, true );
}
Sample::Step();
m_time += timeStep;
}
static Sample* Create( SampleContext* context )
{
return new Kinematic( context );
}
b3BodyId m_bodyId;
float m_amplitude;
float m_time;
};
static int sampleKinematic = RegisterSample( "Bodies", "Kinematic", Kinematic::Create );
2026-05-10 13:36:52 -07:00
class LockMixing : public Sample
{
public:
explicit LockMixing( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 40.0f, b3Pos_zero );
2026-05-10 13:36:52 -07:00
}
AddGroundBox( 20.0f );
2026-05-10 13:36:52 -07:00
b3BoxHull cube = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
b3ShapeDef shapeDef = b3DefaultShapeDef();
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.name = "free";
bodyDef.position = { 0.0f, 2.0f, 0.0f };
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateHullShape( bodyId, &shapeDef, &cube.base );
}
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.name = "angular xz";
bodyDef.position = { 2.0f, 2.0f, 0.0f };
bodyDef.motionLocks.angularX = true;
// bodyDef.motionLocks.angularY = true;
2026-05-10 13:36:52 -07:00
bodyDef.motionLocks.angularZ = true;
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateHullShape( bodyId, &shapeDef, &cube.base );
}
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.name = "linear xyz";
bodyDef.position = { -2.0f, 2.0f, 0.0f };
bodyDef.motionLocks.linearX = true;
bodyDef.motionLocks.linearY = true;
bodyDef.motionLocks.linearZ = true;
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateHullShape( bodyId, &shapeDef, &cube.base );
}
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.name = "full";
bodyDef.position = { 0.0f, 1.0f, 2.0f };
bodyDef.motionLocks.linearX = true;
bodyDef.motionLocks.linearY = true;
bodyDef.motionLocks.linearZ = true;
bodyDef.motionLocks.angularX = true;
bodyDef.motionLocks.angularY = true;
bodyDef.motionLocks.angularZ = true;
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateHullShape( bodyId, &shapeDef, &cube.base );
}
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.name = "static";
bodyDef.position = { 0.0f, 1.0f, -3.0f };
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateHullShape( bodyId, &shapeDef, &cube.base );
}
}
static Sample* Create( SampleContext* context )
{
return new LockMixing( context );
}
};
static int sampleLockMixing = RegisterSample( "Bodies", "Lock Mixing", LockMixing::Create );
// A fully rotation locked body uses a zero inverse inertia tensor
class FixedRotation : public Sample
{
public:
explicit FixedRotation( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 0.0f, 15.0f, 10.0f, b3Pos_zero );
}
AddGroundBox( 20.0f );
b3Capsule capsule = { { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 0.3f };
b3ShapeDef shapeDef = b3DefaultShapeDef();
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.position = { 0.0f, 0.5f, 0.0f };
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
}
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.position = { 0.3f, 0.5f, 0.0f };
bodyDef.type = b3_dynamicBody;
bodyDef.gravityScale = 0.0f;
bodyDef.enableSleep = false;
bodyDef.motionLocks.angularX = true;
bodyDef.motionLocks.angularY = true;
bodyDef.motionLocks.angularZ = true;
capsule.radius = 0.2f;
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
}
}
static Sample* Create( SampleContext* context )
{
return new FixedRotation( context );
}
};
static int sampleFixedRotation = RegisterSample( "Bodies", "Fixed Rotation", FixedRotation::Create );
// A spinning class ring flips its heavy gem from bottom to top, like this video:
// https://www.youtube.com/watch?v=_up0BiLCliA
// This is a fiddley test and requires careful tuning.
class ClassRing : public Sample
{
public:
explicit ClassRing( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 40.0f, 30.0f, 15.0f, { 0.0f, 2.0f, 0.0f } );
}
AddGroundBox( 100.0f );
constexpr int n = 24;
constexpr float r = 1.0f;
constexpr float tubeRadius = 0.1f * r;
constexpr float axisRadius = r - tubeRadius;
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = { 0.0f, r, 0.0f };
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisX, 13.0f * B3_PI / 180.0f );
bodyDef.allowFastRotation = true;
bodyDef.enableContactRecycling = false;
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 1.0f;
// Band built from a loop of capsules.
b3Vec3 vertices[n];
float deltaAngle = 2.0f * B3_PI / n;
b3CosSin cs = b3ComputeCosSin( deltaAngle );
float x = axisRadius, y = 0.0f;
for ( int i = 0; i < n; ++i )
{
vertices[i] = { x, y, 0.0f };
float x2 = cs.cosine * x - cs.sine * y;
float y2 = cs.sine * x + cs.cosine * y;
x = x2;
y = y2;
}
for ( int i = 0; i < n; ++i )
{
b3Capsule capsule = { vertices[i], vertices[( i + 1 ) % n], tubeRadius };
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
}
// Heavy gem provides the mass asymmetry that drives the inversion
shapeDef.density = 2.0f;
b3Sphere sphere = { .center = { 0.0f, -0.65f * r, 0.0f }, .radius = 0.3f };
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
b3Vec3 angularVelocity = b3RotateVector( bodyDef.rotation, { 0.0f, 100.0f, 0.0f } );
b3Body_SetAngularVelocity( bodyId, angularVelocity );
m_ringId = bodyId;
}
void Step() override
{
// This needs a high simulation rate so that contact points keep up with the high speed rotation.
float hertz = m_context->hertz;
int subStepCount = m_context->subStepCount;
// 960Hz simulation
int multiplier = 16;
m_context->hertz = multiplier * 60.0f;
m_context->subStepCount = 8;
// Multiple hidden steps to keep the render real-time.
for ( int i = 0; i < multiplier - 1; ++i )
{
b3World_Step( m_worldId, 1.0f / m_context->hertz, m_context->subStepCount );
}
Sample::Step();
m_context->hertz = hertz;
m_context->subStepCount = subStepCount;
}
static Sample* Create( SampleContext* context )
{
return new ClassRing( context );
}
b3BodyId m_ringId;
};
static int sampleClassRing = RegisterSample( "Bodies", "Class Ring", ClassRing::Create );
/*
mandude — 9:34 AM
I've run into a minor, and admittedly pretty niche, issue with kinematic bodies. Because box3d will skip mass computations on
kinematic bodies, the center of mass is never set. This means if the origin of a shape attached to the body isn't in the center of
the shape, and you add an angular velocity, the shape will rotate about whatever the shape's origin is. For a simple example, if
you have a cube as a mesh, with opposing corners at (0, 0, 0) and (1, 1, 1), rotation will happen about the corner of the cube.
It's possible to work around this by calling setMassData with zero mass, a custom center of mass, and an identity matrix for the
inertia tensor. That function tries to preserve momentum, so it can have unexpected effects on the velocity if the body is already
moving, so perhaps it would be useful to have a more direct way to specify a custom offset for the origin of a kinematic body?
*/
class OffsetKinematic : public Sample
{
public:
explicit OffsetKinematic( SampleContext* context )
: Sample( context )
{
if ( m_context->restart == false )
{
m_camera->SetView( 0.0f, 30.0f, 10.0f, { 0.0f, 1.5f, 0.0f } );
}
AddGroundBox( 20.0f );
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.name = "kinematic";
bodyDef.type = b3_kinematicBody;
bodyDef.position.y = 3.0;
b3BodyId bodyId = b3CreateBody( m_worldId, &bodyDef );
b3Vec3 offset = { 0.5f, 0.5f, 1.0f };
b3BoxHull box = b3MakeTransformedBoxHull( 0.5f, 0.5f, 1.0f, { .p = offset, .q = b3Quat_identity} );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateHullShape( bodyId, &shapeDef, &box.base );
b3MassData massData = b3Body_GetMassData( bodyId );
massData.center = offset;
b3Body_SetMassData( bodyId, massData );
b3Vec3 angularVelocity = {1.0f, -1.0f, 2.0f};
b3Body_SetAngularVelocity( bodyId, angularVelocity );
}
}
static Sample* Create( SampleContext* context )
{
return new OffsetKinematic( context );
}
};
static int sampleOffsetKinematic = RegisterSample( "Bodies", "Offset Kinematic", OffsetKinematic::Create );