rmp::Object
An entity in a scene: the player, an enemy, a bullet, a coin, a platform. Scene::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
THE CENTRE. See the header comment.
scale
Multiplies the shape, the collider and the sprite
rotation
Degrees, as raylib counts them
velocity
Integrated every frame, by us
mass
Only apply_force / apply_impulse read it
Appearance
sprite
If there is a sprite, the sprite is drawn
shape
Otherwise this is
flip_x
Mirrors the sprite left to right
flip_y
Mirrors the sprite top to bottom
visible
False: not drawn, but still updated and collided
layer
Draw order; ties break on creation order
World
gravity_scale, edges, bounds
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
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.
player.sprite.sheet = sheet; // drawn as the picture
player.collider = rmp::rect({ 20, 44 }); // collided as a boxsolid, immovable
Three fields, three different questions, and keeping them apart is what makes the common case free:
collider what shape? deduced from shape/sprite
solid resolve, or notify? false: notify only
immovable who gets moved? false: this one doessolid 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:
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
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.
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 playercollision_layer: Which layers I am ON
collision_mask: Which layers I collide WITH
What you override, all of it empty by default
_ready
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
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
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
The difference between these two is the whole physics API:
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
A behavior is ANY struct with void _update(rmp::Object &, float). It inherits from nothing, has no virtuals and registers nowhere:
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:
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:
_update(Object &, float) every frame, BEFORE the integrator
_late_update(Object &, float) every frame, AFTER itOne of those two is required and the rest are optional.
_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 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
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:
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
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.
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:
piece.on_drag([](rmp::Object &self, Vector2 moved) {
self.position.x += moved.x;
self.position.y += moved.y;
});Identity and life
handle
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
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().
world_bounds
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
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
Deferred: the object stops updating and drawing IMMEDIATELY, and its memory is released after the frame. Calling it twice is harmless.
alive
False from the moment destroy() is called, not from the moment the memory goes away.