Most artillery games are based on the Projectile Motion Formula used to trace the trajectory of a projectile thrown in the air. Due to gravity, its trajectory will be a parabola which shape will vary based on the angle and initial velocity of the projectile.
Use the script below and see what happens when you change the angle. (e.g. use a value between 0 and 90 degrees) or the velocity.
This page helped us with defining the equation for the trajectory of the projectile:
https://en.wikipedia.org/wiki/Projectile_motion
Using the displacement formula we can calculate the position ((x,y) coordinates) of a projectile at any given time.
In these formulas:
- represent the starting position. (e.g. position of the tank on the screen),
- represents the initial velocity, in other words the initial power/speed that was used to shoot/throw the projectile,
- (theta) represents the angle of projection. (At what angle was the projectile thrown)
- represents the time in seconds since the object was thrown. (Starts at 0). The number of frames since the object has been thrown can be used as a frame based game display a frame every x milliseconds.)
- represents the gravity. (On planet Earth: g = 9.81)
Let’s apply these formula using a Python script using the processing library to create a frame based animation.
Angry Birds, Tanks, Worms, Sports/Ball based games (Basketball…) all use a similar algorithm and formula. Can you think of any other video games based on this formula?
Alternative Approach
An alternative approach to implement this projectile motion formula to a flying object/sprite/ball using a frame based animation is to recalculate the position and velocity vector of the sprite at frame n+1 based at on its position and velocity at frame n:
To do so we will use the following formulas:
In these formulas:
- represents the initial velocity, in other words the initial power/speed that was used to shoot/throw the projectile,
- (theta) represents the angle of projection. (At what angle was the projectile thrown)
- represents the time in seconds since the object was thrown. (Starts at 0). The number of frames since the object has been thrown can be used as a frame based game display a frame every x milliseconds.)
- represents the “delta”: the amount of time (milliseconds) between two frames
- represents the gravity. (On planet Earth: g = 9.81)
Python Code: