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:
We generally represent this operation with a superscript notation, for example:
The most common form of exponentiation we will see is raising a number to the power of , for example, .
An operation like this, where the exponent is , is commonly called squaring. The result of such an operation is called the square of the base number.
For example, the square of is , because .
In the order of operations, exponentiation happens before multiplication, but after brackets / parentheses. For example:
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 is the value of such that . We saw, from the previous section, that an answer is , because . Therefore, the square root of is
Square roots are often denoted using the radical sign, which looks like this:
Expressions of any length can appear under a radical sign. That expression should be resolved before calculating the square root. For example:
You may have noted that our earlier equation, , has multiple possible solutions:
This means that both and are solutions (or roots) to the equation .
Most commonly, we are only looking for the positive root, sometimes also called the principal root. When using the radical notation, such as , the only solution is the positive root.
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 axis, and the vertical axis the  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 axis, and one for it's positiong along  .
In the above example, Point 's component is , and it's component is . We can write this point as .
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 .
We could also describe the point as 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 is at the origin of our space, as . The origin is also sometimes denoted by the letter .
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:
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 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 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, and , and wanted to work out the length of the longest side, the hypotenuse
The pythagorean theorem stats that:
Let's apply it to find out how far away point is from the origin:
If we wanted to know how far away point is from the origin, that is equivalent to finding the length of the highlighted hypotenuse.
So, lets apply the pythagorean theorem:
So, point is 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 . How far away is it from the origin?
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 and , so we can calculate how far apart point and  are.
Whilst the selection of the and 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, and 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, and ?
In other words, we did and 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 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.
Something to note about this formula is that it does not matter which point you choose to be and which is . The only effect this choice will have will be to determine whether the result of 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, and are both .
Therefore, will always be positive, so it doesn't matter which point is chosen to be and which is chosen to be .
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 , to give co-ordinates . 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 can use this:
Similarly, the distance formula also expands to three dimensions. To calculate the distance between point with coordinates and point with coordinates we can use this:
Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games