Engine.js performs axis-aligned bounding box (AABB) collision detection every frame for all figures that carry aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/OmarMtya/enginejs-module/llms.txt
Use this file to discover all available pages before exploring further.
Rigido component. The engine handles ground contact, figure-to-figure stacking, and velocity transfer automatically — your job is to configure each figure’s Rigido correctly and read the resulting state.
The Tocando function
All collision checks ultimately delegate to the Tocando(a, b, circulo, calcularGravedad) utility in utilidades.class.js. You will rarely call it directly, but understanding it explains every behaviour described below.
| Parameter | Type | Description |
|---|---|---|
a | Figura | The figure being tested (the “mover”). |
b | Figura | The figure it is tested against (the “obstacle”). |
circulo | Boolean | When true, figure b is treated as a circle: the Y-axis overlap check uses b.transform.altura * 2 instead of b.transform.altura to account for the circular bounding area. |
calcularGravedad | Boolean | Defaults to true. When true, the current rigido.valor (gravity velocity) is subtracted from the overlap bounds so the engine can predict the next frame’s overlap, not just the current one. |
Tocando returns false immediately — without doing any geometry — if either figure lacks a Rigido component or if either figure has sinColision = true.
fixGravedad offset (equal to a.rigido.valor when calcularGravedad is true) means the engine looks one gravity-step ahead, preventing figures from tunnelling through thin platforms at high velocities.
Per-frame collision pipeline
Each call tocalcularSiguientePaso() (triggered by Dibujar(false) inside the animation loop) runs the following pipeline:
Sort figures by Y descending
Environment.figuras is cloned and sorted so figures with a larger transform.y value (closer to the bottom of the canvas) are processed first. This prevents stacking bugs where a figure resolves its collision before the figure below it has moved.Figure-to-figure collision: tocandoRigidos()
For every figure that has a
Rigido and does not have sinColision = true, tocandoRigidos() is called. It checks all other rigid, collision-enabled figures for overlap and repositions this figure if needed.Figure-to-figure collisions: tocandoRigidos()
When Tocando returns true and the obstacle figure’s transform.y is greater than this figure’s transform.y (i.e. the obstacle is below), the engine:
- Repositions this figure so its bottom edge sits exactly on top of the obstacle’s top edge.
- Transfers velocity: the obstacle’s
rigido.valoris set to this figure’s current gravity velocity (simulating an impact impulse). - Resets gravity: this figure’s
rigido.valoris reset toEnvironment.gravedad(default9.8). - Sets
rigido.colision = trueon this figure. - Sets
rigido.gravedadReiniciada = trueto prevent an erroneouscolision = falseon the very next tick after the reset.
rigido.colision is set to false — two figures eating into each other from above do not count as a supported collision.
The
gravedadReiniciada flag is a one-tick guard. After a gravity reset, the engine skips setting colision = false on the immediately following frame. Without it, the collision state would flicker on the very tick the gravity is reset, causing visible jitter.Ground collision: tocandoFondo()
tocandoFondo() computes the maximum allowable transform.y position as:
transform.altura. When transform.y >= fondo, the figure is clamped and rigido.colision is set to true. Otherwise rigido.colision is set to false.
Opting out: sinColision
Pass sinColision: true as the second argument to Rigido to make a figure fall freely without participating in any collision resolution. It will still be affected by gravity (via afectarGravedad()), but it passes through every other figure.
Worked example: stacked boxes
The following scene creates a static platform and a falling box that lands on top of it.box lands on floor, box.rigido.colision will be true every frame it remains resting there. You can poll this property at any time:
Collision sound triggers
When a figure has aSonido attached with activacion: 'colision', the engine automatically plays the sound while rigido.colision === true and pauses it when the figure is airborne. Use activacion: 'colisionInversa' to invert this behaviour — the sound plays while the figure is not colliding.
Related pages
- Animation Loop — how
calcularSiguientePasois called each frame - Physics concepts —
Rigidoproperties in depth - Figures — constructing and adding figures to the scene
- Rigido reference — full property list