RigidBody

The RigidBody component enables physics movement for an entity, making it move under velocity, acceleration, and gravity. It works together with a collider on the same entity. See Physics for an overview.

Body types

TypeDescription
RigidBodyType.DynamicAffected by gravity and velocity, and moved by collisions with other bodies. For objects that need full physical behavior.
RigidBodyType.KinematicMoved by applied velocity, but unaffected by gravity or collisions from other bodies. For moving platforms or scripted movement.
RigidBodyType.StaticImmobile. Unaffected by velocity or gravity. For walls and level geometry.

Options

OptionTypeDefaultDescription
typeRigidBodyTypeDynamicThe body type.
velocityVector2(0, 0)Velocity in pixels per second. For Dynamic and Kinematic bodies.
accelerationVector2(0, 0)Acceleration in pixels per second squared. For Dynamic and Kinematic bodies.
gravitynumber0Gravity in pixels per second squared. For Dynamic bodies only.
staticForLayersstring[][]Collision layers for which this body behaves as static (see below).

Static for layers

When two Dynamic bodies collide, both are repositioned by half the penetration. When a Dynamic body collides with a Static one, only the Dynamic body is repositioned.

staticForLayers lets a Dynamic body behave as static toward specific layers. When a Dynamic body whose collider layer is listed in staticForLayers collides with this body, only the other body is repositioned — this one stays in place, as if it were static. For every other collision, the body still behaves as its own type.

For example, a moving platform that should push the player but never be pushed back can list the player’s collision layer:

new RigidBody({ type: RigidBodyType.Dynamic, staticForLayers: ["Player"] });

Example

import { Transform, BoxCollider, RigidBody, RigidBodyType } from "angry-pixel";

this.entityManager.createEntity([
    new Transform(),
    new BoxCollider({ width: 16, height: 16, layer: "Player" }),
    new RigidBody({ type: RigidBodyType.Dynamic, gravity: 1000 }),
]);