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 = v0 + γt

where v0 is the initial velocity γ the acceleration and t is the elapsed time.

The final location after that time is given by

s = s0 + v0t + γt2/2

Where s0 is the initial location, v0 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

vector add

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

v1 = (x1, y1, z1) and
v2 = (x2, y2, z2) so the sum is
v = (x1+x2, y1+y2, z1+z2)

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

v1 = (x1, y1, z1) and
v = sv1 = (sx1, sy1, sz1)


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/sec2 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.

GusOnGames is member of Obscure Horizons group
Increase your website traffic with Attracta.com