Learn the basics of maths and geometry, so we can represent concepts like positions and distances between objects in our worlds.

Posted

We want to start simulating basic world which we can imagine our objects existing in. To do this, we will need to represent concepts like positioning, distance and movement.

This and the following chapter underpins the minimal topics in maths that we need in order to start to model these relationships.

We'll cover some fundamental arithmetic and then move onto basic geometry. Finally, we'll see how we can apply what we've learnt to determine how far away two objects are in a 2D or 3D world.

Exponentiation is the process of "raising" a number to the power of another number. We can think of it as multiplying a number by itself, multiple times.

The number we start with is called the "base", and the number of times it is multiplied by itself is called the "power", or the "exponent". For example:

- $2$ to the power of $2$ is equivalent to $2 \times 2$, which is equal to $4$
- $2$ to the power of $3$ is equivalent to $2 \times 2 \times 2$, which is equal to $8$
- $3$ to the power of $2$ is equivalent to $3 \times 3$, which is equal to $9$

We generally represent this operation with a *superscript* notation, for example:

- $2$ to the power of $2$ is written as $2^2$
- $2$ to the power of $3$ is written as $2^3$
- $3$ to the power of $2$ is written as $3^2$

The most common form of exponentiation we will see is raising a number to the power of $2$, for example, $5^2$.

An operation like this, where the exponent is $2$, is commonly called *squaring*. The result of such an operation is called the *square* of the base number.

For example, the *square* of $5$ is $25$, because $5^2 = 25$.

In the order of operations, exponentiation happens before multiplication, but after brackets / parentheses. For example:

- $2 \times 2^2 = 2 \times 4 = 8$
- $(2 \times 2)^2 = 4^2 = 16$

Finding the *square root* of a number is, in effect, reversing the process of squaring. For example, the square root of 25 is the number that, if squared, would result in 25.

In other words, the square root of $25$ is the value of $x$ such that $x^2 = 25$. We saw, from the previous section, that an answer is $5$, because $5^2 = 25$. Therefore, the square root of $25$ is $5$

Square roots are often denoted using the *radical* sign, which looks like this: $\sqrt{25}$

Expressions of any length can appear under a radical sign. That expression should be resolved before calculating the square root. For example: $\sqrt{50 + 25 \times 2} = \sqrt{100} = 10$

You may have noted that our earlier equation, $x^2 = 25$, has multiple possible solutions:

- $5^2 = 25$
- $-5^2 = 25$

This means that both $5$ and $-5$ are solutions (or roots) to the equation $x^2 = 25$.

Most commonly, we are only looking for the *positive* root, sometimes also called the *principal* root. When using the radical notation, such as $\sqrt{25}$, the *only* solution is the positive root.

- $\sqrt{25} = 5$
- $\sqrt{25} \neq -5$

We can think of virtual worlds as **spaces**. In maths and physics, a space contains a collection of objects that obey pre-defined structure and rules. Typically, the structure and rules we want to implement mirror those we are most familiar with in the real world.

That means we will be interested in things like the distance between objects, and rules that govern how objects can move around the space.

The most basic object that exists within spaces are points. We can model more complex objects by creating a collection of such points - for example, a triangle could be defined by 3 points - one for each of its corners. A full 3D monster, created by an artist, could have thousands of points to define it's shape.

The most important property we care about when it comes to these points is what their current *position* is within the space. We can implement the concept of position by imagining our space as a grid.

For now, we will work with a two-dimensional space:

The system we most commonly use to define positions in a space is a coordinate system. The most useful system is the Cartesian coordinate system.

We can set up this system by placing an axis along each dimension. Conventionally, we call the horizontal axis the $x$ axis, and the vertical axis the $y$ axis.

With the axes set up, we can now define each point by a collection of numbers - one for each dimension the space has. In a two-dimensional space, each point will have two values - one for its position along the $x$ axis, and one for it's positiong along $y$ .

In the above example, Point $A$'s $x$ component is $3$, and it's $y$ component is $4$. We can write this point as $A = (3, 4)$.

In our lessons on classes and structs, we saw how we might refer to the collection of all the components required to represent a point as being a *vector*.

In C++, we modelled the idea of a vector using a struct, which then let us access each individual component using the `.`

operator.

For example, `A.x`

is how we would access to the `x`

component of a vector called `A`

. Outside of code, it's common to use subscript notation, such as $A_x$.

We could also describe the point as $x_A$ if we preferred. Notation in maths and physics is less strict than code - we can choose whatever format we think best communicates our thoughts. The important thing is just to be consistant.

In a cartesian coordinate system, the point that all the axes intersect is referred to the *origin*. The origin has a value of 0 in every dimension, and can generally be considered the centre of the space.

In the previous example, point $B$ is at the origin of our space, as $B = (0, 0)$. The origin is also sometimes denoted by the letter $O$.

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

Angles are commonly measured in degrees, which is denoted by number followed by the degree sign: $\degree$

The wider an angle, the more degrees it has.

A complete turn is $360\degree$ and a quarter turn is $90\degree$

A $90\degree$ angle is often referred to as a right angle and, on diagrams, is commonly denoted by 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 useful to know, as many problems we encounter can be modeled as 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 $A$ and the origin.

By understanding this situation as involving a right angle triangle, we can use the properties of right angle triangles to work out the distance between our two points.

This has many uses - for example, it is the basis of determining whether or not a monster is in range of our attacks.

We can use the Pythagorean Theorem to work out this distance.

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, lets imagine we know the lengths of the two smaller sides, $a$ and $b$, and wanted to work out the length of the longest side, the hypotenuse $h$

The pythagorean theorem stats that:

$h = \sqrt{a^2 + b^2}$

Let's apply it to find out how far away point $A$ is from the origin:

If we wanted to know how far away point $A$ is from the origin, that is equivalent to finding the length of the highlighted hypotenuse.

So, lets apply the pythagorean theorem:

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

So, point $A$ is $5$ units away from the origin.

The pythagorean theorem is particularly useful when dealing with spaces, because we always know the length of the smaller sides of our right angle triangle.

They are the values of our co-ordinates, so we can just apply the same pattern with different inputs.

Lets imagine we have the point $(6,8)$. How far away is it from the origin?

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

Any situation where we're applying the same process, only with different numbers, might get our programmer senses tingling. This sounds a lot like a function, and we'll be creating it in the next lesson.

It is not especially useful to know how far away an object is from the origin. More often, we will 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 $3$ and $7$, so we can calculate how far apart point $A$ and $B$ are.

$\begin{align}
h &= \sqrt{5^2 + 3^2} \\
&= \sqrt{25 + 9} \\
&= \sqrt{34} \\
&\approx 5.8
\end{align}$

Whilst the selection of the $5$ and $3$ may have seemed intuitive by looking at a our diagram, we generally won't have a visual representation to work from.

We also want to write code that will work with any input points, therefore, it is worth detailing how we would come to these values in a more general case.

For example, $A$ and $B$ will just be points passed to our function. We don't know their position ahead of time - we have to work it out. So how would we get the values that were, in this case, $5$ and $3$?

- We took the $x$ coordinate of point $B$ and subtracted the $x$ coordinate of point $A$. That gave us $7 - 2$
- We took the $y$ coordinate of point $B$ and subtracted the $y$ coordinate of point $A$. That gave us $4 - 1$

In other words, we did $x_\text{B} - x_\text{A}$ and $y_\text{B} - y_\text{A}$ to get the two sides of our right angled triangle.

This allows us to expand the pythagorean theorem to a new formula, to calculate the distance between any two points. Lets call our points $A$ and $B$ and, for now, we'll assume we're working in a two-dimensional space:

$\sqrt{(x_\text{A} - x_\text{B})^2 + (y_\text{A} - y_\text{B})^2}$

This formula is sometimes referred to as the *distance formula*.

Something to note about this formula is that it does not matter which point you choose to be $A$ and which is $B$. The only effect this choice will have will be to determine whether the result of $x_\text{A} - x_\text{B}$ is negative or positive.

However, that result is being immediately squared, so the sign will be discarded anyway. Squaring a number will give the same result whether that number is negative or positive.

This is because multiplying two negative numbers together will always result in a positive number. For example, $5^2$ and $-5^2$ are both $25$.

Therefore, $(x_\text{A} - x_\text{B})^2$ will always be positive, so it doesn't matter which point is chosen to be $A$ and which is chosen to be $B$.

The examples above used two-dimensions in their examples and illustrations, but the techniques apply to three dimensional worlds too.

In cartesian co-ordinate systems, the 3rd dimension is generally identified by the letter $z$, to give co-ordinates $(x, y, z)$. The origin of a three-dimensional cartesian space is $(0, 0, 0)$

The pythagorean theorem expands to three dimensions. To calculate how far a three-dimensional point $(x, y, z)$ is from the origin, we can use this:

$\sqrt{x^2 + y^2 + z^2}$

Similarly, the distance formula also expands to three dimensions. To calculate the distance between point $A$ with coordinates $(x_\text{A}, y_\text{A}, z_\text{A})$ and point $B$ with coordinates $(x_\text{B}, y_\text{B}, z_\text{B})$ we can use this:

$\sqrt{(x_\text{A} - x_\text{B})^2 + (y_\text{A} - y_\text{B})^2 + (z_\text{A} - z_\text{B})^2}$

Was this lesson useful?

Posted

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

- 41.GPUs and Rasterization
- 42.SDL Renderers