## Trajectories

This is a good time to start exploring physics. The better the physics model you develop the more realistic your game will be. User expect things to proceed the way they do in real life. They hate when they have to learn that things fall following different laws than the gravity they know. On the other hand you should let them deal with the gameplay and not the physics laws behind the game.

One of the first things we notice in nature is trajectories. The term seems scientific and it might e intimidating but it is found in almost all the games we play with our friends since we can remember. The term means the path an object follows when thrown. The ball from the hands of a baseball player, or when kicked by a soccer player. This means that everybody is familiar with the phenomenon and this is the reason I decided to start this series of physics articles with it.

#### The physics of movement

First of all we must start analyzing the phenomenon. What happens when we throw a ball for example. What forces act upon it and what effects they have on its state.

As Newton explained when there are no external forces acting upon an object then the object remains in the same state, either moving at constant speed or standing still. On the other hand if we apply a constant force the velocity of the object changes proportionally to the amount of matter within it, aka its mass. The rate of change is constant and is called acceleration. We use the term velocity which is a vector instead of speed which is a scalar. Actually speed is the length of the velocity vector. Vectors have the extra directional property. In the case of velocity its direction is the direction of the movement. What we said so far is actually the second law of motion described by Newton and its formal description is given by the equation:

F = mγ

Where F is the force, m the mass and γ the acceleration.

The velocity of the objects at any given time is calculated by the equation

v = v_{0} + γt

where v_{0} is the initial velocity γ the acceleration and t is the
elapsed time.

The final location after that time is given by

s = s_{0} + v_{0}t + γt^{2}/2

Where s_{0} is the initial location, v_{0} the initial velocity,
γ is the acceleration and t is the elapsed time.

The above equations are all we need to calculate the path of the object. Since velocity, acceleration and distance are vector quantities we do not need anything else in order to determine the accurate position and velocity of the object at any given time. All the above are based on the fact that you are familiar with the underlying vector math required.

#### Mathematical basics

The following paragraphs are a little reminder of the vector math we need for the calculations described above.

First we see how to add two vectors. As shown in the image all we have to do is make the vectors consecutive and then connect the starting point of the first and the ending point of the second. When we have the coordinates of the vectors we can just add the respective coordinates as

v_{1} = (x_{1}, y_{1}, z_{1}) and

v_{2} = (x_{2}, y_{2}, z_{2}) so the sum is

v = (x_{1}+x_{2}, y_{1}+y_{2}, z_{1}+z_{2})

The next thing is the multiplication of a vector with a scalar. To perform the multiplication we multiply every coordinate with the scalar as follows

v_{1} = (x_{1}, y_{1}, z_{1}) and

v = sv_{1} = (sx_{1}, sy_{1}, sz_{1})

#### Summary

Calculating the trajectory of an object is not that hard after all.
The only force applied on it is the gravity which gives it an
acceleration of 9.81m/sec^{2} pointing down in the 'y'
direction. So the 'γ' term is replaced by the 'g' term which represents
the acceleration generated for every object by the gravity of the earth.
All the vector math is coded in the 'clf_' library simplifying the
programming work needed.

The sample program implements a simple canon and cannonball. The initial speed of the cannonball is 15 m/sec and you can modify its shoot angle with the up and down arrow keys so you can see the difference in the path of the cannonball.

Click here to download the sample program.