Skip to content

Physics layers

Updated

By default every collider collides with every other one. Real projects need exceptions: ghosts that float through walls, bullets that pass through their own shooter, glass the camera can see through, a player who should not trip over the leaves on the ground. Physics layers express these exceptions. Each collider sits on one layer, and the project’s collision matrix says how each pair of layers meets.

Physics layers are separate from the Layers that group entities for rendering, picking and switching off.

A project has at most 16 physics layers, numbered by id from 0 to 15. Each has:

Field Description
id 0 to 15. This is the number colliders and scripts use.
name The name the editor shows.
color The colour the editor shows.

Layer 0, Default, always exists, and every collider starts on it. It can be renamed and recoloured, but not removed. You define the other layers, and the matrix, in the project’s physics settings in the editor.

A collider that names a layer the project does not define is built on Default.

Each pair of layers meets in one of three ways. A layer is also paired with itself.

Interaction What happens Events
collide The colliders push each other. PhysicsCollider::collision-enter and collision-exit
intersect They pass through each other, but the overlap is reported. PhysicsCollider::intersection-enter and intersection-exit
ignore They never meet. Nothing is reported. none
  • Every pair collides until set otherwise.
  • The matrix is symmetric: how A meets B is how B meets A.
  • A sensor never pushes. It intersects every layer its own layer collides or intersects with, and meets nothing on an ignore pair.
  • A layer that is removed and added again collides with every layer.

An example set of layers, used by the examples below:

id Name How it meets the other layers
0 Default Collides with every layer.
1 Player Collides with every layer, except Thrown, which it intersects.
2 Ghost Ignores Walls; collides with every other layer.
3 Walls Ignores Ghost; collides with every other layer.
4 Glass Collides with every layer. Sight rays leave it out.
5, 6 Props, Tools Collide with every layer.
7 Thrown Intersects Player; collides with every other layer.

Your own matrix will differ; scripts only need the ids.

Who Prop or option Effect
Every collider collisionLayer The layer the collider is on.
Character Controller collisionLayer The layer its capsule is on. Only colliders on layers it collides with stop it.
Camera Collision collisionLayer The layer the camera’s sweep is on. Only layers it collides with pull the camera in; layers it intersects or ignores, such as glass or foliage, let it through.
PhysicsRaycaster collisionLayers option The ray hits only colliders on those layers. Absent, every layer; an empty list, none.

Every collisionLayer is a whole number from 0 to 15. Change it from a script with setProps({ collisionLayer: id }); the body is rebuilt on the new layer.

The examples use the layer ids from the example matrix above. Name your layer ids once, at the top of a script, so a reader knows what 3 means.

While a power-up lasts, the player walks through walls.

GhostMode.jx
#pragma lifecycle(startup)
const PLAYER_LAYER = 1;
const GHOST_LAYER = 2; // ignores the Walls layer in the project's matrix
const DURATION = 5000; // milliseconds
let character;
let cancelTimer = null;
function startup() {
character = self.parent.getComponent(CharacterControllerComponent);
on('powerup/ghost', () => {
character.setProps({ collisionLayer: GHOST_LAYER });
log('Ghost mode on');
if (cancelTimer) cancelTimer();
cancelTimer = delay(() => {
character.setProps({ collisionLayer: PLAYER_LAYER });
log('Ghost mode off');
}, DURATION);
});
}
  • The Character Controller’s capsule sits on collisionLayer. Moving it to the Ghost layer, which ignores Walls, lets it pass through walls while still standing on floors on other layers.
  • delay switches it back after five seconds. It returns a cancel function, kept so a second power-up restarts the timer instead of ending ghost mode early.
  • The layer ids come from the project’s physics settings; the constants document them.

A guard sees the player through windows on the Glass layer, but not through walls.

GuardSight.jx
#pragma lifecycle(startup, update)
const BLOCKS_SIGHT = [0, 3]; // Default and Walls; Glass (4) is left out
const eye = new THREE.Vector3();
const target = new THREE.Vector3();
let guard;
let player;
let seen = false;
function startup() {
guard = findEntity('Guard');
player = findEntity('Player');
}
function update() {
guard.getWorldPosition(eye);
eye.y += 1.6;
player.getWorldPosition(target);
target.y += 1.6;
const blocked = PhysicsRaycaster.between(eye, target)
.hit(null, { collisionLayers: BLOCKS_SIGHT, ignore: [guard, player] });
const nowSeen = !blocked;
if (nowSeen !== seen) {
seen = nowSeen;
emit(seen ? 'guard/alert' : 'guard/lost');
}
}
  • between(eye, target) casts a ray from the guard’s eyes to the player’s head that stops at the player.
  • Only colliders on the layers in BLOCKS_SIGHT block it. Glass is on layer 4, which is not listed, so the ray passes through windows.
  • ignore skips the guard’s and the player’s own colliders.
  • seen remembers the last result, so the alert is emitted only when it changes, not every frame.

A ray from the mouse hits only colliders on the “pickable” layers, ignoring walls and floors.

PickLayers.jx
#pragma lifecycle(update)
const PICKABLE = [5, 6]; // Props and Tools layers
function update() {
if (!Input.mouse.isButtonReleased(MouseButton.Left)) return;
const hit = PhysicsRaycaster.fromScreen(Input.mouse.position()).hit(null, { collisionLayers: PICKABLE });
if (hit) emit('picked', { id: hit.entity.entityId });
}
  • PhysicsRaycaster.fromScreen makes a ray from the camera through the mouse position.
  • collisionLayers: PICKABLE limits the ray to colliders on layers 5 and 6. A wall in front of a prop on another layer is passed through.
  • PICKABLE is a constant outside update, so no new array is built every frame.
  • See Picking and raycasts for every raycaster option.

2. Let thrown items pass through their thrower

Section titled “2. Let thrown items pass through their thrower”

A ball thrown by the player starts inside the player’s capsule. For a moment it is put on a layer that intersects the Player layer, then returned to normal.

ThrowThrough.jx
#pragma lifecycle(startup)
const THROWN_LAYER = 7; // intersects Player in the project's matrix
const NORMAL_LAYER = 0;
// On a dynamic Ball with a Rigid Body and a Sphere Collider.
function startup() {
const body = self.parent.getComponent(PhysicsRigidBodyComponent);
const shape = self.parent.getComponent(PhysicsColliderComponent);
on('player/throw', (data) => {
shape.setProps({ collisionLayer: THROWN_LAYER });
delay(() => {
body.applyImpulse(data.impulse);
}, 0);
delay(() => shape.setProps({ collisionLayer: NORMAL_LAYER }), 400);
});
}
  • getComponent(PhysicsColliderComponent) finds the ball’s collider whatever its shape.
  • Changing collisionLayer rebuilds the ball’s body on the Thrown layer, which only intersects the Player layer, so the ball slips out of the capsule instead of being shoved back.
  • The impulse is applied on the next frame, after the layer change has been applied. data.impulse is sent by the script that emits player/throw.
  • After 400 milliseconds the ball returns to Default and collides with everything again.
  • Expecting layers to hide things. Physics layers only affect physics: contacts, triggers, the character, camera collision and physics rays. Use rendering layers to hide entities.
  • Using a layer the project does not define. The collider is built on Default. Check the layer list in the project’s physics settings.
  • Forgetting the matrix is symmetric. If Ghost ignores Walls, Walls ignore Ghost, for every collider on them.
  • Building collisionLayers arrays every frame. Keep them in constants.
  • An empty collisionLayers list. It hits nothing. Leave the option out to hit every layer.
  • Changing layers every frame. Each change rebuilds the body; do it on events.