Skip to content

Bouncing Balls (Real Physics)

Balls drop into a box, bounce off each other and the walls, roll and settle — an actual physics simulation.

Physics, D1 MotionAny rendererReal physics277 lines of codev1.0.0

In Science & maths, Toys, games & parties

Next: Domino Chain (Real Physics)Surprise me

Bouncing Balls (Real Physics) preview
Starting live preview…
00:00:00:00 / 00:00:00:01

Live, running the real source.

Your current settings go with it. Combine several clips and hand them over together from your sequence.

Effect controls

Presets

16
0.72

Low is heavy and dead; high is super-bouncy.

1
9.81

9.81 is Earth. Lower feels floaty, like slow motion.

7

Each number gives a different but repeatable drop.

The preview runs the same code your AI receives. These settings travel with the hand-off.

Info

What it is

A small, real rigid-body simulation: spheres with mass, bounciness and friction collide with each other and the walls of a box, spin as they roll, and come to rest. It is calculated once at a fixed step, then played back frame by frame, so the same seed always gives the same motion and video export is exact. The result is just numbers (positions and spins), so you can draw the balls any way you like: flat 2D, glossy 3D, chrome or glass.

Reuse it in your own idea

  • Draw the balls as chrome spheres inside a glass box in Three.js (see the Bouncing Marbles in a Box example).
  • Use positions to drive 2D emoji, logos or product photos falling into a pile.
  • Use only the first second (balls still in the air) for a drop-in transition.

Which part is reusable

src/bouncing-balls.js is the ingredient your AI reuses. demo.js is only the example scene around it, and uses the demo harness only for the demo.

See it combined with other ingredients in Bouncing Marbles in a Box, Bouncing Marbles in a Box.

Good to know

  • Spheres only, inside an axis-aligned box. Not a general physics engine.
  • During hard impacts balls can overlap by up to about 0.05 units for a step before correction (measured ≤ 0.05 with default sizes).
  • Slow contacts are treated as resting (no bounce) so piles settle instead of jittering.

Technical details

Technical details
Implementation
Plain JavaScript math — works with any renderer · ES module src/bouncing-balls.js
Exports
  • simulateBouncingBalls({ count?, seed?, box?, radius?, restitution?, friction?, gravity?, fps?, durationSeconds?, stepHz?, spawnHeight? }) => { count, radii, frameCount, positionAt(frame, i), quaternionAt(frame, i), positions, quaternions, box, stats }
  • rotateByQuaternion([x, y, z], [qx, qy, qz, qw]) => [x, y, z]
Dependencies (exact)
  • apw-runtime@1.0.0 (demo) — Demo harness and colour helpers. src/bouncing-balls.js has no dependencies.
How it fits a scene

Coordinates: Y-up. Box floor at y = 0, centred on x = 0, z = 0. Default inner box 4 × 3 × 4 units. Walls are infinitely tall.

Lighting: None (motion only).

Timing: Bake once with the clip's fps and duration, then look up by frame. Frames are random-access.

Output
1080×1080, 30 fps, 8 s · physics simulation, baked at a fixed step (deterministic) · not a loop — Physics runs forward and settles; it does not return to the starting state.
Tested
Rendered 240 frames on 2026-10-02 in Chrome 151.0.7922.137 headless on Linux 7.0.0-31-generic (x64), Node 22.14.0 (Canvas 2D). Same frame twice from fresh pages: identical.
Performance
Baking 8 s of 40 balls at 240 Hz takes about 50 ms in Node 22 on a server CPU. Playback is a lookup. Up to 60 balls. Collision checking is all-pairs, so cost grows with count².
Export a video yourself
Download the source, then in the folder: npm install npm run render -- bouncing-balls/1.0.0 --out my-video.mp4Needs Node 18+, ffmpeg and Chrome/Chromium. Add --params my-settings.json (from Copy settings) for your own settings.
Version
1.0.0 · sha256:c0a270f1cb930672… · other versions: 1.1.0
Files
License
MIT. Copyright (c) 2026 AI Pixel Wizard. Released under the MIT License (see LICENSE in this folder). License details
Provenance
Original code written for AI Pixel Wizard in October 2026.
BinMore like this

Coin Stack

3D object3DSeamless loop12 seconds

Jelly Cube

3D object3DSeamless loop10 seconds