×
The Physics of Magneticolors
A real-time N-body physics engine blending concepts from molecular dynamics and astrophysics to create swarm-like clustering behaviors.
1. Color as Magnetic Charge
Every ball is assigned a color value on a continuous looping spectrum. Similar colors attract; distant colors repel. The magnitude of attraction or repulsion scales with how close or far apart two colors are β this is the fundamental force driving all emergent behavior.
2. N-Body Gravity & Force Softening
The engine calculates interactions between every possible pair of balls. Force follows an Inverse-Square Law (Force ∝ 1/distanceΒ²). A softening parameter prevents the force reaching infinity at zero distance, keeping the math stable when balls are very close.
3. Dynamic Mass & Inertia
When similar-color balls cluster tightly, they accumulate "Cluster Mass." Heavier clusters pull distant balls more strongly while also resisting being pushed β exactly like gravitational inertia. Mass is capped to keep multiple factions alive rather than everything collapsing into one body.
4. Hard-Sphere Collisions
When two balls overlap, the engine applies a positional correction and an impulse exchange. The low elasticity (bounce = 0.2) bleeds kinetic energy on each collision, allowing clusters to form and hold rather than scattering.
5. Curved Corner Boundaries
Straight walls use flat kinematic reflection. The four corners use a curved boundary β if a ball enters a corner, the engine calculates the curve's normal vector for a smooth sliding escape, preventing clusters from getting permanently trapped.
6. Interactive Disruptions
NUDGE: Globally scatters all balls and resets cluster mass β forcing the system to reform from scratch.
Canvas Click / Tap: A localized radial shockwave at your cursor shatters nearby clusters on demand.
7. Presets
CALM: Few balls, few color factions, gentle friction, long trails. Distinct color tribes slowly coalesce and orbit.
STORM: Dense swarm, many factions, near-frictionless. Complex turbulent interactions with fast micro-clustering.
8. Toggles
Center Gravity: A gentle spring pull toward the canvas center keeps clusters interacting rather than dispersing to edges.
Color Drift: Each ball's color value slowly wanders over time β alliances shift as balls drift toward or away from their faction's color.