---
title: "Screen coordinates: project a world position to the screen, read the mouse cursor position on screen, raycast from a screen point"
description: "Where a point on the screen is, in every call that names one: projecting a world position to screen coordinates (worldToScreen)…"
section: "Topics"
slug: "topics-screen-coordinates"
canonical: "https://origozero.ai/docs/topics-screen-coordinates"
updated: "2026-12-05T22:41:02.520010851+00:00"
---

# Screen coordinates: project a world position to the screen, read the mouse cursor position on screen, raycast from a screen point

## The two spaces

**The scene viewport's physical pixels.** A pointer position, a touch, every
`input.events()` position and a `worldToScreen` projection are measured in
the pixels of the rect the scene is drawn into, from its top-left
(`camera.viewData().viewport`, `getViewportSize()`). `screenToRay`,
`screenToPlanePoint` and `Physics.raycastScreen` take the same space, so a
pointer position goes straight into a ray cast.

These are the window's pixels the scene is shown over, at every render scale.
`renderer.setResolutionScale` (which a phone or a tablet sets below 1 by
default) changes how many pixels the scene rasterizes at, which
`renderer.rasterSize()` reads, and leaves the viewport's size, every pointer
and touch position and every projection where they are: a finger on an object
and `worldToScreen` of that object read the same point at 0.5, 0.76 and 1.

**The UI's logical points.** A screen lays out in points, which a display
with more pixels than points (a HiDPI monitor, a phone) makes a different set
of numbers, on one of two surfaces (`ui.setScreenSpace`):

- `"camera"`: the camera's picture, where a game's HUD and menus lay out (the
  default for content UI). Its canvas is `ui.screenSize()`, and a widget's
  `pos` and anchors are stated on it.
- `"window"`: the window, where the editor's chrome lays out
  (`ui.screenSize("window")`), and the space `ui.getLayoutInfo`,
  `gui.bounds`, `ui.hitTest` and `ui.pointerWidget` answer in, wherever a
  widget's screen draws.

`ui.fromViewport(x, y, space)` carries a scene-viewport point onto either
surface, `"camera"` when `space` is left out, and `ui.toViewport(x, y, space)`
carries one back. `ui.pixelRatio(space)` is either surface's physical pixels
per point.

The two surfaces are one wherever the scene fills the window, as in a running
game. In the editor a Scene panel presents the scene inside a rect of the
window: the camera's canvas is fitted to that panel, so the two differ by the
panel's origin and by the scale its picture is drawn at. A HUD written with
these calls lands in the same place in both.

## A marker over a world point

`worldToScreen` answers the scene viewport's pixels; `ui.fromViewport`
carries the answer onto the HUD's canvas, where a widget placed at it sits
over the point. The marker is a small screen of its own, re-stated each
frame at the new position, its box centred on the point:

```lua
local SIZE = 24
local function marker(name: string, x: number, y: number, text: string)
    ui.updateScreen(name, { type = "area", id = name, pos = { x - SIZE / 2, y - SIZE / 2 },
        children = { { type = "label", props = { text = text },
            style = { width = SIZE, height = SIZE, textAlign = "center" } } } })
end

local s = worldToScreen(target.x, target.y + 2, target.z)
local at = ui.fromViewport(s.x, s.y)
marker("marker", at.x, at.y, "!")
```

## An arrow at the edge for a point off screen

`s.onScreen` says whether the point is in front of the camera and inside the
viewport. Off screen, and behind the camera too, `x` / `y` still point the way
to it: the line from the canvas centre to `ui.fromViewport(s.x, s.y)` is the
direction the camera turns to face the point (`s.behind` says it is behind).
Carry the point along that line to the canvas edge, and the arrow points
along it. A point behind the camera can project inside the canvas, so the
edge is found by scaling the line, never by clamping the point:

```lua
local function edgeArrow(s, margin)
    local size = ui.screenSize()
    local p = ui.fromViewport(s.x, s.y)
    local cx, cy = size.width / 2, size.height / 2
    local dx, dy = p.x - cx, p.y - cy
    local k = math.min(
        (cx - margin) / math.max(math.abs(dx), 1e-6),
        (cy - margin) / math.max(math.abs(dy), 1e-6)
    )
    return cx + dx * k, cy + dy * k, math.deg(math.atan2(dy, dx))
end

local s = worldToScreen(target.x, target.y, target.z)
if not s.onScreen then
    local x, y, angle = edgeArrow(s, 32)
    marker("arrow", x, y, ">") -- rotate the glyph by `angle`
end
```

## The pointer over the UI, and through it into the scene

The mouse cursor position reads in the scene viewport's pixels. Carry it to
the window to ask which widget it is over, or cast it as it is into the
scene:

```lua
local m = input.snapshot().mouse_position
local w = ui.fromViewport(m[1], m[2], "window")
local over = ui.hitTest(w.x, w.y)              -- the widget under the pointer
local hit = Physics.raycastScreen(m[1], m[2])  -- what the pointer is over in the world
```

A point read off the UI goes back through `ui.toViewport` before a ray cast:

```lua
local b = tools.use("gui", "bounds", "target")
local v = ui.toViewport(b.centerX, b.centerY, "window")
local hit = Physics.raycastScreen(v.x, v.y)    -- what the widget covers
```

## Simulated pointers

`input.simulateMouse*`, the `Zin.test` pointer primitives and `inputSim`
with target `"ui"` place a pointer on the window, in its physical pixels: a
`gui.bounds` rect times `ui.pixelRatio()`. `input.snapshot().mouse_position`
reads it back in the scene viewport's pixels, less the Scene panel's origin.
The `input` guide's *Testing it on every device* covers the simulated
devices.
