Skip to content

Rigid bodies and colliders

Updated

A Rigid Body says how an entity moves in the physics world, and its colliders say what shape it has there. Together they turn a picture into an object: something that falls, lands, slides, tips over and gets in the way of other things. This page explains the body types, the props of the Rigid Body and of every collider shape, how several colliders combine into one body, and how a script moves a physical entity without fighting the simulation.

  • You know how to add components to an entity and read them from a script with getComponent. See Components.
  • You know the physics vocabulary from the Physics overview: rigid body, body type, collider, sensor.
  • Physics runs in play only. In the editor nothing falls.

The physics world is built from a few simple rules:

  • One body per entity. An entity has at most one Rigid Body (comp-physics-rigid-body).
  • Colliders are the shape. An entity may carry several colliders, of any shapes. Together they make one compound body that moves as a single piece.
  • Colliders without a body are fixed. An entity with colliders but no Rigid Body gets an implicit fixed body. This is how floors, walls and other static scenery are made.
  • A body without a collider gets a box. A Rigid Body with no collider gets one box fitted to the entity’s mesh.
  • Only the entity’s own meshes count. A shape fitted to the mesh uses the entity’s own meshes. Child entities, such as decals, labels or nested objects, are not part of it; give them their own colliders if they need to collide.

A crate built from several shapes looks like this:

Crate (Primitive Mesh)
├── Rigid Body type: dynamic
├── Box Collider size 1 × 0.2 × 1, offset y 0
├── Box Collider size 0.2 × 1 × 1, offset x -0.4
├── Box Collider size 0.2 × 1 × 1, offset x 0.4
└── Sphere Collider radius 0.3, offset y 0.8

Rigid bodies and colliders are offered on meshes, primitives, groups and snap zones. An invisible trigger or wall is simply a group with colliders.

The Rigid Body’s type decides who moves the body.

Type Moved by Pushed by other bodies Typical use
dynamic Gravity, forces, impulses and collisions Yes Crates, balls, debris, anything the player can knock over.
kinematic Your script, through kinematic moves No, but it pushes dynamic bodies Lifts, moving platforms, sliding doors, a carried object.
fixed Nothing: it never moves No Floors, walls, terrain, buildings.

A Rigid Body whose props leave type out is dynamic.

Prop Type Default Description
type 'dynamic' | 'kinematic' | 'fixed' 'dynamic' Who moves the body; see Body types.
linearDamping number 0 Slows the body’s travel each step, like air resistance. Higher values stop it sooner.
angularDamping number 0 Slows the body’s spin each step.
gravityScale number 1 Gravity multiplier from 0 to 1. 0 floats, 1 falls normally.
sleep boolean true Let the body sleep when it comes to rest, which saves work.
continuousCollision boolean false Stops fast bodies passing through thin colliders between two steps, at extra cost. Turn it on for small, fast objects such as bullets.
mass number 0 The body’s total mass in kilograms. A positive mass is spread over its colliders by volume, so the centre of mass and the spin follow the shapes. 0 derives the mass from the collider volume. Ignored on fixed bodies.
lockRotation { x?, y?, z? } all unlocked Never turn about these world axes. { x: true, z: true } keeps a body upright. Dynamic bodies only.
lockPosition { x?, y?, z? } all unlocked Never move along these world axes. { y: true } keeps a body at its height. Dynamic bodies only.

Change props from a script with setProps, for example body.setProps({ type: 'kinematic' }).

Every collider shape is a component that extends PhysicsColliderComponent. They all share these props:

Prop Type Default Description
offset { x, y, z } 0, 0, 0 Where the shape sits relative to the entity, in local units.
rotation { x, y, z } 0, 0, 0 How the shape is turned relative to the entity, Euler angles in radians.
friction number 0.5 How much surfaces grip, from 0 (ice) to 5. Values above 1 are valid.
restitution number 0 Bounciness, from 0 (no bounce) to 1 (keeps all its speed).
sensor boolean false A sensor reports overlaps but applies no forces. See Collisions and triggers.
collisionLayer integer 0 The physics layer the collider is on, 0 to 15.

The shapes and their own props:

Component Type Shape props Best for
Box Collider comp-box-collider size { x, y, z }, the full size; left out, the box fits the mesh bounds. borderRadius (default 0) rounds its edges, in metres. Crates, walls, floors, furniture.
Sphere Collider comp-sphere-collider radius (default 0.5). Balls, round rocks. The cheapest shape.
Capsule Collider comp-capsule-collider radius (default 0.5), height (default 2), the total including its rounded caps, along local Y. Bottles, logs, limbs.
Cylinder Collider comp-cylinder-collider radius (default 0.5), height (default 1), along local Y. Barrels, pillars, wheels.
Cone Collider comp-cone-collider radius (default 0.5), height (default 1), tip up. Traffic cones, spikes.
Convex Hull Collider comp-convex-hull-collider None: the tightest convex shape around the entity’s meshes. Irregular moving objects. It fills hollows: a bowl collides as a solid lump.
Mesh Collider comp-mesh-collider None: the entity’s exact triangles. Static scenery such as terrain and buildings. On a moving body it falls back to a convex hull, with a warning.

The real room in AR has two more collider shapes, the XR Plane Collider and the XR Mesh Collider, described in XR world.

Collider components allow several instances per entity. Read them all with getComponents:

const shapes = self.parent.getComponents(PhysicsColliderComponent);
log(shapes.length, 'colliders');

getComponent(PhysicsColliderComponent) returns the first one. PhysicsColliderComponent exists as a script global so you can find colliders of any shape, but you cannot add it: add a concrete shape such as BoxColliderComponent.

A solid object is a Primitive Mesh with a collider: there is no separate collider entity.

  • Box. A Box Collider without a size fits the mesh bounds, so it follows the Geometry when you resize it.
  • Sphere, cylinder. The defaults of the Sphere and Cylinder Colliders match the unit primitive (radius 0.5, height 1). Size the object with the entity’s scale and the collider follows.
  • Invisible wall. A Group with a Box Collider of its own size.
  • Movable. Without a Rigid Body the shape is fixed. Add one to make it fall, roll or be pushed.

A body’s pose belongs to the simulation. Move it through an API the simulation understands:

To… Use Notes
Place any body somewhere at once body.teleport(position, rotation?) The body arrives at rest. A body that would start inside the ground is raised onto it. Returns where it ended.
Place an entity and its body entity.setPosition(x, y, z), setRotation, setScale The transform setters move the body too. A scale change rebuilds the colliders at the new size.
Drive a kinematic body by a step body.moveKinematic(dx, dy, dz) Queues a world-space move for the next physics step. It pushes what it touches.
Drive a kinematic body to a pose body.setKinematicTarget(position, rotation) The world pose it reaches by the next step. rotation is a quaternion { x, y, z, w }.
Push a dynamic body Impulses, forces, velocities See Forces and impulses.

Vectors are plain { x, y, z } objects; a THREE.Vector3 works too.

Member Description
isSimulated true while a live body backs the component: in play, with the entity enabled.
bodyType The kind driving the live body now: 'dynamic', 'kinematic' or 'fixed'.
isKinematic Whether the live body is kinematic now. A grabbed dynamic body reads as kinematic while it is held.
  • Editing a collider’s props, or adding or removing a collider, rebuilds the entity’s body.
  • A scale change through the setters rebuilds the colliders at the new size: every axis for boxes and meshes, X for a sphere’s radius, X and Y for a capsule or a cylinder.

The simulation steps at a fixed rate and the display at its own, so a moving body is drawn between the poses of its last two steps. The entity’s transform is that drawn pose, at most one step behind the simulation. Physics itself (velocities, contacts, raycasts) uses the exact pose. A teleport, or a move through the setters, shows at once instead of sliding there.

Items on a shelf start as fixed scenery. When something emits shelf/shake, the script adds a dynamic Rigid Body to each item so they all fall.

ShakeShelf.jx
#pragma lifecycle(startup)
// Put this Behavior on the Shelf group. Each child of the shelf is a mesh with a Box Collider.
let items = [];
function startup() {
items = self.parent.children.filter((child) => isEntity(child) && child !== self);
on('shelf/shake', () => {
for (const item of items) {
if (item.getComponent(PhysicsRigidBodyComponent)) continue;
item.addComponent(new PhysicsRigidBodyComponent({ type: 'dynamic', mass: 2 }));
}
log(`${items.length} items fall`);
});
}
  • self.parent.children lists the shelf’s direct children; isEntity keeps only entities, skipping plain objects such as the shelf’s own mesh, and child !== self leaves out the Behavior itself.
  • Before the signal, each item has only a Box Collider, so it is fixed scenery.
  • addComponent(new PhysicsRigidBodyComponent({ … })) gives the item a body. Its existing Box Collider becomes that body’s shape, and gravity takes over on the next step.
  • The getComponent check makes the handler safe to run twice.

A kinematic platform slides back and forth between two points. Dynamic boxes resting on it are carried and pushed, and nothing can push the platform off its path.

MovingPlatform.jx
#pragma lifecycle(startup, update)
const TRAVEL = 4; // metres from one end to the other
const PERIOD = 6; // seconds for a full back-and-forth
let body;
let start;
let turn;
const target = new THREE.Vector3();
// The platform has a Rigid Body with type 'kinematic' and a Box Collider.
function startup() {
body = self.parent.getComponent(PhysicsRigidBodyComponent);
start = self.parent.position.clone();
turn = self.parent.quaternion.clone();
}
function update(dt, time) {
const t = (Math.sin((time / PERIOD) * M_TAU) + 1) / 2; // 0 → 1 → 0
target.copy(start);
target.x += t * TRAVEL;
body.setKinematicTarget(target, turn);
}
  • The platform’s Rigid Body is kinematic, so gravity ignores it and dynamic bodies cannot push it.
  • start and turn are copied once: the platform’s home position and its fixed rotation.
  • In update, time is the seconds since play began. The sine wave turns it into a smooth value going from 0 to 1 and back.
  • setKinematicTarget asks the simulation to bring the body to that pose by the next step. Because the simulation moves it, the platform carries and pushes the dynamic bodies it touches. target is reused every frame instead of creating a new vector.

3. Respawn objects that fall off the world

Section titled “3. Respawn objects that fall off the world”

A ball knocked off the map falls for ever. This script brings it back to where it started.

Respawn.jx
#pragma lifecycle(startup, update)
const KILL_HEIGHT = -10;
let body;
let home;
let homeRotation;
// On a dynamic ball with a Rigid Body and a Sphere Collider.
function startup() {
body = self.parent.getComponent(PhysicsRigidBodyComponent);
home = self.parent.position.clone();
homeRotation = self.parent.quaternion.clone();
}
function update() {
if (self.parent.position.y > KILL_HEIGHT) return;
const landed = body.teleport(home, homeRotation);
log('Respawned at', landed);
emit('ball/respawned', { id: self.parent.entityId });
}
  • self.parent.position is the drawn pose, which is good enough to check a height.
  • teleport puts the body back at its start pose, at rest, so it does not keep the speed it had while falling. Writing self.parent.position.copy(home) would not work: the body would pull the entity straight back.
  • teleport returns where the body really ended, which can differ if the start point was inside the ground.
  • emit tells other scripts, for example a score keeper, that the ball came back.

4. Switch a body between thrown and carried

Section titled “4. Switch a body between thrown and carried”

A crate can be carried by a script (kinematic) or dropped to fall freely (dynamic). Switching the type with setProps keeps its colliders.

CarryCrate.jx
#pragma lifecycle(startup, update)
const HOLD = new THREE.Vector3(0, 1.2, -1.5); // in front of the camera
const held = new THREE.Vector3();
const facing = new THREE.Quaternion();
let crate;
let body;
let carrying = false;
function startup() {
crate = findEntity('Crate');
body = crate.getComponent(PhysicsRigidBodyComponent);
}
function update() {
if (Input.keyboard.isKeyReleased(Key.E)) {
carrying = !carrying;
body.setProps({ type: carrying ? 'kinematic' : 'dynamic' });
if (!carrying) body.wakeUp();
}
if (!carrying || !camera) return;
held.copy(HOLD).applyMatrix4(camera.matrixWorld);
camera.getWorldQuaternion(facing);
body.setKinematicTarget(held, facing);
}
  • Pressing E toggles between carrying and dropping. setProps({ type }) changes the body type; the crate keeps its shape.
  • camera is the camera the player sees through. HOLD is a point in the camera’s own space; applyMatrix4(camera.matrixWorld) turns it into a world point in front of the player.
  • While carried, setKinematicTarget drags the crate to that point every frame, and the crate pushes whatever it hits.
  • On drop, wakeUp makes sure the now-dynamic crate starts falling at once.
ListColliders.jx
#pragma lifecycle(startup)
function startup() {
const shapes = self.parent.getComponents(PhysicsColliderComponent);
for (const shape of shapes) {
log(shape.type, 'sensor:', shape.props.sensor === true, 'layer:', shape.props.collisionLayer ?? 0);
}
// Make every shape a little bouncier.
for (const shape of shapes) shape.setProps({ restitution: 0.3 });
}
  • getComponents(PhysicsColliderComponent) returns every collider, whatever its shape, in the order they were added.
  • type is the component type, such as comp-box-collider. props holds each collider’s current props; a prop left out uses its default.
  • setProps on a collider rebuilds the entity’s body once the edit is applied, so avoid changing colliders every frame.
  • Moving a body by writing its position. entity.position.x += 1 changes only the drawing; the body pulls it back. Use setPosition, teleport or a kinematic move.
  • Moving a dynamic body with kinematic calls. moveKinematic and setKinematicTarget are for kinematic bodies. To steer a dynamic body, use forces, impulses or velocities.
  • A Mesh Collider on a falling object. Exact triangles only work on fixed scenery. A moving body falls back to a convex hull. Use simple shapes on dynamic bodies.
  • Expecting a model’s children to collide. A fitted shape uses the entity’s own meshes only. Give child entities their own colliders.
  • A fast bullet passing through a thin wall. Between two steps it may jump over the wall. Turn on continuousCollision for that body only.
  • Rebuilding bodies every frame. Changing collider props, adding colliders or scaling an entity rebuilds its body. Do it on events, not in update.
  • Adding a Rigid Body to a character. A Character Controller’s capsule is the entity’s body; the Rigid Body is ignored.
  • Testing in the editor. Physics does not step there. Enter play.