Particle Forces: Gravity & Electromagnetism
Every particle here carries two properties: a mass, which determines how strongly gravity pulls it toward every other particle, and a charge, which determines how strongly it pushes or pulls via the electromagnetic force. Gravity is always attractive. Electromagnetism is attractive between opposite charges and repulsive between like charges. Give particles both properties at once and you get motion neither force produces alone—orbits that spiral, pairs that repel until gravity finally wins, three-body chaos with a electromagnetic twist.
There is no closed-form solution once you have more than two bodies. Instead, the simulation does what physics engines actually do: compute the net force on every particle from every other particle, then take a very small step forward in time. Repeat sixty times a second and the trajectory emerges from nothing but pairwise forces and Newton’s second law.
Try It
Click and drag on the canvas to place a new particle—the drag direction and length set its launch velocity, like a slingshot. Adjust mass and charge with the sliders before placing. Click an existing particle (without dragging) to remove it. Try the presets below for configurations that are already balanced into orbits.
The Physics
Two inverse-square laws govern this simulation. Newton’s law of gravitation says every pair of masses attracts:
$$\vec F_g = \frac{G m_1 m_2}{r^2}\,\hat r$$
Coulomb’s law says every pair of charges pushes or pulls depending on sign:
$$\vec F_e = \frac{k_e q_1 q_2}{r^2}\,\hat r$$
Both are central forces that fall off as $1/r^2$, which is what lets them combine into a single pairwise update. For particles $i$ and $j$ separated by $\vec r_{ij} = \vec r_j - \vec r_i$, the net force on $i$ from $j$ is:
$$\vec F_{ij} = \frac{G m_i m_j - k_e q_i q_j}{r^3}\,\vec r_{ij}$$
When the gravitational term dominates, the pair attracts regardless of charge. When like charges are large enough, the $k_e q_i q_j$ term flips the sign and the pair repels instead—this is exactly how the simulation produces both orbiting pairs and particles that fly apart from the same piece of code.
Numerical Integration
With three or more bodies, these equations have no closed-form solution (this is the same n-body problem behind the Millennium Prize era work on orbital dynamics). The simulation sums the pairwise forces on every particle each frame, divides by mass to get acceleration, then advances with semi-implicit (symplectic) Euler integration—update velocity from the current acceleration, then update position from the new velocity:
$$\vec v_i \leftarrow \vec v_i + \vec a_i \, \Delta t \qquad\qquad \vec r_i \leftarrow \vec r_i + \vec v_i \, \Delta t$$
This is the same family of method described in the numerical methods article—simpler than Runge-Kutta, but symplectic integrators like this one conserve energy far better than plain (explicit) Euler over long orbital runs, which is why orbits here stay roughly circular instead of slowly spiraling into the void.
One more detail matters: as $r \to 0$, both force laws blow up toward infinity, which would send close-passing particles flying off at absurd speed. The simulation adds a small softening length $\epsilon$, replacing $r^2$ with $r^2 + \epsilon^2$ in every force calculation. This caps the force at close range—a standard trick borrowed directly from real astrophysical n-body simulations, where softening prevents stars from unphysically slingshotting on close encounters.
What to Try
- Binary Orbit: Two equal masses, tuned to a stable circular orbit around their shared center. Pure gravity, no charge.
- Charged Pair: Same setup, but attraction now comes entirely from opposite charges instead of mass—electromagnetism can produce the same orbital shapes gravity does.
- Three-Body: Three masses in a triangle. Even small asymmetries eventually break the pattern into something unpredictable.
- Mixed Chaos: A cluster with random mass and charge. Watch how same-charge particles get flung to the edges while massive neutral ones sink toward the center.
- Build your own: Set charge to a large negative value, mass low, and drop several particles near each other with no initial velocity—watch them scatter on pure repulsion, then dial charge back toward zero and increase mass to watch gravity take back over.