Skip to content

World - Screen Transforms

World - Screen Transforms

function worldToScreen(worldPos, camera):
    return worldPos - camera.position

function screenToWorld(screenPos, camera):
    return screenPos + camera.position

World-to-screen / screen-to-world transforms are just a coordinate offset: the camera's position in the world defines what world coordinate maps to (0,0) on screen. Subtracting the camera's position converts world coordinates to screen coordinates; adding it converts back. (If you also support zoom, you additionally multiply by a scale factor before/after the offset.)