Skip to content

Commit 13daaac

Browse files
Site changes [skip-ci]
1 parent 2509b68 commit 13daaac

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

llms-full.txt

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

6488+
### Adaptive zoom
6489+
6490+
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*.
6491+
6492+
Two common approaches to adaptive zoom are:
6493+
6494+
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.
6495+
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.
6496+
6497+
Example:
6498+
6499+
```lua
6500+
local DISPLAY_WIDTH = sys.get_config_int("display.width")
6501+
local DISPLAY_HEIGHT = sys.get_config_int("display.height")
6502+
6503+
function init(self)
6504+
local initial_zoom = go.get("#camera", "orthographic_zoom")
6505+
local display_scale = window.get_display_scale()
6506+
window.set_listener(function(self, event, data)
6507+
if event == window.WINDOW_EVENT_RESIZED then
6508+
local window_width = data.width
6509+
local window_height = data.height
6510+
local design_width = DISPLAY_WIDTH / initial_zoom
6511+
local design_height = DISPLAY_HEIGHT / initial_zoom
6512+
6513+
-- max zoom: ensure that the initial design dimensions will fill and expand beyond the screen bounds
6514+
local zoom = math.max(window_width / design_width, window_height / design_height) / display_scale
6515+
6516+
-- min zoom: ensure that the initial design dimensions will shrink and be contained within the screen bounds
6517+
--local zoom = math.min(window_width / design_width, window_height / design_height) / display_scale
6518+
6519+
go.set("#camera", "orthographic_zoom", zoom)
6520+
end
6521+
end)
6522+
end
6523+
```
6524+
6525+
A complete example of adaptive zoom can be seen in [this sample project](https://github.com/defold/sample-adaptive-zoom).
6526+
6527+
64886528
### Following a game object
64896529

64906530
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:

manuals/camera.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ toc:
1111
- Render script
1212
- Panning the camera
1313
- Zooming the camera
14+
- Adaptive zoom
1415
- Following a game object
1516
- Converting mouse to world coordinates
1617
- Runtime manipulation
@@ -186,6 +187,46 @@ You can zoom in and out when using an orthographic camera by changing the *Ortho
186187
go.set("#camera", "orthographic_zoom", 2)
187188
```
188189

190+
### Adaptive zoom
191+
192+
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*.
193+
194+
Two common approaches to adaptive zoom are:
195+
196+
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.
197+
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.
198+
199+
Example:
200+
201+
```lua
202+
local DISPLAY_WIDTH = sys.get_config_int("display.width")
203+
local DISPLAY_HEIGHT = sys.get_config_int("display.height")
204+
205+
function init(self)
206+
local initial_zoom = go.get("#camera", "orthographic_zoom")
207+
local display_scale = window.get_display_scale()
208+
window.set_listener(function(self, event, data)
209+
if event == window.WINDOW_EVENT_RESIZED then
210+
local window_width = data.width
211+
local window_height = data.height
212+
local design_width = DISPLAY_WIDTH / initial_zoom
213+
local design_height = DISPLAY_HEIGHT / initial_zoom
214+
215+
-- max zoom: ensure that the initial design dimensions will fill and expand beyond the screen bounds
216+
local zoom = math.max(window_width / design_width, window_height / design_height) / display_scale
217+
218+
-- min zoom: ensure that the initial design dimensions will shrink and be contained within the screen bounds
219+
--local zoom = math.min(window_width / design_width, window_height / design_height) / display_scale
220+
221+
go.set("#camera", "orthographic_zoom", zoom)
222+
end
223+
end)
224+
end
225+
```
226+
227+
A complete example of adaptive zoom can be seen in [this sample project](https://github.com/defold/sample-adaptive-zoom).
228+
229+
189230
### Following a game object
190231

191232
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)