Angles and Distance

Learn key geometry concepts like angles, triangles, and vectors to implement distance calculations.
This lesson is part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Get Started for Free
Abstract art representing computer programming
Ryan McCombe
Ryan McCombe
Updated

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.

Angles

When two lines intersect, they form an angle. On diagrams, angles of interest are marked using a circular segment between the two intersecting lines:

A diagram showing an angle

Angles are commonly measured in degrees, which are denoted by a number followed by the degree sign: °\degree. The wider an angle, the more degrees it has:

A diagram showing two angles

A complete turn is 360°360\degree and a quarter turn is 90°90\degree. A 90°90\degree angle is often referred to as a right angle and, on diagrams, is commonly denoted using a square icon:

A diagram showing a right angle

Right-Angled Triangles

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 AA and the origin.

A diagram showing a vector as a right angle triangle

If we can determine the length of the hypotenuse of this triangle, we can understand how far away AA 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.

Pythagorean Theorem

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, aa and bb, and want to work out the length of the longest side, the hypotenuse hh

A diagram showing the edges of a right-angle triangle

The Pythagorean theorem states that:

h=a2+b2h = \sqrt{a^2 + b^2}

Let’s apply it to our previous problem, to determine how far away AA is from the origin. That is equivalent to finding the length of the highlighted hypotenuse:

A diagram showing a vector as a right angle triangle

So, let’s apply the Pythagorean theorem:

h=32+42=9+16=25=5\begin{align} h &= \sqrt{3^2 + 4^2} \\ &= \sqrt{9 + 16} \\ &= \sqrt{25} \\ &= 5 \end{align}

So, point AA is 55 units away from the origin.

Pythagorean Theorem with Vectors

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 (6,8)(6,8) and want to know how far away it is from the origin. We simply insert 66 and 88 into the theorem:

h=62+82=36+64=100=10\begin{align} h &= \sqrt{6^2 + 8^2} \\ &= \sqrt{36 + 64} \\ &= \sqrt{100} \\ &= 10 \end{align}

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 V\lvert V \rvert, or two pairs of vertical bars, as in V\lvert \lvert V \rvert \rvert. So, for a vector with two dimensions labeled xx and yy, we have:

V=Vx2+Vy2 \lvert V \rvert = \sqrt{{V_x}^2 + {V_y}^2}

Vector Size in C++

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

Distance Between Any Two Points

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:

A diagram showing the distance between two vectors as a right angle triangle

In this example, the side lengths of our right angle triangle have a length of 55 and 33, so we can calculate how far apart points AA and BB are:

h=52+32=25+9=345.83\begin{align} h &= \sqrt{5^2 + 3^2} \\ &= \sqrt{25 + 9} \\ &= \sqrt{34} \\ &\approx 5.83 \end{align}

Calculating Side Lengths

It may be obvious why we choose the values 55 and 33 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, AA and BB 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, 55 and 33?

  • We took the xx coordinate of point BB and subtracted the xx coordinate of point AA. That gave us 72=57 - 2 = 5
  • We took the yy coordinate of point BB and subtracted the yy coordinate of point AA. That gave us 41=34 - 1 = 3

In other words, we did AxBxA_\text{x} - B_\text{x} and ByAyB_\text{y} - A_\text{y} to get the two sides of our right-angled triangle.

Which Point is AA and Which is BB?

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:

  • If we use AA first, we calculate AxBxA_\text{x} - B_\text{x} and AyByA_\text{y} - B_\text{y}
  • If we use BB first, we calculate BxAxB_\text{x} - A_\text{x} and ByAyB_\text{y} - A_\text{y}

These expressions will have opposite signs. For example, if AxBx=5A_\text{x} - B_\text{x} = 5, then BxAx=5B_\text{x} - A_\text{x} = -5

However, in the distance formula, we square these differences: (AxBx)2(A_\text{x} - B_\text{x})^2 and (AyBy)2(A_\text{y} - B_\text{y})^2.

Since the square of a number is always positive (whether the original number was positive or negative), we get the same result either way:

  • 52=255^2 = 25
  • (5)2=25(-5)^2 = 25

Therefore, the distance calculated will be identical regardless of which point you designate as AA and which as BB. This makes sense intuitively - the distance from London to Paris is the same as the distance from Paris to London.

Distance Formula

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 AA and BB and, for now, we'll assume we're working in a two-dimensional space:

(AxBx)2+(AyBy)2\sqrt{(A_\text{x} - B_\text{x})^2 + (A_\text{y} - B_\text{y})^2}

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

Going 3D

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 zz, to give coordinates (x,y,z)(x, y, z).

// Vec3.h
#pragma once

struct Vec3 {
  float x;
  float y;
  float z;
};

The origin of a three-dimensional cartesian space is (0,0,0)(0, 0, 0)

The Pythagorean theorem expands to three dimensions. To calculate how far a three-dimensional point (x,y,z)(x, y, z) is from the origin, we add the zz component as follows:

x2+y2+z2\sqrt{x^2 + y^2 + z^2}

Similarly, the distance formula also expands to three dimensions. To calculate the distance between point AA with coordinates (Ax,Ay,Az)(A_\text{x}, A_\text{y}, A_\text{z}) and point BB with coordinates (Bx,By,Bz)(B_\text{x}, B_\text{y}, B_\text{z}), we calculate:

(AxBx)2+(AyBy)2+(AzBz)2\sqrt{(A_\text{x} - B_\text{x})^2 + (A_\text{y} - B_\text{y})^2 + (A_\text{z} - B_\text{z})^2}

Summary

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:

  • Angles measure the space between intersecting lines, with 90° forming right angles
  • Right-angled triangles have special properties that make them useful for distance calculations
  • The Pythagorean theorem h=a2+b2h = \sqrt{a^2 + b^2} helps calculate the hypotenuse of right-angled triangles
  • Vector magnitude represents how far a point is from the origin
  • The distance formula allows us to calculate distances between any two points in space
  • We can extend these formulas to work in 3D by adding the z-component
Free and Unlimited Access

Professional C++

Unlock the true power of C++ by mastering complex features, optimizing performance, and learning expert workflows used in professional development

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Ryan McCombe
Ryan McCombe
Updated
sdl2-promo.jpg
This lesson is part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Get Started for Free
Maths, Physics and Collisions
sdl2-promo.jpg
This lesson is part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

This course includes:

  • 90 Lessons
  • 92% Positive Reviews
  • Regularly Updated
  • Help and FAQs
Free and Unlimited Access

Professional C++

Unlock the true power of C++ by mastering complex features, optimizing performance, and learning expert workflows used in professional development

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2025 - All Rights Reserved