Published on

Introduction To Vector In Game Dev

Authors
  • avatar
    Name
    Kamil Demirtaş
    Twitter

Vectors: What are they?

There are many definitions of vectors in many different disciplines. However, more in-depth and mathematical descriptions of them are way beyond this post. Therefore, for simplicity, you can think about them as arrows pointing in a direction in space, or a point in space relative to an origin. Making this simplification helps us conceptualize and understand them better.

For now, let's think of vectors as points in space. If you paid any attention in school you might remember something called a coordinate system.

A coordinate system is used to describe a space in which we can show the position of things. For example, the number line is a coordinate system that is one-dimensional.

line-number

We can use a single number to show the position of things in that one-dimensional space.

If I give you (3) aside from its numerical value it also conveys the position information too. It tells us that it is (3) units away from the origin which is zero.

The sign of it also tells us the direction in which it is facing away from the origin. Since (3) positive it is on the right side of the origin. Therefore, (3) is a vector, a one-dimensional one, albeit not a very useful one.

However, if we bump up dimensions and go to the two-dimensional coordinate system they suddenly become extremely useful. One such system is the cartesian coordinate system, in which there is surprisingly more than one. I'll talk about another coordinate system in the future, especially the polar coordinate system, but for now, let's stick with the Cartesian.

cartesian-coordinate-system

In a two-dimensional coordinate system, we can represent a position using two values. We use X and Y to represent the distance from the origin in the horizontal and vertical directions. If I want to show the position of anything in this space I have to say how far it is from the origin on the vertical axis and how far it is on the horizontal axis. So, if I want to say something is 2 units away from the origin on the horizontal axis and 2 units away on the vertical axis, I can show it as (2,2). Just like our one-dimensional example the sign of the components of (2,2) also tells us the direction in which they are going away from the origin. Let's plot it on the Cartesian coordinate system.

cartesian-coordinate-system

In the cartesian coordinate system, the first part of our vector of (2,2) represents the horizontal displacement, while the second part represents the vertical displacement. On the horizontal axis which from now on we'll use X to demonstrate, positive numbers go on the right side of the origin while negative numbers are on the left side of it. The same thing applies to the vertical axis which from now on we'll use Y to demonstrate. Positive numbers go up and negative numbers go down from the origin. Since we can think of these two axes as two number lines perpendicular to each other, we can represent infinitely possible positions, not just integers. For example (6.485147487456478, 98,41754674134713) is as valid as (0,0) which is our origin.

Why the hell do we care about Vectors?

Now, since we get to know the basics about vectors and coordinate systems, let's get to the fun part. Why do we even care about vectors when we can just point to a thing and show where it is? While we can do that computers cannot. We need to represent our objects in the space they are to the computer for it to be "computed" by the computer.

If we want to draw a point on the screen, how do we tell the computer to draw that point exactly where we want to? Yes, that is right we use a coordinate system to represent the position of our point for the computer to draw.

There is a lot of black magic going on behind the scenes with all sorts of coordinate system transformations and conversions to draw anything on the screen. However, right now none of it is our concern, we can simply use our cartesian coordinates to tell the computer the position of our point and let it do everything else it needs to do to draw it.

Now, imagine the center of your screen as the origin of our coordinate system. That is not how screen coordinates work but for simplicity let's assume they do. Since we can convert one coordinate system to another everything works regardless of what system you use

Now, we can tell the computer which pixels we want it the light up since we know the positions of each and every pixel on our screen. No matter how big or small the screen is or the number of pixels it has they all have a position on our coordinate system.

Great, we know the position of every pixel and tell the computer to turn on or off them, now what? Now, we can do much more. Instead of limiting ourselves to the screen let's imagine an intently big plain. Now we have infinite places to put anything into it.

We can put a cat (5,10) units away from the origin, and a mouse (-5,-5) units away from the origin. We can put anything anywhere we want. But, what if we want the cat to catch the mouse? How do we tell the computer to move the cat to the position of the mouse, in which direction? What about the speed of the cat? The questions go on and on, and the answer to all those questions involves "Vectors". In my next post, I'll talk about how to solve all of these problems using vectors and some properties of vectors.