Skip to content

Commit

Permalink
Inverted screen to world bounds vertical values
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Dec 15, 2017
1 parent db03a28 commit 5f75910
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ The orthographic/render folder contains a render script that does the above ment
## The Orthographic Camera API
The API can be used in two ways:

1. Calling functions on the camera.lua module
2. Sending messages to the camera.script
1. Calling functions on the ```camera.lua``` module
2. Sending messages to the ```camera.script```

### camera.shake(camera_id, [intensity], [duration], [direction], [cb])
Shake the camera.
Expand Down Expand Up @@ -141,7 +141,8 @@ Limits the camera position to within the specified rectangle.
* ```right``` (number) - Right edge of camera bounds
* ```bottom``` (number) - Bottom edge of camera bounds

### camera.screen_to_world(camera_id, x, y, [z])

### camera.screen_to_world(camera_id, screen)
Translate screen coordinates to world coordinates, based on the view and projection of the camera.

**PARAMETERS**
Expand All @@ -152,7 +153,17 @@ Translate screen coordinates to world coordinates, based on the view and project
* ```world_coords``` (vector3) World coordinates


### camera.world_to_screen(camera_id, x, y)
### camera.screen_to_world_bounds(camera_id)
Translate screen boundaries (corners) to world coordinates, based on the view and projection of the camera.

**PARAMETERS**
* ```camera_id``` (hash|url)

**RETURN**
* ```bounds``` (vector4) Screen bounds (x = left, y = top, z = right, w = bottom)


### camera.world_to_screen(camera_id, world)
Translate world coordinates to screen coordinates, based on the view and projection of the camera. This is useful when manually culling game objects and you need to determine if a world coordinate will be visible or not.

**PARAMETER**
Expand Down Expand Up @@ -250,6 +261,3 @@ Enable the camera. While the camera is enabled it will update it's view and proj

### disable
Disable the camera.

## License
This library is released under the same [Terms and Conditions as Defold](http://www.defold.com/about-terms/).
8 changes: 4 additions & 4 deletions example/camera_controls.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ end

function update(self, dt)
local bounds = camera.screen_to_world_bounds(CAMERA_ID)
gui.set_text(gui.get_node("bottom_left"), ("%d,%d"):format(bounds.x, bounds.y))
gui.set_text(gui.get_node("bottom_right"), ("%d,%d"):format(bounds.z, bounds.y))
gui.set_text(gui.get_node("top_left"), ("%d,%d"):format(bounds.x, bounds.w))
gui.set_text(gui.get_node("top_right"), ("%d,%d"):format(bounds.z, bounds.w))
gui.set_text(gui.get_node("bottom_left"), ("%d,%d"):format(bounds.x, bounds.w))
gui.set_text(gui.get_node("bottom_right"), ("%d,%d"):format(bounds.z, bounds.w))
gui.set_text(gui.get_node("top_left"), ("%d,%d"):format(bounds.x, bounds.y))
gui.set_text(gui.get_node("top_right"), ("%d,%d"):format(bounds.z, bounds.y))
end


Expand Down
4 changes: 2 additions & 2 deletions orthographic/camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -448,15 +448,15 @@ end
-- Note: You need to have called update() at least once (this is done automatically
-- by the camera.script)
-- @param camera_id
-- @return bounds Vector4 where x is left, y is bottom, z is right and w is top
-- @return bounds Vector4 where x is left, y is top, z is right and w is bottom
function M.screen_to_world_bounds(camera_id)
local view = cameras[camera_id].view or MATRIX4
local projection = cameras[camera_id].projection or MATRIX4
local inv = vmath.inv(projection * view)
local bl_x, bl_y = unproject_xyz(inv, 0, 0, 0)
local br_x, br_y = unproject_xyz(inv, DISPLAY_WIDTH, 0, 0)
local tl_x, tl_y = unproject_xyz(inv, 0, DISPLAY_HEIGHT, 0)
return vmath.vector4(bl_x, bl_y, br_x, tl_y)
return vmath.vector4(bl_x, tl_y, br_x, bl_y)
end

return M

0 comments on commit 5f75910

Please sign in to comment.