The mathematics of my simulation...

The mathematics involved are not super difficult, but do require some thought. Remember that I am only dealing with collisions that occur between two spheres. The approach that I took is below:

For
the collisions to work both kinetic energy and momentum have to be conserved. To
do this, the momentum equation was solved for the final velocity of the second
object and that value was then plugged into the kinetic energy equation:

M_{1}V_{1i
}+ M_{2}V_{2 i}= M_{2}V_{2f }+ M_{1}V_{1f
}

_{ }(M_{1}V_{1i
}+ M_{2}V_{2 i }- M_{1}V_{1f}) / M_{2 }=
V_{2f }

Letting W = M_{1i}V_{1i
}+ M_{2i}V_{2 I, }the new equation became:

M_{1}V_{1i}^{2}_{
}+ M_{2}V_{2i}^{2 }= M_{1}V_{1f}^{2
}+ (W - M_{1}V_{1f})^{ 2}/M_{2}

Foiling the added expression, the new equation
became:

M_{1}V_{1i}^{2}_{
}+ M_{2}V_{2i}^{2 }= M_{1}V_{1f}^{2
}+ (W^{2 }– 2WM_{1}V_{1f} + M_{1}^{2}V_{1f}^{2})/M_{2
}

_{ }Letting Z = M_{1}V_{1i}^{2}_{
}+ M_{2}V_{2i}^{2} and splitting the numerator of
the added expression the new equation became:

Z
= M_{1}V_{1f}^{2 }+ W^{2}/M_{2} – (2WM_{1}V_{1f})/M_{2}
+ (M_{1}^{2}V_{1f}^{2})/M_{2
}

_{ }Next, the fact that this equation is a quadratic
equation was used to find the possible roots for V_{2f. }This was done
by letting A = (M_{1}^{2}/M_{2} +M_{1}), B =
(-2WM_{1})/M_{2}, and letting C = (W^{2}/M_{2}
–Z):

V_{2f
}= (-B ± Ö(B^{2}
–4AC))/(2A)

The
positive root was assumed and since it is obvious that the final velocity could
not be larger than the initial velocity of a sphere that has a mass larger than
the second sphere that it is colliding with, if this was the case, the negative
root was used. The value of V_{1f} was calculated using the calculated V_{2f
}value:

V_{1f}
= (M_{1}V_{1i }+ M_{2}V_{2
i }– M_{2}V_{2f}) / M_{1}

Using this information and the angle of the
collision between the two spheres, which came out to look like this:

q
= arctan((Y_{1 }– Y_{2}) / (X_{1 }– X_{2}))
at the time of collision.

The program assigned new values to the V_{x} and V_{y}
values of the two spheres:

V_{x1}=
cos(q)V_{1f}

V_{x2}=
cos(q)V_{2f
}

V_{y1}=
sin(q)V_{1f}

V_{y2}=
sin(q)V_{2f}

To check if any two spheres had collided, the
distance between them was compared to their total radii. Thus:

Collision
occurred if Ö((X_{1}-X_{2})^{2}+
(Y_{1}-Y_{2})^{2}) >= R_{1} + R_{2
}