# Introduction to Vectors

Simply put, vectors can be interpreted as arrows. More specifically, they are geometric objects that hold information about direction and magnitude. By *magnitude*, we mean "length along its own direction". Vectors are used extensively throughout almost all fields of game development: movement, shading, collision, and transformations all rely heavily on their use.

Vectors are geometric objects that has both direction and magnitude.

## Geometric Meaning

Vectors are first and foremost geometric objects. They represent a direction and magnitude in some dimension. We can choose any dimension we want, but in game development, we usually end up using 2D, 3D, and 4D vectors. Why would we use four-dimensional vectors in 3D games? We will get back to that in the chapter on *transformations*. We will be working exclusively with 2D and 3D vectors in this section. Bear in mind that most vector concepts carry over into higher dimensions, although there are some operations that are only to be found in certain dimensions. This will be clearly communicated whenever that is the case.

Here are some examples of vectors in 2D:

Based on the definition above, vectors only contain information about direction and magnitude, and we cannot force them to contain more information than that, even if we wanted to. So where is the information about its position? If we want to plot a vector in a coordinate system, we have to know its position, right? By definition, vectors do not hold any information about position, so mathematicians have accepted that we can place vectors anywhere in the coordinate system we want to, without affecting its geometric meaning. This is an important concept to learn.

Vectors are not bound to any position. Placing a vector in a different position will not change its geometric meaning.

If you've touched a game engine framework before, you have probably used vectors to convey information about points. Mathematically, this is invalid, because vectors aren't points, and should not be treated in the same way as points. We will see some differences in the section about transformations later on. But programmatically, vectors contain the same amount of - and the same data types of - information. A 3D vector will usually contain three floats, and a 3D point will contain the same.

We can perform some operations between vectors and points, and the table below shows the resulting object type:

- Vector + vector = vector
- Point + point = undefined
- Point + vector = point

One argument for having no programmatic separation between vectors and points, is that we no longer have to convert between the two. By keeping one vector class to express both points and vectors Their geometric meaning still haven't changed, and it is up to the programmer to make those disctinctions.

## Notation

When we write vectors on paper, we usually want to specify its name and its contents. Different texts use different naming conventions and markup style. Some textbooks - especially older ones - use the bolded notation to write the name of the vector, with standard parentheses to contain its information: \({\bf a} = (3, 4)\).

We will avoid this type of notation in LudoMath, in favor of the overhead arrow and square brackets: \(\overrightarrow{a} = [3, 4]\).

The notation mentioned above writes vectors in one line as a row-vector. You could also write them vertically as column-vectors:

$$\overrightarrow{a} = \begin{bmatrix}3 \\ 4 \end{bmatrix}$$

There is no informational difference between the two notations, but there are other reasons for choosing to use column-vector instead of row-vector notation. We will see thatin the section on matrices later on. For now, we'll keep using row-vector notation.

## Forms

In addition to writing vectors differently, we can also express them via different kinds of information.

In most game engines, you will be writing them in *Cartesian form*, which contains information about the x-, y-, and z-components along the coordinate axes.

Sometimes, you also just want to express vectors directly by what

# Vector Operations

Sometimes there will be differences between vector operations in different dimensions. For easier visualization, we will keep to using 2D and 3D vectors, and all the following examples will be explained for those dimensions. Whenever there are important differences, like in the cross product, we will clearly outline them. For most operations, the concept will easily carry over from 2D to 3D, and also into higher dimensions.

Remember that we can perform certain operations on two vectors, but also some operations between vectors and points, or between vectors and regular numbers. Before we start, we give a name to these "regular numbers": *scalars*. Scalars are the numbers you've been working with most of your life, for example 1, 0, 999.9, -17, \(-\frac{1}{12}\), \(\pi\), and *e*.

# Addition and Subtraction

# Scalar Multiplication

# Dot Product (AKA Scalar Product)

# Cross Product (AKA Cross Product)

# Wedge Product (AKA Exterior Product)

Before starting this section, know that the wedge product is used only in the more advanced situations, and a typical game programmer will likely not have to work with it. You can safely regard this operation as "for the especially interested", and skip over to the next section if you want more immediate uses of the operations mentioned above.

# Vector Techniques

This section is an extension of the previous section. Here, we introduce some more things you can do with vectors, but these "techniques" are not operations. Instead, they use some of the previously defined operations in order to carry out their purpose. Needless to say, this section requires that you are familiar with the previous section to a large degree.

## Angle Between Two Vectors

### Directed Angle in 2D

In some cases, it is important to know the directed angle between two vectors. This will be utilized in the *refraction* formula, for example.

## Projection

## Rejection

## Reflection

## Refraction

Refraction occurs when light passes from one medium to another. More specifically, when those two media have different indices of refraction. An index of refraction can be thought of as a measurement of how fast light moves within a medium. That's not the entire truth physically, but because game engines run in real-time, we have to make simplifications. It looks like this:

[Insert figure here.]

How much does the vector bend upon entering the new medium? To calculate the refracted vector, we need four pieces of information:

- Incident vector
- Normal vector
- Refractive index of the first medium
- Refractive index of the second medium

Look at the figure above. If we know the incident vector and the normal, then we can easily find the angle between them. All that remains now is to find the angle between the negated normal and the refracted vector. There is a formula we can use to find that - Snell's law:

$$n_1 \cdot sin(\theta_1) = n_2 \cdot sin(\theta_2)$$

Refraction playbox:

Problem Ideas:

- Calculate a single refraction angle
- Intuition: will the refracted vector's angle be larger or less than the incident angle?
- Calculate several refractions

# Vector Applications

This section is a collection of both abstract and situational cases for applying the vector mathematics we've learned so far. Sometimes, you have to combine many of the different operations. It is important to remember that vectors do not contain information about position while we are exploring these applications, and see vectors instead as directions with magnitude.