Skip to content

Rigid Body Rectangle Component

The Rigid Body Rectangle Component creates dynamic physics bodies using Matter.js. These are movable objects that respond to forces, collisions, and physics simulation.

Purpose

The Rigid Body Rectangle Component handles:

  • Dynamic physics simulation
  • Collision response and bouncing
  • Force and velocity application
  • Physical material properties

Properties

Required Properties

PropertyTypeDescription
xnumberX position of the rigid body center
ynumberY position of the rigid body center
widthnumberWidth of the rectangular body
heightnumberHeight of the rectangular body

Optional Properties

PropertyTypeDefaultDescription
velocity{ x: number, y: number }{ x: 0, y: 0 }Initial velocity
angularVelocitynumber0Initial rotational velocity (rad/s)
rotationnumber0Initial rotation in radians
restitutionnumber0.8Bounce factor (0 = no bounce, 1 = perfect bounce)
frictionnumber0.001Surface friction coefficient
frictionAirnumber0.01Air resistance/drag
densitynumber0.001Mass density (affects weight)

Usage Examples

Basic Falling Box

typescript
const rigidBodyData: RigidBodyRectangleComponentData = {
  x: 200,
  y: 100,
  width: 50,
  height: 50
};

Bouncy Ball Simulation

typescript
const rigidBodyData: RigidBodyRectangleComponentData = {
  x: 300,
  y: 200,
  width: 30,
  height: 30,
  restitution: 0.95, // Very bouncy
  frictionAir: 0.001, // Low air resistance
  density: 0.002 // Light weight
};

Heavy, Slow Object

typescript
const rigidBodyData: RigidBodyRectangleComponentData = {
  x: 400,
  y: 150,
  width: 80,
  height: 60,
  velocity: { x: -50, y: 0 }, // Moving left
  restitution: 0.2, // Low bounce
  friction: 0.8, // High friction
  frictionAir: 0.05, // High air resistance
  density: 0.01 // Heavy
};

Spinning Object

typescript
const rigidBodyData: RigidBodyRectangleComponentData = {
  x: 250,
  y: 300,
  width: 40,
  height: 40,
  angularVelocity: 0.1, // Rotating
  rotation: Math.PI / 4 // 45-degree initial rotation
};

Runtime Properties

The created component includes all the above properties plus:

  • _body: The Matter.js Body object configured as dynamic

Physics Properties Explained

Restitution (Bounce)

  • 0.0: No bounce (completely inelastic)
  • 0.5: Moderate bounce
  • 1.0: Perfect bounce (no energy loss)
  • > 1.0: Gains energy (not realistic but fun!)

Friction

  • 0.0: No friction (slides freely)
  • 0.5: Moderate friction
  • 1.0: High friction (sticky surface)

Density

  • 0.001: Very light object
  • 0.01: Heavy object
  • Higher values = more mass = harder to accelerate

Air Resistance

  • 0.001: Minimal drag (space-like)
  • 0.01: Normal air resistance
  • 0.1: High drag (underwater-like)

Physics Behavior

  • Dynamic: Bodies move and rotate based on forces and collisions
  • Gravity: Affected by world gravity (if enabled)
  • Collisions: Responds to collisions with other bodies
  • Forces: Can have forces applied for movement and effects

Notes

  • Position coordinates represent the center of the rectangle
  • Velocity is in pixels per second
  • Angular velocity is in radians per second
  • Bodies automatically respond to gravity and forces
  • Higher density objects require more force to accelerate
  • Combine with sprite components for visual representation

Released under the Academic License.