Skip to content

Commit 0bcddbb

Browse files
committed
Update camera.md
1 parent be8884c commit 0bcddbb

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

docs/en/manuals/camera.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,33 @@ An alternative way is to update the position of the game object the camera compo
225225
When the camera has panned, zoomed or changed it's projection from the default orthographic Stretch projection the mouse coordinates provided in the `on_input()` lifecycle function will no longer match to the world coordinates of your game objects. You need to manually account for the change in view or projection. The code to convert from mouse/screen coordinates to world coordinates looks like this:
226226

227227
```Lua
228-
--- Convert from screen to world coordinates
229-
-- @param sx Screen x
230-
-- @param sy Screen y
231-
-- @param sz Screen z
232-
-- @param window_width Width of the window (use render.get_width() or window.get_size().x)
233-
-- @param window_height Height of the window (use render.get_height() or window.get_size().y)
234-
-- @param projection Camera/render projection (use go.get("#camera", "projection"))
235-
-- @param view Camera/render view (use go.get("#camera", "view"))
236-
-- @return wx World x
237-
-- @return wy World y
238-
-- @return wz World z
239-
local function screen_to_world(sx, sy, sz, window_width, window_height, projection, view)
228+
--- Convert screen to world coordinates taking into account
229+
-- the view and projection of a specific camera
230+
-- @param camera URL of camera to use for conversion
231+
-- @param screen_x Screen x coordinate to convert
232+
-- @param screen_y Screen y coordinate to convert
233+
-- @param z optional z coordinate to pass through the conversion, defaults to 0
234+
-- @return world_x The resulting world x coordinate of the screen coordinate
235+
-- @return world_y The resulting world y coordinate of the screen coordinate
236+
-- @return world_z The resulting world z coordinate of the screen coordinate
237+
function M.screen_to_world(camera, screen_x, screen_y, z)
238+
local projection = go.get(camera, "projection")
239+
local view = go.get(camera, "view")
240+
local w, h = window.get_size()
241+
-- The window.get_size() function will return the scaled window size,
242+
-- ie taking into account display scaling (Retina screens on macOS for
243+
-- instance). We need to adjust for display scaling in our calculation.
244+
local scale = window.get_display_scale()
245+
w = w / scale
246+
h = h / scale
247+
248+
-- https://defold.com/manuals/camera/#converting-mouse-to-world-coordinates
240249
local inv = vmath.inv(projection * view)
241-
sx = (2 * sx / window_width) - 1
242-
sy = (2 * sy / window_height) - 1
243-
sz = (2 * sz) - 1
244-
local wx = sx * inv.m00 + sy * inv.m01 + sz * inv.m02 + inv.m03
245-
local wy = sx * inv.m10 + sy * inv.m11 + sz * inv.m12 + inv.m13
246-
local wz = sx * inv.m20 + sy * inv.m21 + sz * inv.m22 + inv.m23
247-
return wx, wy, wz
250+
local x = (2 * screen_x / w) - 1
251+
local y = (2 * screen_y / h) - 1
252+
local x1 = x * inv.m00 + y * inv.m01 + z * inv.m02 + inv.m03
253+
local y1 = x * inv.m10 + y * inv.m11 + z * inv.m12 + inv.m13
254+
return x1, y1, z or 0
248255
end
249256
```
250257

0 commit comments

Comments
 (0)