Power-User Systems

Physics Engine

A from-scratch 2D rigid-body physics lab — fixed-timestep impulse solver, drag-to-throw bodies, force and velocity overlays, and a live energy readout on a blueprint grid.

Install

# PhysicsEngine needs no npm packages

Then copy 2 files into your project:

  • components/registry/physics-engine/physics-engine.tsx
  • hooks/use-animation-loop.ts

Props

PropTypeDefaultDescription
scene"playground" | "stack" | "wrecking-ball" | "projectile""playground"Preset world. Switching rebuilds the scene deterministically from a fixed seed.
gravitynumber9.8Uniform downward gravity. 0 is zero-g — throw bodies and watch them coast.
restitutionnumber0.6Bounciness of every collision, applied live to existing bodies. Impacts below 1 m/s are absorbed so stacks can settle.
frictionnumber0.4Coulomb friction coefficient for all contacts, applied live. 0 is ice; boxes need around 0.3 to stack.
timeScalenumber1Simulation speed. Physics always steps at a fixed 120 Hz — this scales how much simulated time each frame consumes. 0 freezes time.
bodyCountnumber14How many bodies the playground rains and how tall the stack and wrecking-ball towers build. Projectile ignores it.
sizeVariationnumber0.5Spread of playground body sizes — 0 rains uniform bodies, 1 mixes pebbles with boulders. Density is fixed, so bigger really is heavier.
showVectorsbooleanfalseVelocity (green, solid) and net-force (red, dashed) arrows anchored at each body's center of mass, scaled to magnitude.
trailsbooleantrueFading motion trails behind moving bodies — read parabolas and pendulum arcs at a glance.
showEnergybooleantrueLive HUD readout of kinetic, potential, and total energy in joules, computed from solver state every step — never faked.
tintstring (hex)"#a855f7"Accent color for dynamic bodies, trails, and the grab spring. Grid and chrome follow the theme.

What It Demonstrates

Drag
Moving an element by grabbing it, often with momentum when released.
Momentum
Motion that carries velocity, especially after a drag or interruption.
Velocity
How fast and in which direction an element is moving. A spring carries it into the next animation when interrupted, so a flicked element keeps its speed.
Mass
How heavy the animated element feels. More mass makes it slower and more sluggish.
Bounce
A spring that overshoots and settles, adding playfulness.
Frame rate (FPS)
Frames drawn per second. 60fps is the baseline for smooth motion; 120fps on newer displays.

Related Components

Appears In