Skip to content

Coordinate space

Updated

The same point can be described in several ways. On the screen, it is a pixel position. In the world, it is a position in the scene’s shared space. In an object’s own space (its local space), it is a position relative to that object, turning and moving with it. CoordinateSpace converts points between these three spaces in one call, so a script can drag something under the mouse, find the tip of a gun wherever the gun is held, or tell whether the player stands to the left or the right of a door.

You should know:

  • What screen points are: CSS pixels from the top-left of the page, as Input.mouse.position() gives them; see Keyboard, mouse and touch.
  • That every entity has a position, rotation and scale relative to its parent, and a resulting place in the world.
  • When to use a raycast instead: to find where the pointer meets a surface; see Picking and raycasts.
  • Screen space. Two numbers, x to the right and y down, in CSS pixels from the top-left of the page. It is what the mouse, touches and GraphicsRaycaster.fromScreen use, seen through the camera the player sees through.
  • World space. Three numbers in the scene’s shared frame: y is up. Raycast hits, getWorldPosition and XR controller poses are in world space.
  • Local space. Three numbers relative to one object: its origin is 0, 0, 0, x is its right, y its up, and -z its forward. A point in local space moves and turns with the object.
Call What it returns
worldToScreen(point) Where a world point shows on the screen, as a THREE.Vector2 in CSS pixels. A point behind the camera has no real screen position.
screenToWorld(point, distance) The world point distance units from the camera, under a screen point, as a THREE.Vector3.
localToWorld(point, object) A point given in object’s own space, in world space.
worldToLocal(point, object) A world point, in object’s own space.
  • Points accept a THREE.Vector2 or THREE.Vector3, or any plain { x, y } or { x, y, z }.
  • Every result is a new object. The point you pass is never changed, unlike an object’s own localToWorld, which overwrites its argument.
  • screenToWorld gives a point at a fixed distance from the camera, floating in the air. To find where the pointer meets the floor or an object, use GraphicsRaycaster.fromScreen(point).ground() or .hit() instead.
DragAtDistance.jx
#pragma lifecycle(startup, update)
const DISTANCE = 3; // metres from the camera
let lantern;
function startup() {
lantern = findEntity('Lantern');
}
function update() {
if (!Input.mouse.isButtonPressed(MouseButton.Left)) return;
const spot = CoordinateSpace.screenToWorld(Input.mouse.position(), DISTANCE);
lantern.position.copy(spot);
}
  • While the left button is held, the lantern follows the pointer, always three metres from the camera.
  • screenToWorld returns a new vector each call; position.copy writes it into the lantern.
  • The lantern has no rigid body, so writing its position is enough. For an object with physics, use setPosition(spot.x, spot.y, spot.z).

2. Fire from the muzzle, wherever the gun is

Section titled “2. Fire from the muzzle, wherever the gun is”
Muzzle.jx
#pragma lifecycle(startup, update)
const MUZZLE = { x: 0, y: 0.05, z: -0.4 }; // the barrel's tip, in the gun's own space
const AHEAD = { x: 0, y: 0.05, z: -1.4 }; // one metre further along the barrel
let gun;
function startup() {
gun = findEntity('Gun');
}
function update() {
if (!Input.mouse.isButtonReleased(MouseButton.Left)) return;
const tip = CoordinateSpace.localToWorld(MUZZLE, gun);
const ahead = CoordinateSpace.localToWorld(AHEAD, gun);
const direction = ahead.clone().sub(tip).normalize();
const bullet = spawn('Bullet', { position: tip, enabled: true });
bullet.getComponent(PhysicsRigidBodyComponent)?.applyImpulse(direction.multiplyScalar(5));
}
  • The muzzle’s position is written once, in the gun’s own space: 40 cm in front of its origin, slightly up. localToWorld turns it into a world point however the gun is held, moved or turned.
  • A second local point further along the barrel gives the firing direction: subtract the two world points and normalise.
  • spawn('Bullet', …) copies an existing entity named Bullet into the scene at the muzzle. The new bullet’s rigid body is pushed along the barrel with applyImpulse; see Physics.

1. Hover over a point of interest without a mesh

Section titled “1. Hover over a point of interest without a mesh”
HotspotHover.jx
#pragma lifecycle(startup, update)
const RADIUS = 40; // pixels
const toPoint = new THREE.Vector3();
const eye = new THREE.Vector3();
const forward = new THREE.Vector3();
let hotspots;
let current = null;
function startup() {
hotspots = ['Fountain', 'Statue', 'Gate'].map((name) => findEntity(name));
}
function inFront(entity) {
if (!camera) return false;
camera.getWorldPosition(eye);
entity.getWorldPosition(toPoint).sub(eye);
camera.getWorldDirection(forward);
return toPoint.dot(forward) > 0;
}
function update() {
const mouse = Input.mouse.position();
let nearest = null;
let best = RADIUS;
for (const spot of hotspots) {
if (!inFront(spot)) continue;
const onScreen = CoordinateSpace.worldToScreen(spot.getWorldPosition(toPoint));
const pixels = Math.hypot(onScreen.x - mouse.x, onScreen.y - mouse.y);
if (pixels < best) {
best = pixels;
nearest = spot;
}
}
if (nearest !== current) {
current = nearest;
emit('hotspot/hover', { name: current ? current.name : null });
}
}
  • worldToScreen projects each hotspot’s world position onto the screen; the script then measures, in pixels, how far it is from the mouse.
  • inFront skips points behind the camera, whose screen position would be meaningless. A camera looks along its own getWorldDirection, so a positive dot product means “in front”.
  • camera is the global for the camera the player sees through.
  • The nearest hotspot within 40 pixels counts as hovered, even though it has no mesh to click. The signal is emitted only when the hovered hotspot changes.

2. Which side of the door is the player on?

Section titled “2. Which side of the door is the player on?”
DoorSide.jx
#pragma lifecycle(startup, update)
const here = new THREE.Vector3();
let door;
let player;
let side = null;
function startup() {
door = findEntity('Door');
player = findEntity('Player');
}
function update() {
player.getWorldPosition(here);
const local = CoordinateSpace.worldToLocal(here, door);
// The door faces -Z: negative z is in front, positive z behind.
const now = local.z < 0 ? 'front' : 'back';
if (now !== side) {
side = now;
emit('door/side', { side, offset: local.x });
}
}
  • worldToLocal expresses the player’s position relative to the door. Whatever way the door is turned in the world, local.z says in front or behind, and local.x left or right.
  • The signal is emitted only when the side changes: when the player walks through the doorway. A room script can use it to switch lights or music.
  • Using screenToWorld to find the floor. It returns a point at a fixed distance in the air. Use GraphicsRaycaster.fromScreen(point).ground() or .hit() to find a surface.
  • Trusting worldToScreen for points behind the camera. Check that the point is in front first.
  • Mixing up local and world. position is relative to the parent. For an entity inside a group, use getWorldPosition before passing it as a world point.
  • Expecting the argument to change. CoordinateSpace always returns a new object. Use its return value.
  • Writing position on a physics body. Use setPosition on entities with a rigid body.