# rmp::Camera

> The camera

Source: https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html

class `#include <rmp/scene.h>` [include/rmp/scene.h:91](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L91-L191)

class Camera

The camera

Owned by the scene, because the framing is a property of the context and not of any one object. Four things in this framework depend on it — the default `bounds`, [`rmp::input::pointer()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-input.html#pointer) in world units, the hit test behind on_click, and [`rmp::behavior::Parallax`](https://omardev29.github.io/rmp-docs/reference/behaviors/Parallax.html) — and they all read it from here.

```cpp
camera.follow = player.handle();
camera.limits = map.bounds();     // never shows the void outside the map
```

It starts centred on the design resolution (`[window] width` x height), so a scene that never mentions it draws exactly where it always did: at that size the camera is the identity. `position` is the CENTRE of what is visible.

On a window of another size it fits the world the way `[window] stretch` says ([`rmp::app::stretch()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-app.html#stretch)): under LETTERBOX and EXPAND the design is scaled into the window before `zoom` is applied, so `zoom = 2` is twice as big as the stretch drew it, and `view()`, `to_world()` and the pointer follow. Under NONE a bigger window shows more of the world at 1:1, as it always did.

The app opens it around the map and the objects and closes it before [`Scene::_draw()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#_draw), which is why [`rmp::ui`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-ui.html) inside `_draw()` does not drift with the view. Under LETTERBOX the map and the objects are also cut to the game's area, so the bars keep the clear colour. To draw in world units yourself: `BeginMode2D(camera.raylib())`.

Two details that are written wrong by hand more often than not:

- `limits` win over `follow`: when the player nears the edge of the map the camera stops and the player keeps going, instead of a black strip.
- Smoothing is a rate with delta in the exponent, not a lerp per frame, and shake is a separate offset that decays on its own and never touches `position`. Both are explained where they are declared below.

A number that is not finite — NaN or an infinity, the mark of a division by zero somewhere in the game — in a field below, an argument of `shake()` or the position of the object it follows never reaches what it draws or where it is: the camera uses the safe value the field names, and says so once in the log (`[dev] strict` stops a debug build there).

### `position`

Vector2 position{ RMP_WINDOW_WIDTH / 2.0f, RMP_WINDOW_HEIGHT / 2.0f }

field [include/rmp/scene.h:97](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L97)

The world point at the centre of the view. While `follow` has a target the camera writes it every frame, and `limits` clamp it. One that is not a finite number goes back to the middle of the design — or to the target it follows — on the next frame.

### `zoom`

float zoom = 1.0f

field [include/rmp/scene.h:101](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L101)

2 = everything twice as big, on top of the stretch. From 1/1024 to 1024: past either end it stops there, and one that is not a positive number is 1.

### `rotation`

float rotation = 0

field [include/rmp/scene.h:103](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L103)

Degrees, clockwise, like raylib's Camera2D. Not a number: unturned.

### `follow`

Handle<Object> follow

field [include/rmp/scene.h:109](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L109)

Follow this object: every frame, after it has moved, the camera centres on it. Empty = the camera stays where you left it. A handle, so a target that dies simply stops being followed. A target whose position is not a finite number is waited for: not followed on that axis until it is.

### `smoothing`

float smoothing = 0

field [include/rmp/scene.h:124](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L124)

How fast `follow` catches up, per second. 0 = it snaps, which is the default and what every one-screen game wants. 5 is a gentle drift, 20 is nearly a snap. It is a RATE and not a lerp factor per frame: the camera covers the same fraction of the distance in a second whether that second was 30 frames or 144, because delta is in the exponent — 1 - exp(-smoothing * delta). The per-frame `position += (target - position) * 0.1` that every tutorial writes follows twice as fast at 120 Hz as at 60, which is the bug this exists to not have.

A NEW target is snapped to, not glided to: a level that opens with the camera drifting in from the middle of the screen is the other half of the same classic bug. An infinite rate snaps too, and so does one that is negative or not a number.

### `limits`

Rectangle limits{}

field [include/rmp/scene.h:133](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L133)

Never show outside this rectangle, in world units. One axis at a time: a zero width or a zero height means "unbounded on that axis", so a runner pins y with `{ 0, 0, 0, RMP_WINDOW_HEIGHT }` and follows x freely. Empty = no limits. A limit narrower than the view centres the view on it. Applied AFTER the smoothing, so a smoothed camera still never shows the void past the edge of the level. An axis whose edge or size is not a finite number is unbounded.

### `shake`

void shake(float strength, float seconds = 0.3f)

function [include/rmp/scene.h:154](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L154)

Shake the view: up to `strength` world units, fading to nothing over `seconds`. A hit, an explosion, a landing.

The shake NEVER TOUCHES `position` or `view()`: it is an offset applied only to what is drawn, `raylib()` and the two conversions that go with it. So an object clamped to the view does not jitter, and the camera is exactly where it was when the shake ends — nothing to restore. And a click during a shake still lands on what is under the pointer, because `to_world()` converts through the same shaken camera that drew it. The drawn view is clamped to `limits` like the position is, so a shake near the edge of the level shakes inwards and never shows the void.

Shaking again while a shake is running keeps the STRONGER of the two — measured against what the running one is doing now, not what it started at — rather than adding them, so a burst of hits cannot build a shake that throws the screen across the room. A short strong hit on a long shake lasts at least as long as the long one had left. A strength or duration that is not a positive finite number is ignored, and one that is not a finite number at all is said once.

### `shake_offset`

Vector2 shake_offset() const

function [include/rmp/scene.h:158](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L158)

How far the shake asks to move the view right now; {0,0} when still. Near a limit less of it shows: `raylib()` clamps the result.

### `view`, `to_screen`, `to_world`

Rectangle view() const

Vector2 to_screen(Vector2 world) const

Vector2 to_world(Vector2 screen) const

function [include/rmp/scene.h:165](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L165)

What is visible, in world units, ignoring rotation: the game's area of the window divided by the zoom and the stretch's scale. The window under NONE; the design under LETTERBOX, whatever the window; under EXPAND the design and more, the way the window is longer than the design's shape — taller on a 4:3 monitor, wider on a 21:9 one.

### `raylib`

Camera2D raylib() const

function [include/rmp/scene.h:172](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/scene.h#L172)

The raylib camera this is, for `BeginMode2D()` in your own drawing. The stretch is in it: `offset` is the middle of the game's area, and `zoom` is this zoom times the stretch's scale.
