Skip to content

Commit

Permalink
Fix world_to_window (#65)
Browse files Browse the repository at this point in the history
* Fix "orthographic/camera.lua:795: attempt to index a nil value" from `world_to_window`

* Updated world_to_window camera_id param comment
  • Loading branch information
NaakkaDev authored Jan 22, 2024
1 parent 1fc8e62 commit 3fdeb0e
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions orthographic/camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -788,16 +788,20 @@ end
-- on a specific camera's view and projection
-- Window coordinates are the non-scaled coordinates provided by action.screen_x
-- and action.screen_y in on_input()
-- @param camera_id
-- @param camera_id or nil for the first camera
-- @param world World coordinates as a vector3
-- @return window coordinates
function M.world_to_window(camera_id, world)
local view = cameras[camera_id].view or MATRIX4
local projection = cameras[camera_id].projection or MATRIX4
local screen = M.project(view, projection, vmath.vector3(world))
local scale_x = screen.x / (dpi_ratio * DISPLAY_WIDTH / WINDOW_WIDTH)
local scale_y = screen.y / (dpi_ratio * DISPLAY_HEIGHT / WINDOW_HEIGHT)
return vmath.vector3(scale_x, scale_y, 0)
camera_id = camera_id or camera_ids[1]
assert(camera_id, "You must provide a camera id")
assert(world, "You must provide world coordinates to convert")
local camera = cameras[camera_id]
local view = camera.view or MATRIX4
local projection = camera.projection or MATRIX4
local screen = M.project(view, projection, vmath.vector3(world))
local scale_x = screen.x / (dpi_ratio * DISPLAY_WIDTH / WINDOW_WIDTH)
local scale_y = screen.y / (dpi_ratio * DISPLAY_HEIGHT / WINDOW_HEIGHT)
return vmath.vector3(scale_x, scale_y, 0)
end

--- Translate world coordinates to screen coordinates given a
Expand Down

0 comments on commit 3fdeb0e

Please sign in to comment.