|
11 | 11 | - Render script
|
12 | 12 | - Panning the camera
|
13 | 13 | - Zooming the camera
|
| 14 | +- Adaptive zoom |
14 | 15 | - Following a game object
|
15 | 16 | - Converting mouse to world coordinates
|
16 | 17 | - Runtime manipulation
|
@@ -186,6 +187,46 @@ You can zoom in and out when using an orthographic camera by changing the *Ortho
|
186 | 187 | go.set("#camera", "orthographic_zoom", 2)
|
187 | 188 | ```
|
188 | 189 |
|
| 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 | + |
189 | 230 | ### Following a game object
|
190 | 231 |
|
191 | 232 | 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