diff --git a/README.md b/README.md index 3463f7e..7db3370 100644 --- a/README.md +++ b/README.md @@ -325,6 +325,16 @@ Translate world coordinates to [screen coordinates](#screen-coordinates), based **RETURN** * `screen_coords` (vector3) Screen coordinates +### camera.world_to_window(camera_id, world) +Translate world coordinates to [window coordinates](#window-coordinates), based on the view and projection of the camera. + +**PARAMETER** +* `camera_id` (hash|url|nil) nil for the first camera +* `world` (vector3) World coordinates to convert + +**RETURN** +* `window_coords` (vector3) Window coordinates + ### camera.unproject(view, projection, screen) Translate [screen coordinates](#screen-coordinates) to world coordinates using the specified view and projection. diff --git a/orthographic/camera.lua b/orthographic/camera.lua index c544b7c..6fe4ba1 100644 --- a/orthographic/camera.lua +++ b/orthographic/camera.lua @@ -784,6 +784,21 @@ function M.world_to_screen(camera_id, world, adjust_mode) return vmath.vector3(screen.x, screen.y, screen.z) end +--- Convert world coordinates to window coordinates based +-- 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 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) +end --- Translate world coordinates to screen coordinates given a -- view and projection matrix