Skip to main content

Physics

A plugin wrapping Rapier: rapier3d for body3d and collider3d, rapier2d for body2d and collider2d. Both worlds run over the same scene tree, built with Rapier's enhanced-determinism feature, stepped on a fixed 60 Hz tick.

Declaring bodies​

[[nodes]]
name = "Crate"
transform = { position = [0, 3, 0] }
body3d = { kind = "dynamic" } # dynamic | kinematic | static
collider3d = { kind = "box", size = [1.0, 1.0, 1.0] }
this.node.body3d.set(#{ kind: physics3d::BODY_DYNAMIC });
this.node.collider3d.set(#{ kind: physics3d::SHAPE_SPHERE, radius: 0.5 });

this.node.body2d.set(#{ kind: physics2d::BODY_DYNAMIC });
this.node.collider2d.set(#{ kind: physics2d::SHAPE_RECTANGLE, size: [1.0, 1.0] });
  • A collider takes kind (sphere and box in 3D, circle and rectangle in 2D, and a dozen more each) with the matching dimensions.
  • Plus restitution, friction and density.
  • A body's mass is its total, spread over its colliders' shapes; 0 sums what they weigh.
  • collision_layer and collision_mask name layers 1 to 32, as Godot's do.
  • Scene keys and script constants use the same names.

Component handles​

this.node.body2d.apply_impulse(1.0, 0.0);
  • A function that drives a component is a method on that component's handle.
  • node.body2d binds to the module that drives the component. apply_impulse on a body3d handle reaches physics3d.
  • Handles also carry get(), set(table), has() and remove().
  • A method no module declared for the component is an error naming it (node.sprite.apply_impulse).
  • Each component's methods are on its reference page, for example body2d.

Driving the simulation​

ModuleWhat is in it
physics3d, physics2dBodies, colliders, joints, velocities and queries, one module per world
physics2d onlymax_contact_impulse(node), the strongest impact a body took this frame
physicsBoth worlds: set_paused, is_paused, clear, set_sleeping_allowed, sleeping_allowed, the debug draw, the solver's tuning and counters

The editor pauses physics until play. Its Physics workspace draws every body's collider and centre of mass over the scene, playing or not.

The Physics workspace's collider and centre-of-mass overlays over examples/angrynerds, the Bird selected

The full function lists are in the script modules reference.

Determinism​

  • Physics steps on a fixed 60 Hz tick.
  • Order-sensitive collections iterate deterministically.
  • Two runs with identical inputs match bit for bit. See Determinism.