Introduction
Vector Math is a core subject of game development. I will try and be as succinct as I possibly can and I will focus on 2D spaces.
Cartesian Coordinates
This coordinate system is represented by an x-axis and a y-axis. This makes it a 2D coordinate system.
Figure 1 - Cartesian Coordinate System
A point in this system marks an x and y pair (x, y).
Vector
Roughly speaking, a vector is a line that connects two points in the cartesian plane. The direction and length of the line are taken into account to define the vector.
The vector length is called vector magnitude.
Figure 2 - A Vector
A vector is often represented like this in game programming:
struct Vector {
float x;
float y;
}
Vector Use In Games
Position
Vector(10, 20) can be used to represent the position of a game object in the 2D space. The game object is said to be at position x = 10 and y = 20.
Direction
The length of a vector has no impact on its direction. It could be 30° for a vector of any length.
Since length does not impact on direction, the math to rotate a vector is simplified by normalizing the vector. Normalizing means making the vector length 1 by dividing both the x and y component of the vector by the vector's length.
Velocity
Velocity is the rate of change of distance over of time. The change of distance is called displacement. If we use m/s to measure velocity, a bullet traveling from x = 0 to x = 5 in 1 second has a velocity of 5 m/s. The formula to calculate average velocity is (x1 - x0)/(t1 - t0). You can think of it also as displacement / time.
You can also find the displacement an object has travelled over time with this formula: displacement = average velocity * time (i.e.: If average velocity is 5 m/s, how far has it travelled in 3 seconds? distance = 5 * 3 = 15 m.
Move a game object
With the information above, it's easy to derive the logic to move an object. Use this formula: final position = current position + displacement. In a game, the time used to calculate a displacement, is called the delta time.
Delta time is the period of time elapsed between update calls. It is also known as game step. So our formula will look like this to move an object:
Pf = Pc + (V*Dt)
Where Pf = final position, Pc = current postion, V = average velocity, Dt = delta time.
Acceleration
Velocity can be constant, but it can also vary. Acceleration is the change in velocity over time. For instance, m/s measures velocity, but (m/s)/s measures acceleration and we can state that as m/s^2. The acceleration formula is:
A = Dv / Dt
Where A = acceleration, Dv = change in velocity and Dt = change in time. Think of Dv as (v2 - v1) and Dt as (t2 -t1). If at t1 = 1, v1 = 3 m/s and at t2 = 2, v2 = 6 m/s, then
A = (6 - 3) / 1 = 3 m/s^2.
In other words, if the acceleration of an object is 3 m/s^2. 3 m/s will be added to its velocity every second.
Essential Vector Operations
Let's use these two vectors Va = {3, 5} and Vb = {10, 12} to perform the operations bellow.
Addition
In order to add them you do the following:
V = Va + Vb = {Va.x + Vb.x, Va.y + Vb.y}, therefore you get V = {13, 17}.
Subtraction
To subtract those two vectors, we would do this: V = Va - Vb = {Va.x - Vb.x, Va.y - Vb.y}.
Negating
To negate them simply change the signs of the x and y values: Va = {-3, -5} and Vb = {-10, -12}.
Scaling
You can increase or decrease the length of a vector by multiplying or dividing the vector values. This would change its magnitude (as stated earlier, this has no impact on direction). In order to double Va's length, we simply times it by 2:
Va = {3 * 2, 5 * 2} = {6, 10}.
To make its length half of what it is, we simply divide it by 2: Va = {3 / 2, 5 / 2} = {1.5, 2.5}.
Find out length of a Vector
We just need to find the hypothenuse of the vector:
Length of Va = √(3^2 + 5^2) = √(9 + 25) = 5.83095.
Conclusion
There is more we can do with Vectors in game development. I might write a Part 2 of this topic, but this serves as a brief intro for now.
Images Attributions