## Bouncing Back

I need to retract the mathematics in my previous post. A few simulations revealed that large masses were getting bounced around a bit too much, and tiny masses were exceptionally steady, hardly budging as big balls pounded them from all sides. Clearly, something was wrong.

I was right to be skeptical of the cubic mass, but wrong to trust my second set of equations, for I had made the same wrong substitution. From m₁·Δv₁=-m₂·Δv₂ I had ended up substituting -Δv₁·m₂/m₁ for Δv₂ in the conservation of energy equation. One mistake in a single term, and the whole equation explodes.

Way back when I thought Java was a good idea, I encountered a program that would let you drag around terms in an equation, and almost started an open-source version. Then again, Maple would probably have helped me even more in this case. I was very lucky to have physics professors who accepted homework in the form of Maple printouts.

Anyway, the corrected and verified equations:

`dx = (a.x - b.x)`

`dy = (a.y - b.y)`

`factor = 2 * (dx*(b.v.x - a.v.x) + dy*(b.v.y - a.v.y)) / ((a.m + b.m) * (dx**2 + dy**2))`

`a.v.x += factor * b.m * dx`

`a.v.y += factor * b.m * dy`

`b.v.x -= factor * a.m * dx`

`b.v.y -= factor * a.m * dy`

This set also has some simplifications, particularly with the `dx`

and `dy`

calculations. Since they were already needed in two places, using them to replace the radius made the whole thing more correct for very little cost.

In fact, it now looks decent even when we let the balls slip inside each other, with one caveat: A negative `factor`

means the balls are already moving away from each other; they may already have bounced, but haven’t yet separated. Don’t adjust the velocities in that case, or things can get jittery. Granted, this rule of thumb lets really fast collisions pass by undetected, but that doesn’t look nearly as bad.

## Leave a Reply