# rmp::Object

> An entity in a scene: the player, an enemy, a bullet, a coin, a platform.

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

class `#include <rmp/object.h>` [include/rmp/object.h:629](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L629-L939)

class Object

An entity in a scene: the player, an enemy, a bullet, a coin, a platform. [`Scene::spawn()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#spawn) makes one and the scene owns it; its public fields are the whole of its configuration, read and written directly.

Derive from it to override the hooks (_ready, _update, _draw, _collision, _end), add behaviors to it with `add()`, or hand it functions with `on_click()`, `on_drag()` and `on_collision()`.

## Transform

### `position`

Vector2 position{}

field [include/rmp/object.h:645](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L645)

THE CENTRE. See the header comment.

### `scale`

Vector2 scale{ 1, 1 }

field [include/rmp/object.h:646](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L646)

Multiplies the shape, the collider and the sprite

### `rotation`

float rotation = 0

field [include/rmp/object.h:647](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L647)

Degrees, as raylib counts them

### `velocity`

Vector2 velocity{}

field [include/rmp/object.h:648](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L648)

Integrated every frame, by us

### `mass`

float mass = 1.0f

field [include/rmp/object.h:649](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L649)

Only apply_force / apply_impulse read it

## Appearance

### `sprite`

Sprite sprite

field [include/rmp/object.h:652](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L652)

If there is a sprite, the sprite is drawn

### `shape`

Shape shape

field [include/rmp/object.h:653](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L653)

Otherwise this is

### `flip_x`

bool flip_x = false

field [include/rmp/object.h:654](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L654)

Mirrors the sprite left to right

### `flip_y`

bool flip_y = false

field [include/rmp/object.h:655](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L655)

Mirrors the sprite top to bottom

### `visible`

bool visible = true

field [include/rmp/object.h:656](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L656)

False: not drawn, but still updated and collided

### `layer`

int layer = 0

field [include/rmp/object.h:657](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L657)

Draw order; ties break on creation order

## World

### `gravity_scale`, `edges`, `bounds`

float gravity_scale = 0

Edge edges = Edge::NONE

Rectangle bounds{}

field [include/rmp/object.h:664](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L664)

0 means gravity does not touch this object, and that default is not negotiable: a top-down game cannot have things falling over. The scene carrying a downward gravity that nobody uses until they ask is what lets one Object serve a platformer and a Zelda.

`edges`: What happens at the edge of `bounds`

`bounds`: Empty = the map's bounds, or the view without a map

### `collider`

Shape collider

field [include/rmp/object.h:676](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L676)

What it collides AS. NONE means "the same as `shape`", or the sprite's frame when there is no shape — which is right until it is not, and the moment it is not is the most common step up in the whole API: a character's sprite includes hair, a cape and air, and nobody wants to collide with the air.

```cpp
player.sprite.sheet = sheet;                // drawn as the picture
player.collider = rmp::rect({ 20, 44 });    // collided as a box
```

### `solid`, `immovable`

bool solid = false

bool immovable = false

field [include/rmp/object.h:701](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L701)

Three fields, three different questions, and keeping them apart is what makes the common case free:

```text
collider   what shape?          deduced from shape/sprite
solid      resolve, or notify?  false: notify only
immovable  who gets moved?      false: this one does
```

`solid` is false by default because most objects in a 2D game are not walls — bullets, coins, particles, pickups, triggers — and the two that are are exactly the two you configure:

```cpp
ground.solid = true;  ground.immovable = true;
player.solid = true;  player.gravity_scale = 1;
```

Whoever is pushed apart loses the part of its velocity going INTO the contact, after the _collision hooks have run — so a box resting on another starts from rest when the one below moves away, a jump off it keeps its jump, and a hook that flips the velocity to bounce loses nothing.

The scene's map counts as ground too: an object that is solid and not immovable is stopped by the map's solid cells as it moves, each axis on its own, so it runs along a floor of tiles without catching on the seams.

### `collision_layer`, `collision_mask`

unsigned collision_layer = 1

unsigned collision_mask = 1

field [include/rmp/object.h:720](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L720)

Godot's model, because it is the right one. Two objects interact when either one's mask touches the other's layer. Both default to 1, so everything collides with everything and the simple case pays nothing — the Pong in 01-principles.md never mentions a layer.

Without them there are three things you cannot say, and they turn up in about the fourth game anybody writes: that the player's bullet must not hit the player, that enemies pass through each other but not through walls, and that a trigger sees the player and nothing else. The alternative is an `if` at the top of every _collision, which is the mechanical work this framework exists to absorb.

```cpp
namespace layer { constexpr unsigned PLAYER = 1 << 0, ENEMY = 1 << 1,
                                     BULLET = 1 << 2, WORLD = 1 << 3; }
bullet.collision_layer = layer::BULLET;
bullet.collision_mask  = layer::ENEMY | layer::WORLD;   // not the player
```

`collision_layer`: Which layers I am ON

`collision_mask`: Which layers I collide WITH

## What you override, all of it empty by default

### `_ready`

virtual void _ready()

hook [include/rmp/object.h:726](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L726)

Once, when `spawn()` has put the object in its scene and applied the options: the place to load what it draws and to add its behaviors.

### `_update`, `_draw`, `_end`

virtual void _update(float delta)

virtual void _draw()

virtual void _end()

hook [include/rmp/object.h:733](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L733)

Every frame the object is alive, after its behaviors' _update and before it is moved by its velocity. A new object's first _update depends on where in the frame it was spawned: before the object pass — from the scene's _update or an on_click — it is the same frame; during the pass — from an object's or a behavior's _update — or after it, from a _collision, it is the frame after.

`_draw`: The sprite and the shape draw themselves

`_end`: On destruction: drop loot, tell somebody

### `_collision`

virtual void _collision(Object &other)

hook [include/rmp/object.h:741](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L741)

Once per pair per frame, on BOTH objects: a touching b calls `a._collision(b)` and `b._collision(a)`. It fires whether or not either one is `solid` — solid decides whether they are pushed apart, not whether you are told.

## Forces

### `apply_force`, `apply_impulse`

void apply_force(Vector2 force)

void apply_impulse(Vector2 impulse)

function [include/rmp/object.h:757](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L757)

The difference between these two is the whole physics API:

```text
if you call it every frame it is a force; if you call it once it is an
impulse.
```

Both exist because using the wrong one gives exactly the bug you would expect. A force applied once barely moves anything, and an impulse applied every frame goes twice as fast at 120 fps as at 60.

Either one that is not a number — or that would leave the velocity past what a float holds — is refused and said once, and so is a frame that would: the object stops where it is rather than carrying a NaN into its position and from there into everything it touches.

`apply_force`: Sustained: velocity += f/mass*delta

`apply_impulse`: Instant: velocity += i/mass

## Behaviors

### `add`, `get`, `has`, `remove`

template <class B> B &add(B value = {})

template <class B> Ref<B> get() const

template <class B> bool has() const

template <class B> void remove()

function [include/rmp/object.h:807](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L807)

A behavior is ANY struct with `void _update(rmp::Object &, float)`. It inherits from nothing, has no virtuals and registers nowhere:

```cpp
struct Spin {
    float degrees_per_second = 90;
    void _update(rmp::Object &self, float delta) {
        self.rotation += degrees_per_second * delta;
    }
};
coin.add<Spin>({ .degrees_per_second = 180 });
```

That is the whole model, and the absence of a base class is not taste: a struct with a base and virtual functions stops being an aggregate in C++20, and `{ .speed = 320 }` no longer compiles against it. The usual workaround — a nested options struct and a constructor taking it — turns every later read into `get<TopDown>()->opts.speed`. Without inheritance the fields ARE the configuration, hot:

```cpp
if (auto move = player.get<rmp::behavior::TopDown>()) move->speed = 400;
```

get<B>() is an `rmp::Ref:` empty when the object has no B, and the `if` is how you ask.

And the part that was not aimed for and turns out to be the best of it: one of yours and one of ours are literally the same thing. There is no extension API to learn because there is no extension.

The optional hooks are detected at compile time with `requires`, so what you do not declare costs nothing:

```cpp
_update(Object &, float)            every frame, BEFORE the integrator
_late_update(Object &, float)       every frame, AFTER it
```

One of those two is required and the rest are optional.

```cpp
_ready(Object &)                    once, when it is added
_draw(Object &)                     after the object is drawn
_collision(Object &, Object &other) once per pair per frame
_end(Object &)                      when removed, or the object dies
```

_late_update is not decoration. Behaviors run before the velocity is integrated, because that is what lets one WRITE velocity — and a behavior whose job is to correct where the object ENDED UP therefore has nowhere to run. [`rmp::behavior::GridSnap`](https://omardev29.github.io/rmp-docs/reference/behaviors/GridSnap.html) is exactly that: it exists to leave a puzzle piece square with the grid, and rounding the position the object had before it moved would round the wrong number.

### `enable`, `enabled`

template <class B> void enable(bool on = true)

template <class B> bool enabled() const

function [include/rmp/object.h:825](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L825-L827)

Pauses a behavior without taking it off, and resumes it. A paused one keeps its fields and what it was keeping track of, and gets no hooks — no _update, _late_update, _draw or _collision — until it is enabled again; it still gets its _end when it is removed or the object dies. How an enemy stops patrolling while it chases:

```cpp
self.enable<rmp::behavior::Patrol>(false);
```

Nothing happens for an object without a B, and enabled<B>() is false for it. A behavior is enabled when it is added, and adding it again replaces it with an enabled one.

## Callbacks, for when a subclass is more than you want

### `on_click`, `on_drag`, `on_collision`

template <class F> void on_click(F &&fn)

template <class F> void on_drag(F &&fn)

template <class F> void on_collision(F &&fn)

function [include/rmp/object.h:854](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L854-L856)

The other half of the underscore rule: `_name` is yours and we call it, `on_name` is ours and you hand us a function. They coexist without ambiguity, which is the whole reason the rule exists — an object can override _collision AND carry an on_collision, and both run.

```cpp
coin.on_collision([&](rmp::Object &self, rmp::Object &other) {
    if (&other == &player) { score += 10; self.destroy(); }
});
```

Setting one twice replaces it. There is no list, because a list of handlers is a signal system, and that is a bigger idea than this needs.

on_drag is handed how far the pointer moved since the last frame while it holds this object, in WORLD units — through the scene's camera, so adding it to the position keeps the object under the finger at any zoom:

```cpp
piece.on_drag([](rmp::Object &self, Vector2 moved) {
    self.position.x += moved.x;
    self.position.y += moved.y;
});
```

## Identity and life

### `handle`

template <class T = Object> Handle<T> handle() const

function [include/rmp/object.h:866](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L866-L868)

`handle()` is the plain one and handle<Goblin>() is the typed one, which is what a behavior holding a target wants: `if (!target) return;` and then `target->hp` without a cast.

### `scene`

Scene &scene() const

function [include/rmp/object.h:872](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L872)

The scene that spawned it. An object no scene spawned — a member, a local, a test's — has none: asking is said once, and answers [`Scene::current()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#current).

### `world_bounds`

Rectangle world_bounds() const

function [include/rmp/object.h:879](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L879)

The axis-aligned box this object occupies right now, from the sprite or the shape. Empty when it has neither, which is what an invisible logic object is. With no `collider` and no `shape`, this box is what the object collides as, and an empty one collides with nothing. A shape's mirrored scale or negative size is still a size: the same box.

### `world_collider`

Rectangle world_collider() const

function [include/rmp/object.h:887](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L887)

The box the collider occupies right now: `collider` if it has one, else `world_bounds()`. For debug drawing and for a game doing its own query — the collision pass works with the precise shape and not with this box, so a circle collides as a circle. A shape whose box is not a finite number — a NaN or an infinity in its position, size or scale — is nowhere, and collides with nothing.

### `destroy`

void destroy()

function [include/rmp/object.h:891](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L891)

Deferred: the object stops updating and drawing IMMEDIATELY, and its memory is released after the frame. Calling it twice is harmless.

### `alive`

bool alive() const

function [include/rmp/object.h:895](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/object.h#L895)

False from the moment `destroy()` is called, not from the moment the memory goes away.
