Spheres bouncing in a box
With Gravity, Friction, and Air Resistance at zero and Elasticity near one, the spheres keep bouncing around an invisible box. Lower elasticity and the rebounds lose motion. Add gravity and the spheres fall. Negative gravity sends them upward.
Friction and air resistance act in different places. Air Resistance reduces the whole velocity every frame, wherever the sphere is. Friction reduces motion along a wall when the sphere hits it. The simulation has no spin, so that wall friction won't make a sphere roll.
The speed slider needs a reset
Speed sets the random velocities when particles are created. Change it, then press Reset to see the difference. Reset also picks new positions and colors.
Particle Count creates a new population immediately. Particle Size changes the existing spheres, but doesn't make them heavier: collisions treat every sphere as having equal mass. Debug Mode draws lines for resolved sphere collisions. Clicking doesn't add particles.
When two spheres overlap
The code checks every pair. If their centers are closer than their combined radii, it pushes each sphere back by half the overlap along the line between them. It then checks their relative velocity along that line. If they're approaching one another, an impulse changes their velocities in opposite directions, with elasticity setting the rebound.
Each sphere then gets gravity and air damping, advances by its velocity, and checks the walls. A wall crossing is moved back inside the box. The velocity perpendicular to the wall is reversed and scaled by elasticity; friction reduces the other two components.
The box extends ten scene units in each direction from the origin. Those aren't calibrated meters, and the updates happen per animation frame rather than on a fixed physical timestep. A faster display can change the apparent motion. A fast sphere can also pass through another between overlap checks, because the code doesn't calculate the exact moment of contact. Random starting positions can overlap too.
Those shortcuts leave you with a way to compare gravity, damping, and collisions, rather than predict a real machine's motion. The particle pulse animation takes a different approach: paths and random turns, with no collisions at all.