This lesson introduces the core geometric concepts used in game programming. We'll cover angles and right-angled triangles before exploring the Pythagorean theorem and its applications in calculating distances.
We'll implement these mathematical principles in C++ to solve common game development problems. The lesson demonstrates how to calculate vector magnitudes and distances between objects in both 2D and 3D coordinate systems.
These techniques form the basis for many game mechanics, including collision detection and spatial positioning, which we’ll implement later in this chapter.
When two lines intersect, they form an angle. On diagrams, angles of interest are marked using a circular segment between the two intersecting lines:
Angles are commonly measured in degrees, which are denoted by a number followed by the degree sign: . The wider an angle, the more degrees it has:
A complete turn is and a quarter turn is . A angle is often referred to as a right angle and, on diagrams, is commonly denoted using a square icon:
A right-angled triangle is a triangle where two of the sides are perpendicular to each other, forming a right angle. Right-angled triangles tend to be illustrated with a square in the right angle.
The side opposite this angle is referred to as the hypotenuse and will be the longest side of the triangle.
Right-angle triangles and their properties are extremely useful, as many problems we encounter can be solved by using the properties of right-angled triangles.
For example, we can imagine any two points in our space as forming the hypotenuse of a right angle triangle. Below, we illustrate this with point and the origin.
If we can determine the length of the hypotenuse of this triangle, we can understand how far away is from the origin.
Determining the distance between two points has many uses - for example, we can use it to check if the player is close to some object they’re trying to interact with, or some enemy they’re trying to attack.
The Pythagorean theorem allows us to calculate the length of a right-angled triangle's hypotenuse if we know the length of its two other sides.
For example, let’s imagine we know the lengths of the two smaller sides, and , and want to work out the length of the longest side, the hypotenuse
The Pythagorean theorem states that:
Let’s apply it to our previous problem, to determine how far away is from the origin. That is equivalent to finding the length of the highlighted hypotenuse:
So, let’s apply the Pythagorean theorem:
So, point is units away from the origin.
The Pythagorean theorem is particularly useful when dealing with vectors because we always know what the inputs should be. The inputs are the values of the vector’s components.
Let’s imagine we have the point and want to know how far away it is from the origin. We simply insert and into the theorem:
How far away the vector is from the origin is referred to as the vector’s length, size, or magnitude. In mathematical notation, the size of a vector is typically represented by surrounding it with a pair of vertical bars, as in , or two pairs of vertical bars, as in . So, for a vector with two dimensions labeled and , we have:
Let’s implement the Pythagorean theorem in C++ to determine a vector’s length:
#include <iostream>
#include <cmath>
struct Vec2 {
float x;
float y;
float GetLength() const {
return std::sqrt(x * x + y * y);
}
};
int main() {
Vec2 Vector{6, 8};
std::cout << "Vector Length: "
<< Vector.GetLength();
}
Vector Length: 10
Often, we don’t care how far an object is from the origin. Rather, we want to know how far away our object is from some other object.
The technique using the Pythagorean theorem applies to this situation, too. We can create a right-angled triangle from any two points:
In this example, the side lengths of our right angle triangle have a length of and , so we can calculate how far apart points and are:
It may be obvious why we choose the values and to insert into the formula - we can see those values in our diagram. However, we generally won't have a visual representation to work from.
Therefore, it is worth understanding how we would come to these values in a level of detail that allows us to create a general algorithm, like a C++ function.
In the general case, and will be points - like Vec2
instances - that are passed to our function. We won't know the horizontal and vertical distance between them - we need to work it out. So how did we get the values that were, in this case, and ?
In other words, we did and to get the two sides of our right-angled triangle.
When calculating the distance between two points, you might wonder if the order matters. Let's investigate what happens when we swap the points in our calculation:
These expressions will have opposite signs. For example, if , then
However, in the distance formula, we square these differences: and .
Since the square of a number is always positive (whether the original number was positive or negative), we get the same result either way:
Therefore, the distance calculated will be identical regardless of which point you designate as and which as . This makes sense intuitively - the distance from London to Paris is the same as the distance from Paris to London.
We can use the approach we covered here to expand the Pythagorean theorem to a new formula. This new formula allows us to calculate the distance between any two points. Let’s call our points and and, for now, we'll assume we're working in a two-dimensional space:
This formula is sometimes referred to as the distance formula. In C++, we could implement it like this:
#include <iostream>
#include <cmath>
struct Vec2 {
float x;
float y;
float GetDistance(const Vec2& Other) const {
return std::sqrt(
std::pow(x - Other.x, 2) +
std::pow(y - Other.y, 2)
);
}
};
int main() {
Vec2 A{2, 1};
Vec2 B{7, 4};
std::cout << "Distance between points: "
<< A.GetDistance(B);
}
Distance between points: 5.83095
The examples above used two dimensions in their examples and illustrations, but the techniques apply to three-dimensional spaces too.
In cartesian coordinate systems, the 3rd dimension is generally identified by the letter , to give coordinates .
// Vec3.h
#pragma once
struct Vec3 {
float x;
float y;
float z;
};
The origin of a three-dimensional cartesian space is
The Pythagorean theorem expands to three dimensions. To calculate how far a three-dimensional point is from the origin, we add the component as follows:
Similarly, the distance formula also expands to three dimensions. To calculate the distance between point with coordinates and point with coordinates , we calculate:
We've covered essential geometric concepts that form the foundation of many game mechanics. From understanding angles and right-angled triangles to implementing the Pythagorean theorem in C++, you now have the tools to calculate distances between game objects. Key takeaways:
Learn key geometry concepts like angles, triangles, and vectors to implement distance calculations.
Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games