Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Start a free Basic trial
to watch this video
During the game development process it is always necessary to dig into a little algebra to calculate angles and velocities. In this video we talk about the math needed to figure out the direction and speed of a bullet.
Further Explanation

0:00
At some point in the game development process, you will

0:03
be required to use just a little bit of math.

0:07
Today we'll be asking you to dig into

0:08
your algebra skills to figure out shooting trajectories.

0:13
So before you write the code, let's talk about the plan and what equations we're

0:18
going to use to figure out the

0:19
shooting trajectory and the speed of the projectile.

0:24
Our game will use a simple one touch mechanic for interaction, where the

0:29
player touches is ultimately where we would like our projectile to go through.

0:35
I say go through, because the projectile should

0:37
not stop where the player touched the screen.

0:39
It should continue until out of sight of

0:42
the player or until it crashes into something.

0:46
For example, a space dog.

0:47
[NOISE] When the player touches the screen,

0:50
we will know the location on a grid.

0:53
Our X and Y origin is in the bottom left hand corner of the screen.

0:58
This also happens to be our scene's anchor point.

1:04
We want a projectile to go from the red point,

1:07
which is right above our machine, to the blue point where

1:12
the player touch the screen and then continue off the screen

1:16
to a yellow point, we'll call this an off screen point.

1:21
Likewise, if a player touches on the other side of the

1:25
screen, or the right side of the screen, we want the

1:28
projectile to go from the red point through the blue point

1:32
and continue until out of sight, or to the off screen point.

1:38
In order to achieve all this, we will need to know a little bit about line equations.

1:43
The first thing to know about any straight line is the slope.

1:47
This is by definition how fast the line raises from

1:50
the X axis as it goes outwards in either direction.

1:55
Slope can be calculated with rise over run, or

1:59
the change in Y divided by the change in X.

2:03
If we take the red point as x1 and y1 and the blue point

2:09
as x3 and y3, then we can calculate the slope to y3

2:15
minus y1 divided by x3 minus x1.

2:21
Now that we know the slope of the line, we can calculate

2:24
the line's equation by using the Point Slope form of a line equation.

2:30
You would choose either the red dot or blue dot plus the

2:33
slope we had previously, to find an equation that matches the green line.

2:39
The point of the equation is to find the Y value given any X value.

2:45
This will help us determine the yellow point which

2:48
is off screen to set the trajectory of our projectile.

2:52
For the game, we'll be using minus ten for the X

2:55
value as the point off screen on the left hand side.

2:59
If the player touches the screen on the right side, then we

3:02
can set our X value as the width of the screen plus ten.

3:07
We can now plug that into our equation to get our Y value

3:11
of the yellow point, so that the

3:13
projectile can travel off screen as desired.

3:18
Once we have direction, then the only other missing piece to

3:21
simulating an projectile is the speed at which it will travel.

3:26
In order to calculate a constant speed over varying distances, we will

3:30
need to know the distance that we want the projectile to travel.

3:34
We can use the Pythagoreans theorem to calculate the

3:37
distance from the red point to the yellow point.

3:41
The Pythagorean's Theorem states A square plus B square is equal to C square.

3:48
If look at the green line as C and the other lines

3:52
as A and B, we can easily calculate the distance of C.

3:57
A is simple.

3:59
The Y value of the yellow point minus the Y value of the red point.

4:05
And B is the X value of the yellow point minus the X value of the red point.

4:13
Squaring will remove any negative values so we need not worry.

4:18
If we square both sides of our equation, then we get C on

4:22
one side and just have to plug in values for the left side.

4:27
Now that we finally have our distance, we can calculate the speed.

4:32
Speed is distance divided by time.

4:35
Or we can look at it as time is distance divided by speed.

4:41
If we want all our projectiles to go at the

4:43
same speed, then the time will vary, and it will

4:47
tell us how long our projectile should take to travel

4:50
from the red point to the yellow point in all cases.

4:55
In our next video, we will put our

4:57
math skills to work and code in our trajectories.
You need to sign up for Treehouse in order to download course files.
Sign up