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: axis is the twist axis in each body. twist limits rotation about it in radians, and swing limits the cone half-angle.
  • Revolute: hinges about axis, with optional angle limits.
  • Motors: a positive stiffness or damping springs 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_impulse removes the joint after a step whose constraint impulse reaches it. World#broken_joints lists 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_kind converts 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-space at: point, and apply_torque_impulse change velocity according to mass.
  • Velocity: velocity, angular_velocity and motion read velocities. velocity(body, v) and angular_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:) and gravity_scale(body, scale) tune how a body moves.
  • Sleep: sleep, wake, sleeping? and sleep_threshold(body, linear:, angular:) control when resting bodies stop simulating.

Contact data and impacts

  • Contact data: each Contact carries a world-space point, a normal from first toward second, and the step's total normal impulse.
    • 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 from World#impacts. began? marks a pair that first touched in that step.
  • Frames: a Frame also 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:

  • advance steps once without copying poses. Read pose, position, contacts, triggers, each_contact or each_trigger afterwards.
  • step steps once and returns a copied Frame. Its tick, pose(body), pose?(body), contacts and triggers (and in 3D impacts and broken_joints) remain available after another step, body removal or world close.
  • tick counts completed steps; timestep is the fixed step in seconds. In 3D, timestep= (or seconds=) changes it for later steps, for slow motion.
  • A Contact has kind (Begin or End), first, second, began?, ended? and involves?(body). For triggers, first is the sensor.
  • remove(body) and remove_joint(joint) release handles.
  • BodyKind is Static, Kinematic or Dynamic in both shards.
  • Layer masks are UInt32 in both shards; UInt64::MAX means all layers.
  • Closed worlds and stale or foreign handles raise Physics3D::Error, a Garnet::Error. Invalid arguments raise ArgumentError.

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.

Repository

physics3d

Owner
Statistic
  • 0
  • 0
  • 0
  • 1
  • 1
  • 2 days ago
  • November 30, 2026
License

MIT License

Links
Synced at

Thu, 03 Dec 2026 02:11:06 GMT

Languages