physics3d
Garnet Physics 3D
An optional fixed-step, owner-thread 3D simulation module using the pinned Rapier 0.32.0 library through a small C ABI. World coordinates are Y-up. Worlds support fixed, dynamic and kinematic body handles, primitive colliders, heightfields, triangle terrain, collision transitions, ray and sphere casts, a capsule-capable character controller and a swept camera arm. Queries use the last completed broad-phase step. Write body poses back to ordinary scene components after stepping.
cargo build --locked --release --manifest-path physics3d/native/Cargo.toml
CRYSTAL_PATH="$(crystal env CRYSTAL_PATH):$PWD/math/src" crystal spec physics3d/spec
Use CharacterMotor to combine camera-relative movement with gravity and jump height. Provide named actions through garnet-input or your own game code. CameraArm sweeps from a pivot, contracts immediately under obstruction, and extends at a configurable rate. Copy World#contacts after each step when a system needs begin and end transitions. Close the World on its owner thread when finished.
Colliders accept category: and mask: bitmasks (UInt32). Box, sphere, and capsule colliders also accept sensor: true. Read overlap transitions from World#triggers after each step. Ray and sphere casts accept layer filters and skip sensors. Dynamic and kinematic bodies use their collider's filter when moving against the world.
World#compound accepts translated box, sphere and capsule parts. For each part, use half extents for a box, X as the sphere radius, or X and Y as the capsule half-height and radius.
Joints
fixed_joint, spherical_joint and revolute_joint anchor two bodies in their local spaces. Remove a joint explicitly or remove either body.
- Spherical:
axisis the twist axis in each body.twistlimits rotation about it in radians, andswinglimits the cone half-angle. - Revolute: hinges about
axis, with optional anglelimits. - Motors: a positive
stiffnessordampingsprings every free axis toward the rest pose.joint_motor(joint, :twist | :swing_y | :swing_z, target, ...)retargets one axis later, for example to blend a ragdoll back into a pose. - Breaking: a positive
break_impulseremoves the joint after a step whose constraint impulse reaches it.World#broken_jointslists the joints that broke in the most recent step. - Contacts: jointed bodies ignore each other unless
contacts: true.
Driving bodies
- Kinematic poses:
kinematic_target(body, position, orientation)sets the full pose for the next step. The step derives the body's velocity from the move, so it pushes the dynamic bodies it sweeps through. - Teleport:
teleport(body, position, orientation)moves a body to a full pose immediately without sweeping, for respawns and pooled bodies.teleport(body, position)keeps its rotation and velocity. - Changing kind:
change_kindconverts a body in place and keeps its pose, colliders and joints. A kinematic actor that becomes dynamic carries its last velocity. Use this to let an intact object react to a hit. - Impulses:
apply_impulse, with an optional world-spaceat:point, andapply_torque_impulsechange velocity according to mass. - Velocity:
velocity,angular_velocityandmotionread velocities.velocity(body, v)andangular_velocity(body, v)set them.
Materials and simulation controls
- Material:
material(body, friction:, restitution:, density:)applies to every collider on the body and recomputes its mass. Rapier averages the two surfaces' friction and restitution. - CCD:
ccd(body)enables continuous collision detection, so a fast or small body cannot tunnel through a thin collider. - Damping and gravity:
damping(body, linear:, angular:)andgravity_scale(body, scale)tune how a body moves. - Sleep:
sleep,wake,sleeping?andsleep_threshold(body, linear:, angular:)control when resting bodies stop simulating.
Contact data and impacts
- Contact data: each
Contactcarries a world-spacepoint, anormalfromfirsttowardsecond, and the step's total normalimpulse.- Ended contacts and pairs the solver does not resolve report zero impulse.
- The solver does not resolve a kinematic body against another kinematic or a fixed body.
- Kinematic contacts: call
kinematic_contacts(body)to also report a body's contacts against kinematic and fixed bodies. - Impacts:
impact_threshold(body, impulse)reports every touching pair involving that body whose step impulse reaches the threshold, not only new contacts. Read them fromWorld#impacts.began?marks a pair that first touched in that step. - Frames: a
Framealso copies the step's impacts and broken joints.
The optional garnet-physics3d-scene shard provides Physics3D::Scene::Plugin, which creates bodies from components, steps in FixedUpdate, writes Scene::Components::Pose3D and emits contact events.
Stepping and contacts (shared with 2D)
The 2D garnet-physics world uses the same verbs and shapes:
advancesteps once without copying poses. Readpose,position,contacts,triggers,each_contactoreach_triggerafterwards.stepsteps once and returns a copiedFrame. Itstick,pose(body),pose?(body),contactsandtriggers(and in 3Dimpactsandbroken_joints) remain available after another step, body removal or world close.tickcounts completed steps;timestepis the fixed step in seconds. In 3D,timestep=(orseconds=) changes it for later steps, for slow motion.- A
Contacthaskind(BeginorEnd),first,second,began?,ended?andinvolves?(body). For triggers,firstis the sensor. remove(body)andremove_joint(joint)release handles.BodyKindisStatic,KinematicorDynamicin both shards.- Layer masks are
UInt32in both shards;UInt64::MAXmeans all layers. - Closed worlds and stale or foreign handles raise
Physics3D::Error, aGarnet::Error. Invalid arguments raiseArgumentError.
Coordinates are Y-up: World::DEFAULT_GRAVITY is -9.81 on Y. The 2D world is Y-down and uses +9.81; both point down.
Deprecated names
| Deprecated | Use |
|---|---|
Kind, Kind::Fixed, :fixed |
BodyKind, BodyKind::Static, :static |
step_frame |
step (now returns the Frame) |
World.new(seconds) named seconds: |
World.new(timestep) |
Contact#began? only |
also Contact#kind |
See the scene plugin for authored colliders, Pose3D publication and contact events.
physics3d
- 0
- 0
- 0
- 1
- 1
- 2 days ago
- November 30, 2026
MIT License
Thu, 03 Dec 2026 02:11:06 GMT