🧭 ScreenToWorld

What is it?

Converts screen coordinates (for example, the mouse position) into world coordinates, taking into account the current camera offset.

📖 Syntax

ScreenToWorld(x, y)

📦 Returns

{
  x: worldX,
  y: worldY
}

✅ Example

// Get the mouse position in world space
const { x, y } = ScreenToWorld(mouse.x, mouse.y);

// Use that position in a Raycast
Raycast(player.x, player.y, x, y, 800, true, player);

📌 Notes

- GameCrom uses a camera implemented with ctx.translate(offsetX, offsetY), which is why ScreenToWorld subtracts the offset.
- It is the recommended way to work with clicks or pointers in games with a moving camera.