# Camera

> rmp::Camera decides which part of a scene's world is on screen: following an object, smoothing that is the same at any frame rate, limits, zoom, shake, and converting between world and screen.

Source: https://omardev29.github.io/rmp-docs/manual/core/camera.html

Every scene has a [`camera`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#camera), an [`rmp::Camera`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html), which decides what part of the scene's world is on screen. Untouched, it is the identity while the window is the design size, and on a window of any other size it fits the world the way `[window] stretch` says; a level bigger than the screen is three lines.

```cpp
auto &player = spawn({ .position = { 400, 300 }, .size = { 16, 24 } });
camera.follow = player.handle();
camera.smoothing = 10;
camera.limits = map.bounds();      // never show the void outside the level
```

## Where it starts

The camera starts centred on the design resolution — half of `[window] width` and `height` — with no zoom and no rotation. At the design size that is the identity: a world unit is a pixel, and an object at `{ 100, 50 }` is drawn 100 pixels from the left and 50 from the top, as raylib would draw it.

| Field | Default | Is |
|---|---|---|
| [`position`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#position) | the centre of the design resolution | The world point at the centre of the view. |
| [`zoom`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#zoom) | `1` | 2 shows everything twice as big. Zero or less counts as 1. |
| [`rotation`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#rotation) | `0` | Degrees, clockwise, as raylib's `Camera2D` counts them. |
| [`follow`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#follow) | empty | A handle to the object to keep centred. |
| [`smoothing`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#smoothing) | `0` | How fast it catches up with that object, per second; 0 snaps. |
| [`limits`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#limits) | empty | A rectangle of the world the view never leaves. |

## What it frames

Each frame, the scene draws its map and its objects through its camera, and then runs its own `_draw()` outside it, in screen pixels. So everything in the world moves with the view, and a HUD or a pause menu drawn in a scene's `_draw()` stays where it is while the player moves — which is why [the interface](https://omardev29.github.io/rmp-docs/manual/modules/ui.html) in a scene's `_draw()` never drifts with the camera. An object's own `_draw()`, and its behaviors', run inside the camera, in world units like the object.

### The window is not the design size

The window can be resized, and a phone's screen is whatever it is. How the world fits a window that is not the design size is [`[window]
stretch`](https://omardev29.github.io/rmp-docs/manual/core/app.html#a-window-of-another-size)'s to say, and the camera is where it happens. What the camera shows, [`view()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#view), is the game's area of the window divided by the zoom and the stretch's scale, centred on `position`:

| `stretch` | `view()` is |
|---|---|
| `"none"` | The window, at 1:1: a bigger window shows more of the world, not a bigger picture of the same part. |
| `"letterbox"` | The design, whatever the window: the same slice of the world for every player, scaled to fit, with bars around it. |
| `"expand"` | The design and more, the way the window is longer than the design: taller on a 4:3 monitor, wider on a 21:9 one. |

The camera puts `position` in the middle of the game's area — the window's middle, unless `letterbox` left bars — and `to_screen()`, `to_world()` and the pointer carry the stretch with them, so nothing in a scene asks which mode is on.

`zoom` goes on top of the stretch: under `letterbox` and `expand` the design is scaled into the window first, and a zoom of 2 is twice as big as the stretch drew it. So a game that wants a slice of the world of its own size — 225 world units of height, say, for pixel art drawn two screen pixels to a pixel of the art — sets a constant zoom once and leaves the window to the stretch. Under `expand` it never shows fewer than those 225 units of height, and shows more where the window is longer:

```cpp
rmp::app::set_stretch(rmp::app::Stretch::EXPAND);
camera.zoom = RMP_WINDOW_HEIGHT / VIEW_HEIGHT; // 225 world units of height at the design size
```

That is [the platformer](https://omardev29.github.io/rmp-docs/examples/games/07_platformer.html)'s camera. Working the zoom out from the window every frame, from `GetScreenHeight()`, is the stretch's job already: under `letterbox` or `expand` it would be done twice, and the world drawn too big.

Under `letterbox` the map and the objects are cut to the game's area, so the bars keep the clear colour whatever stands past the edge of the design, and a pointer on a bar is outside the world. The scene's own `_draw()` is not cut: the interface lays itself out inside the area on its own, and something a scene draws over the whole window, a veil, covers the bars as well.

Carefulraylib has one scissor, not a stack of them. An object that calls `BeginScissorMode()` and `EndScissorMode()` in its own `_draw()` ends the letterbox's cut with them, for the rest of its scene's world.

[The stretch example](https://omardev29.github.io/rmp-docs/examples/scenes/02_stretch.html) is a court drawn in world units, in all three modes, with a key and a button to go round them; resize the window and the camera, the pointer and the interface follow, with nothing in the scene asking which mode is on. On Android and on a Linux console every mode is a letterbox: see [a window of another size](https://omardev29.github.io/rmp-docs/manual/core/app.html#a-window-of-another-size).

## Following

With [`follow`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#follow) set to an object's handle, the camera centres on that object every frame — after the objects have moved and the collision pass has had its say, so it frames where things ended up and not where they were heading. It is a handle, so a target that is destroyed simply stops being followed, and the camera stays where it was. An empty handle leaves the camera wherever you put it.

Changing `follow` to a different object snaps to it instead of gliding across the level: a scene that opens with the camera drifting in from the middle of the screen is the other half of the classic smoothing bug.

### Smoothing is a rate

`smoothing` is how fast the camera catches up, as a rate per second: 0, the default, is a snap, which is what a one-screen game wants; 5 is a gentle drift; 20 is nearly a snap.

It is a rate and not the fraction-per-frame that tutorials write — `position += (target - position) * 0.1f` every frame — because that version depends on the frame rate: at 120 frames a second it takes twice as many steps a second, and follows twice as fast as at 60. Here the fraction of the distance covered in a frame is `1 - exp(-smoothing × delta)`, with the frame's time in the exponent, so two frames of half the time land exactly where one frame of the whole time does, and the camera feels the same at 30 frames a second and at 144.

## Limits

[`limits`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#limits) is a rectangle of the world the view never leaves. Near the edge of the level the camera stops and the player keeps going, instead of the screen showing a black strip past the end of the map. Limits win over following, and over smoothing too: they are applied after both, so a smoothed camera still never shows the void.

```cpp
camera.limits = map.bounds();                          // the level, whatever its size
camera.limits = { 0, 0, 0, RMP_WINDOW_HEIGHT };        // a runner: free across, pinned top to bottom
```

Each axis is limited on its own, and a zero width or a zero height means that axis is not limited at all — which is exactly what [an endless runner](https://omardev29.github.io/rmp-docs/examples/games/05_endless_runner.html) wants. A level narrower than the view, on an axis, centres the view on it rather than showing the void on one side. The limits apply to a `position` you write by hand as well, so a camera you move yourself — a title screen panning across the level — needs no arithmetic about the window to stay inside it.

## Zoom and rotation

`zoom` scales the view about its centre: 2 shows half as much of the world, twice as big. Limits are worked out on the zoomed view, so a camera zoomed in can travel closer to the edge.

`rotation` turns the picture, in degrees clockwise. `view()` and `limits` ignore it: they describe the unrotated rectangle, and a rotated view can show a little past the limits at its corners.

## Shake

```cpp
camera.shake(8, 0.35f);   // up to 8 world units, fading out over 0.35 seconds
```

That is [Breakout](https://omardev29.github.io/rmp-docs/examples/games/02_breakout.html)'s, when a ball is lost.

[`shake(strength, seconds)`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#shake) shakes the view by up to `strength` world units, fading to nothing over `seconds`, 0.3 if you leave it out: a hit, an explosion, a landing. The fade goes with the square of the time left, so it reads as a jolt that settles rather than a buzz that stops, and the motion is a fixed pattern of waves rather than random numbers, which would change direction every frame and look like a broken monitor.

The shake never touches `position` or `view()`. It is an offset applied only to what is drawn, and to the two conversions below — so an object clamped to the view does not jitter, there is nothing to restore when it ends, and a click during a shake still lands on what is under the pointer, because the pointer is converted through the same shaken camera that drew the frame. The drawn view is kept inside `limits` too, so a shake near the edge of the level shakes inwards.

Shaking again while a shake is running keeps the stronger of the two, measured against what the running one is doing now rather than what it started at, instead of adding them up: a burst of hits in one frame cannot build a shake that throws the screen across the room. A short strong hit on top of a long shake lasts at least as long as the long one had left. A strength or a duration that is not a positive number is ignored. [`shake_offset()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#shake_offset) is how far the shake is moving the view right now.

The camera is moved, smoothed and shaken in its scene's update, so the camera of a scene frozen under a pause menu stands still, shake included, until the scene runs again.

## World and screen

| Call | Gives |
|---|---|
| [`to_world(screen)`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#to_world) | The world point under a point of the screen, in pixels from its top-left corner. |
| [`to_screen(world)`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#to_screen) | Where a world point is on the screen. |
| [`view()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#view) | The rectangle of the world that is visible, ignoring rotation. |
| [`raylib()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Camera.html#raylib) | The raylib `Camera2D` this camera is, shake and stretch included, for `BeginMode2D()`. |

The pointer is the commonest thing to convert, and it is already done: [`rmp::input::pointer()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-input.html) is the pointer in world units, through the current scene's camera, ready to compare with an object's position. An object's `on_click` is tested the same way. The other direction places something drawn in screen space over something in the world — a marker over the player's head, in the scene's `_draw()`:

```cpp
rmp::Handle<rmp::Object> _player;

void _draw() override {
    if (!_player) return;
    const Vector2 above = camera.to_screen({ _player->position.x, _player->position.y - 30 });
    DrawText("!", static_cast<int>(above.x), static_cast<int>(above.y), 20, YELLOW);
}
```

A box of the interface over a point of the world — a health bar, a name, a button over a door — is [`rmp::ui::at()`](https://omardev29.github.io/rmp-docs/manual/modules/ui.html#boxes-over-the-world), which goes through the camera by itself.

### Drawing in world units yourself

A scene's `_draw()` is in screen pixels. To draw part of it in the world instead — a net across a court, a line from the player to its target — open the camera for the lines that need it:

```cpp
void _draw() override {
    BeginMode2D(camera.raylib());
    DrawLineV({ 400, 0 }, { 400, 450 }, GRAY);    // in the world, moving with the view
    EndMode2D();

    DrawText("SCORE", 10, 10, 20, WHITE);         // on the screen, staying put
}
```

Drawing world things without it is right only while the window is the design size and the camera has not moved, which is exactly when the difference does not show — and on the first other window, the net ends up somewhere the court is not. [Pong](https://omardev29.github.io/rmp-docs/examples/games/01_pong.html) draws its net this way for that reason.

## What reads the camera

Besides drawing, five things in the framework take the camera into account, so they follow it without anything written:

- an object's `edges`, when its `bounds` are empty and the scene has no map, use the camera's view as the edge of the world;
- `on_click` and `on_drag` find the object under the pointer through it, and `on_drag` hands over the movement through it, in world units, so a dragged object stays under the finger at any zoom;
- `rmp::input::pointer()` answers in world units through it;
- the [`Parallax`](https://omardev29.github.io/rmp-docs/reference/behaviors/Parallax.html) behavior scrolls each layer by how far the view has moved;
- `rmp::ui::at()` puts its boxes where the camera shows the points they follow.

Each scene has a camera of its own. A pause menu pushed over a level draws through its own camera, which nobody has moved, while the level underneath keeps its framing.
