Engine.js ships a lightweight physics model built around a single component:Documentation 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. Attaching a Rigido to a Figura opts that figure into the simulation — gravity pulls it downward each frame, it detects when it reaches the canvas floor, and it resolves overlaps with other rigid figures. Figures without a Rigido are completely inert and serve as pure visual elements.
The Rigido Component
The starting gravity strength for this figure, measured in pixels per frame. This value grows slightly each frame, producing the characteristic acceleration of a falling object. It is also used as the initial velocity value when another figure lands on top of this one.
When
true, the figure is subject to gravity but is excluded from all collision resolution. It falls through floors and other rigid bodies without interacting with them. Useful for decorative particles or pass-through effects.Internal State Properties
These are set automatically by the engine; you can read them but generally should not write them directly.A copy of
valor taken at construction time. Stored for potential future use in restoring the original gravity strength.true when the figure is currently resting on the canvas floor or on top of another rigid figure. The engine uses this flag to suppress gravity for that frame so the figure does not pass through the surface it is resting on.Initialised to
null at construction time. Reserved as an internal tracking field on the Rigido component. Note that the tocadoPor reference used in collision resolution (the figure that landed on top of another) is stored directly on the Figura instance, not here.An internal flag that the engine sets to
true when a collision resets valor back to Environment.gravedad. On the very next tick the engine reads this flag, clears it, and also clears colision — this one-frame delay prevents physics glitches when two figures bounce against each other.How Gravity Works
Every frame, for each figure that has aRigido and is not currently in collision, the engine calls figura.afectarGravedad():
valor grows by a fraction of itself every frame, the figure speeds up continuously — just like free-fall. At the default of 60 FPS and valor = 9.8, the increment per frame is tiny (9.8 / 6000 ≈ 0.0016), but it compounds over hundreds of frames into a convincing drop.
Floor Collision
Each frame, afterafectarGravedad() is called, the engine calls figura.tocandoFondo(). This method computes the floor boundary:
- For ordinary figures:
fondo = Environment.altura - transform.altura - For sprite figures:
fondo = Environment.altura - sprite.altura
transform.y >= fondo, the figure is clamped to fondo and rigido.colision is set to true. On the next frame, gravity is skipped for that figure. If the figure is no longer at the boundary, colision is reset to false and gravity resumes.
Rigid-Body Collision
figura.tocandoRigidos() is called every frame (before afectarGravedad) for each figure that has a Rigido with sinColision = false. It filters Environment.figuras down to all other figures that also have a collision-eligible Rigido, then runs an AABB overlap test via the internal Tocando() utility.
When two figures overlap and the other figure’s transform.y is greater (i.e. it is below the current figure in canvas space):
- The current figure is repositioned so its bottom edge sits flush with the top of the lower figure.
rigido.colisionis set totrue, suppressing gravity for that frame.- The lower figure’s
tocadoPorproperty is set to the current figure (the impacting one). - The lower figure inherits the current figure’s
rigido.valor(velocity transfer on impact), simulating a push-down effect. - The current figure’s
rigido.valoris reset toEnvironment.gravedad, andgravedadReiniciadais flagged so the engine clearscolisionon the following tick.
Figures are sorted by Y-axis (descending — highest Y first) before collision resolution runs each frame. This ensures that the figure closest to the floor is resolved first, cascading the physics upward through any stack of objects correctly.
Creating a Static Platform
A figure whoseRigido has valor = 0 does not accelerate — gravity cannot speed up something that starts at zero. Other falling figures still collide with it and land on top of it normally.
Pass-Through Figures (sinColision)
Setting sinColision = true makes a figure fall under gravity but skip all collision checks against other rigid bodies. It will also not be a valid landing target for other figures — Tocando() returns false for any pair where either side has sinColision = true.
Resetting the Simulation
When you call$g.DetenerAnimacion(), the engine cancels the animation frame, restores Environment.figuras from the backup snapshot taken at the start of Animar(), and resets the animando flag. Every figure reverts to its original position, and all Rigido values — including valor and colision — return to their initial state.
Related
Figures
How Rigido attaches to a Figura.
Environment
Set global gravity before starting the loop.
Collision Detection Guide
Practical patterns for reading and reacting to collisions.
Rigido API Reference
Complete API reference for the Rigido class.