VECTORS

Vectors are useful in storing information like 'position' and 'velocity'. I wrote a vector class called 'Vector2D' that handles all of my vector needs. For example, all entities in the game have an attribute called 'position' which is a Vector2D type. This page just explains what the methods in my Vector2D class do. Finally we apply the Vector2D class to our Pacman game.

Vector Basics

A vector is something that describes magnitude and direction. Both quantities are necessary in describing the vector. Imagine you live in a city with a grid-based system as shown below. You're at point A and you're trying to get to point B. You're new in town so you stop somebody on the street and ask them how to get to point B. That person tells you that you need to go 3 blocks East, then 2 blocks South. The person basically just gave you two vectors to follow. The first vector is 3 blocks East. The magnitude, or length, of this vector is 3 blocks and the direction of this vector is East. The second vector is 2 blocks South. Notice how both parts of each vector are necessary in order for you to reach your destination. If the person left out the directions and just told you to go 3 blocks then 2 blocks, you'd naturally want to know in which directions? Likewise, if the person told you to go East then South, you'd naturally want to know how far in each direction? The order in these instructions isn't important either, the person could have just as easily told you to go 2 blocks South first, then 3 blocks East. There are many ways the person could give you directions in order for you to reach your destination. In the third example below the person gives you the runaround, but you reach your destination nevertheless.

It would be better if you could just go straight to point B, which is the shortest distance after all. Notice that this is also a vector since it has both magnitude and direction, but what are they? The magnitude is easy, you just use Pythagorean's Theorem. You see that the three vectors make a right triangle, and Pythagorean's Theorem states that if you have a right-triangle and you know the lengths of two of the sides, then you can find the length of the third side by the following formula:
hypotenuse^2 = leg1^2 + leg2^2 With this formula the length, or magnitude, of the red vector is: sqrt(13) or around 3.61 since the legs have lengths of 2 and 3. A vector also needs a direction and we see that this vector points in both the South and East directions, but how do we write it? |

Vector Arithmetic

You can add and subtract vectors in order to form another vector. Adding vectors that point in the same direction is easy, you just add the magnitudes together and the resulting vector still points in the same direction. Subtracting is also easy, just subtract the magnitudes from each other, however in this case the resulting vector can end up pointing in the opposite direction if the vector you're subtracting from is larger than the first vector. A vector can also be lengthened or contracted by multiplying it by a scalar. Multiplying it by a number larger than 1 will lengthen it, and multiplying it by a number between 0 and 1 will shorten it. For example, 3East times 4 equals 12East and 6East times .5 equals 3East.

Adding two vectors that point in different directions isn't that difficult either. Every vector is composed of two or more vectors depending on how many unique directions there are. For example, in the image below and to the left, the vector 4East is really 4East + 0South. Also, the vector 2South is really 0East + 2South. In order to add these two vectors together, you just add the components together to get the final vector 4East + 2South. The two images on the right show the general method of adding and subtracting two vectors. In subtracting two vectors you're really just adding a vector with the negative of the other vector. The negative of a vector is a vector with the same magnitude, but points in the opposite direction. For example, the negative of East is West and the negative of South is North.

## Change of Coordinates and Unit Vectors

We've been using North, South, East, and West as the directions, but you can use any coordinate system you want. In order to understand how to use vectors in games we'll use the computers coordinate system instead from here on out. The computers coordinate system has the origin or zero point at the top left corner of the screen. The positive x-direction points to the right and the positive y-direction points down. Conversely, the negative x and y point in the opposite directions.
A unit vector is any vector that has a length of 1. In the computer coordinate system there are two basic unit vectors that point in the positive-x direction and positive-y direction. The unit vector in the x-direction is denoted as <1,0> and the unit vector in the y-direction is <0,1>. The unit vectors in the negative x and y directions are <-1,0> and <0,-1> respectfully. You can multiply the unit vectors by any integer to lengthen or shorten it. You can therefore make any vector you want by adding unit vectors together with their respective multipliers. In the example to the left we take the x and y unit vectors, add 2 and 4 to them respectfully, and then add them together: <1,0>*2 + <0,1>*4 = <2,4>. |

You can find the unit vector of any vector by dividing the vector by its magnitude. For example, if our vector is <2,4>, then its magnitude is found by Pythagorean's Theorem which is around 4.47. Divide that number by each value in the vector to get: <0.45, 0.89>. Indeed the magnitude of this vector is 1 which makes it a unit vector. The magnitude of a vector is typically denoted with vertical bars on both sides of the vector. For example, if we have a vector called

**a**, the magnitude is written as**|a|**. Therefore, the unit vector of**a**can be written as**a**/**|a|**. Finding the unit vector of a vector is also known as**normalizing**the vector.Dot Product

Play around with vectors long enough and you may eventually ask yourself a serious question. Lets say you have two vectors as shown below. The question that you may ask yourself is, "what is the angle between the two vectors?" We'll denote the angle between any two vectors as theta. In this case some trigonometry is useful to know. What we'll use to find this is the "Law of Cosines". The Law of Cosines is shown below and how it corresponds to vectors. As we can see, to get the dot product of two vectors we simply multiply the same components in each vector together, then add the results. Therefore, the result of taking the dot product of two vectors is a scalar. To find the angle between the two vectors we just solve for theta in the equation below. This has an important consequence: we can automatically know if two vectors are perpendicular to each other if the dot product of the two vectors is zero. The reason is because when two vectors are perpendicular to each other the angle between them is 90 degrees and cos(90) = 0. The reasons why we would want to know if two vectors are perpendicular are numerous so I won't list them here.

For example, if our two vectors are <1,3> and <5,2>, then the dot product is 1*5 + 3*2 = 11. The magnitude of each vector respectfully is 3.2 and 5.4. Therefore the angle between the two is found to be: theta = arccos(11/(3.2*5.4)) = 50.5 degrees.

For example, if our two vectors are <1,3> and <5,2>, then the dot product is 1*5 + 3*2 = 11. The magnitude of each vector respectfully is 3.2 and 5.4. Therefore the angle between the two is found to be: theta = arccos(11/(3.2*5.4)) = 50.5 degrees.

Vector Projection

The projection of a vector isn't as scary as it sounds. We're all familiar with the basic concept. If you've ever been standing outside on a sunny day and seen your shadow on the ground, then you've just experienced the concept of vector projection. You're shadow is basically the projection of you onto the ground.

Let's now say that we have two vectors

What happens when we take the projection of a vector

Let's now say that we have two vectors

**a**and**b**. We want to find the projection of**a**onto**b**. What we're essentially asking is what component of**a**points in the same direction as**b**? The direction part is easy since it has the same direction as**b**. To find the magnitude notice that the dot product of**a**and**b**is the magnitude of**b**multiplied by the magnitude of the projection of**a**onto**b**. Look at the image on the right for the reasoning. The projection is then found using the equation in the box in the picture on the right. Put into words, you take the dot product of**a**and**b**, divide the result by the magnitude of**b**squared, then multiply that result by each component of**b**.What happens when we take the projection of a vector

**a**onto the unit vector <1,0> which is the unit vector in the positive x-direction? The magnitude of <1,0> is just 1 since it is a unit vector and the dot product of**a**with <1,0> is just the x-component of**a**, therefore the projection of**a**onto <1,0> is simply the x-component of**a**. So the x and y components of a vector are just the projections of the vector onto the x and y axis. But our equation is more basic since it allows us to project a vector onto anything we want.This only scratches the surface of vectors, and there's a lot more you can do with them. For example, if you're working in 3 dimensions, then the cross product of two vectors is important since this defines a vector that is perpendicular to both vectors.

## Vector2D

Add a file to the directory and call it vectors.py. Inside will be our Vector2D class that will do everything that we defined above. Instead of just listing the contents I'll include the file to download here.

vectors.py | |

File Size: | 3 kb |

File Type: | py |

In the entity.py file you need to import Vector2d at the beginning.

**from vectors import Vector2D**

Then make the pos variable a vector.

**self.pos = Vector2D(pos)**

In the draw() method you'll need to adjust the code for the values variable.

**values = list(self.pos.toTuple()) + list(self.dim)**

In the pacman.py file you'll need to change all instances of

**self.pos[0]**to**self.pos.x**and**self.pos[1]**to**self.pos.y**