COMP259: Homework 1

Simple Physics


Problem A: Ballistic Motion

Given a location of a gun at (0,0,0), write an 3D artillery simulator that can take in the mass of the projectile, amount of powder, the azimuth and elevation of the gun barrel. Use the amount of powder and the mass of the projectile to determine the muzzle velocity. Account for gravity and air friction. Assume that one kilogram of powder produces 10,000 newtons of force. Assume instantaneous acceleration as a result of the powder going off. Air friction coefficient is constant. Set it to be 50 kg/s initially. The gun and target are both on the X-Z plane.

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.

Projectile Class
Next I implemented the Projectile class which defines a particle that acts under the influence of gravity and wind resistance. The projectile is initialized with a structure of parameters (described above). The initial muzzle velocity is calculated assuming a 1m long gun barrel over which the force of the gun powder acts uniformly until the ball leaves the barrel. Main Program
The main program initializes the cannon parameters and allows cannon balls to be launched with a key press. The scene is dresses up with a model of a powder cannon, so you can see where you're shooting.




Problem B: Spring-Mass Simulator

A spring hands vertically in its equilibrium or resting position. Given a user-defined mass m attached to the spring with the spring constant k, not stretched at first. Simulate the motion of the spring and mass under the effects of spring and gravitational forces. Assume the mass is 5 kg and k = 15 kg/s^2. Then, set the mass to be 10 kg and k = 20 kg/s^2.

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.

Main Program
The main program initializes two springs, one using each integrator type, allowing side by side comparison.

Integrators

For both problems, you'll need to write at least two functions (Euler's method vs. Mid-point or 4th order Runge-Kutta) for integration and compare their numerical accuracy and stability. Which function is more accurate? Which one is more stable? Which one is more efficient?

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.