Skip to content

Commit be8884c

Browse files
committed
Update camera.md
1 parent d27c7da commit be8884c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/en/manuals/camera.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,46 @@ You can zoom in and out when using an orthographic camera by changing the *Ortho
172172
go.set("#camera", "orthographic_zoom", 2)
173173
```
174174

175+
### Adaptive zoom
176+
177+
The concept behind adaptive zoom is to adjust the camera zoom value when the resolution of the display change from the initial resolution set in *game.project*.
178+
179+
Two common approaches to adaptive zoom are:
180+
181+
1. Max zoom - Calculate a zoom value such that the content covered by the initial resolution in *game.project* will fill and expand beyond the screen bounds, possibly hiding some content to the sides or above and below.
182+
2. Min zoom - Calculate a zoom value such that the content covered by the initial resolution in *game.project* will be completely contained within the screen bounds, possibly showing additional content to the sides or above and below.
183+
184+
Example:
185+
186+
```lua
187+
local DISPLAY_WIDTH = sys.get_config_int("display.width")
188+
local DISPLAY_HEIGHT = sys.get_config_int("display.height")
189+
190+
function init(self)
191+
local initial_zoom = go.get("#camera", "orthographic_zoom")
192+
local display_scale = window.get_display_scale()
193+
window.set_listener(function(self, event, data)
194+
if event == window.WINDOW_EVENT_RESIZED then
195+
local window_width = data.width
196+
local window_height = data.height
197+
local design_width = DISPLAY_WIDTH / initial_zoom
198+
local design_height = DISPLAY_HEIGHT / initial_zoom
199+
200+
-- max zoom: ensure that the initial design dimensions will fill and expand beyond the screen bounds
201+
local zoom = math.max(window_width / design_width, window_height / design_height) / display_scale
202+
203+
-- min zoom: ensure that the initial design dimensions will shrink and be contained within the screen bounds
204+
--local zoom = math.min(window_width / design_width, window_height / design_height) / display_scale
205+
206+
go.set("#camera", "orthographic_zoom", zoom)
207+
end
208+
end)
209+
end
210+
```
211+
212+
A complete example of adaptive zoom can be seen in [this sample project](https://github.com/defold/sample-adaptive-zoom).
213+
214+
175215
### Following a game object
176216

177217
You can have the camera follow a game object by setting the game object the camera component is attached to as a child of the game object to follow:

0 commit comments

Comments
 (0)