Base Classes for Simulation Management
To implement ballistic motion I first implemented base classes to define
a simulation object and a simulation scene. The scene can contain many objects
(cannon balls) and updates, draws, and culls inactive objects at every frame.
Spring Class
The spring class models a particle connected by a spring to a rigid anchor.
The particle is influenced by gravity, air friction and the spring force.
I implemented two integrators, the basic Euler method and the Midpoint
method (2nd order Runge-Kutta) to compare their performance and accuracy.
source:
In terms of performance, both methods are very fast. Euler, is slightly
faster, since it only requires one derivative evaluation to integrate a time
step, while the Midpoint method requires 2. In the systems used in the assignment,
the cost of derivative evaluation is quite low. But in many cases such as
large particle systems or complex force field simulations, the evaluation
of the velocity derivative can be very costly, giving the Euler method a clear
advantage.
In terms of accuracy and stability, the Midpoint method is a clear winner. Because the Euler method uses the derivative at the beginning of the time step as its estimate for the derivative over the entire time step, it always overshoot periodic motion. This is most clearly illustrated in part B, the spring mass simulation. The Euler method spring builds up error at every peak of the spring's motion, causing it's amplitude to increase without bound. This is extremely evident when the spring constant is large or the time step is increased. The Midpoint method is mope accurate and more stable. Although it does overshoot slightly, it performs a much better approximation of the true, stable sinusoidal motion. The Midpoint method will also grow unstable is the time step or the stiffness is increased, although the results are still better than those achieved with the Euler method.
Thus unless the application requires real-time update and the force evaluations are prohibitively costly, it makes sense to use the Midpoint method. For the cost of one extra derivative evaluation the results are much more stable than those achieved with the simple Euler integration method. This allows the Midpoint method to take larger time steps, offsetting the extra cost in all but a few specialized situations.