Skip to content

Commit cde12bc

Browse files
Site changes [skip-ci]
1 parent c33d919 commit cde12bc

File tree

3 files changed

+39563
-39531
lines changed

3 files changed

+39563
-39531
lines changed

llms-full.txt

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6538,26 +6538,33 @@ An alternative way is to update the position of the game object the camera compo
65386538
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:
65396539

65406540
```Lua
6541-
--- Convert from screen to world coordinates
6542-
-- @param sx Screen x
6543-
-- @param sy Screen y
6544-
-- @param sz Screen z
6545-
-- @param window_width Width of the window (use render.get_width() or window.get_size().x)
6546-
-- @param window_height Height of the window (use render.get_height() or window.get_size().y)
6547-
-- @param projection Camera/render projection (use go.get("#camera", "projection"))
6548-
-- @param view Camera/render view (use go.get("#camera", "view"))
6549-
-- @return wx World x
6550-
-- @return wy World y
6551-
-- @return wz World z
6552-
local function screen_to_world(sx, sy, sz, window_width, window_height, projection, view)
6541+
--- Convert screen to world coordinates taking into account
6542+
-- the view and projection of a specific camera
6543+
-- @param camera URL of camera to use for conversion
6544+
-- @param screen_x Screen x coordinate to convert
6545+
-- @param screen_y Screen y coordinate to convert
6546+
-- @param z optional z coordinate to pass through the conversion, defaults to 0
6547+
-- @return world_x The resulting world x coordinate of the screen coordinate
6548+
-- @return world_y The resulting world y coordinate of the screen coordinate
6549+
-- @return world_z The resulting world z coordinate of the screen coordinate
6550+
function M.screen_to_world(camera, screen_x, screen_y, z)
6551+
local projection = go.get(camera, "projection")
6552+
local view = go.get(camera, "view")
6553+
local w, h = window.get_size()
6554+
-- The window.get_size() function will return the scaled window size,
6555+
-- ie taking into account display scaling (Retina screens on macOS for
6556+
-- instance). We need to adjust for display scaling in our calculation.
6557+
local scale = window.get_display_scale()
6558+
w = w / scale
6559+
h = h / scale
6560+
6561+
-- https://defold.com/manuals/camera/#converting-mouse-to-world-coordinates
65536562
local inv = vmath.inv(projection * view)
6554-
sx = (2 * sx / window_width) - 1
6555-
sy = (2 * sy / window_height) - 1
6556-
sz = (2 * sz) - 1
6557-
local wx = sx * inv.m00 + sy * inv.m01 + sz * inv.m02 + inv.m03
6558-
local wy = sx * inv.m10 + sy * inv.m11 + sz * inv.m12 + inv.m13
6559-
local wz = sx * inv.m20 + sy * inv.m21 + sz * inv.m22 + inv.m23
6560-
return wx, wy, wz
6563+
local x = (2 * screen_x / w) - 1
6564+
local y = (2 * screen_y / h) - 1
6565+
local x1 = x * inv.m00 + y * inv.m01 + z * inv.m02 + inv.m03
6566+
local y1 = x * inv.m10 + y * inv.m11 + z * inv.m12 + inv.m13
6567+
return x1, y1, z or 0
65616568
end
65626569
```
65636570

manuals/camera.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -240,26 +240,33 @@ An alternative way is to update the position of the game object the camera compo
240240
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:
241241

242242
```Lua
243-
--- Convert from screen to world coordinates
244-
-- @param sx Screen x
245-
-- @param sy Screen y
246-
-- @param sz Screen z
247-
-- @param window_width Width of the window (use render.get_width() or window.get_size().x)
248-
-- @param window_height Height of the window (use render.get_height() or window.get_size().y)
249-
-- @param projection Camera/render projection (use go.get("#camera", "projection"))
250-
-- @param view Camera/render view (use go.get("#camera", "view"))
251-
-- @return wx World x
252-
-- @return wy World y
253-
-- @return wz World z
254-
local function screen_to_world(sx, sy, sz, window_width, window_height, projection, view)
243+
--- Convert screen to world coordinates taking into account
244+
-- the view and projection of a specific camera
245+
-- @param camera URL of camera to use for conversion
246+
-- @param screen_x Screen x coordinate to convert
247+
-- @param screen_y Screen y coordinate to convert
248+
-- @param z optional z coordinate to pass through the conversion, defaults to 0
249+
-- @return world_x The resulting world x coordinate of the screen coordinate
250+
-- @return world_y The resulting world y coordinate of the screen coordinate
251+
-- @return world_z The resulting world z coordinate of the screen coordinate
252+
function M.screen_to_world(camera, screen_x, screen_y, z)
253+
local projection = go.get(camera, "projection")
254+
local view = go.get(camera, "view")
255+
local w, h = window.get_size()
256+
-- The window.get_size() function will return the scaled window size,
257+
-- ie taking into account display scaling (Retina screens on macOS for
258+
-- instance). We need to adjust for display scaling in our calculation.
259+
local scale = window.get_display_scale()
260+
w = w / scale
261+
h = h / scale
262+
263+
-- https://defold.com/manuals/camera/#converting-mouse-to-world-coordinates
255264
local inv = vmath.inv(projection * view)
256-
sx = (2 * sx / window_width) - 1
257-
sy = (2 * sy / window_height) - 1
258-
sz = (2 * sz) - 1
259-
local wx = sx * inv.m00 + sy * inv.m01 + sz * inv.m02 + inv.m03
260-
local wy = sx * inv.m10 + sy * inv.m11 + sz * inv.m12 + inv.m13
261-
local wz = sx * inv.m20 + sy * inv.m21 + sz * inv.m22 + inv.m23
262-
return wx, wy, wz
265+
local x = (2 * screen_x / w) - 1
266+
local y = (2 * screen_y / h) - 1
267+
local x1 = x * inv.m00 + y * inv.m01 + z * inv.m02 + inv.m03
268+
local y1 = x * inv.m10 + y * inv.m11 + z * inv.m12 + inv.m13
269+
return x1, y1, z or 0
263270
end
264271
```
265272

0 commit comments

Comments
 (0)