Vectors in game development

I'm new to programming and game programming. I've reading something about vectors and math, but I have a question - where do I use vectors in game programming? Maybe anyone can give a simple example where you are using vectors (in 2D)?

I've found examples but mostly they are in the console where they output numbers, and big examples which I don't understand.

• Basic TL;DR Vectors are part of the Linear Algebra topic and lead to Matricies. With Matricies and Linear Algebra you write anything from a Minesweeper solver to a 3D world projection to see what object is under your cursor. Linear Algebra is the single most useful and necessary branch of Mathematics for any game developer. Learn it now; you will not regret it. – Robert Massaioli Jun 4 '11 at 11:17
• Thanks all for all awesome's answers! But why something like don't use Vector's in this tutorial's?: zetcode.com/tutorials/javagamestutorial Or one developer's use other no? – vqwer Jun 4 '11 at 11:59
• Hard to say, probably the author wanted to keep it simple and basic for beginners. – Maik Semder Jun 4 '11 at 12:31
• Actually the author uses them, look at the points-array in class Star here – Maik Semder Jun 4 '11 at 12:37
• Also here is Point2D used in class ResizeRectangle – Maik Semder Jun 4 '11 at 12:41

What are vectors?

Vectors are sets of coordinates of varying dimension. Each coordinate in a vector represents some absolute position in that direction of the space the vector is in.

• A 1-D vector would be {1} . This could be, for example, a position at X = 1. Or a time t = 1.
• A 2-D vector would be {-4,3}. This could be, for example, a position at -4 on the X-axis, and 3 on the Y-axis. It could also be the temperature (3 degrees) at a position (-4 meters) back on the X-axis.
• A 3-D vector would be {1,2,3}. This could be a position in space 1 along the X-axis, 2 back on the Y-axis, and 3 up on the Z-axis. Or it could be 1 red, 2 green, and 3 blue in a color. Or, it could be an XY position ({1,2}) at some time T ({3}).

Note that in all cases, we've assigned meaning to the vectors for our problem. While you will commonly find vectors being used for geometry in games, there is no reason you can't do something else with them.

Why do I use vectors?

First, you never have to use vectors. As long as you are keeping track of x and y, or whatever coordinates you care about, in some way you are fine.

However, the advantage to using vectors is that they neatly represent things such as direction and position, and also have several mathematical operations defined on them that make your life easier.

For a simple example of these, consider the dot product.

Suppose you have a radar system in a top-down style game. Every enemy that appears in the sector of the radar (some pie-shaped wedge in 2D) should get a little red dot in your screen. So, you need to figure out what enemies are in your radar section.

You could test if the enemies are inside a triangle. You could also test if the enemies are contained in the intersection of the two half-spaces of the planes/lines defining the two side of the radar sector.

Or, you could just use a dot product to do the check. Here's how:

1. Create a vector going from the center of the radar out towards the "front of the radar". Normalize it.
2. Create a vector going from the center of the radar out towards the object we want to check the radar visibility of. Normalize it.
3. Take the dot product of the two normalized vectors.
4. Take the arccosine of that product, and check if it is less than half the angle of the width of the radar. If it is, draw a blip.

This is very handy, and also now lets you easily have radars that point in different directions (just change the forward vector) and have different widths (just change the radar width angle)--and you can reuse the same code for those cases too!

Why else do I use vectors?

If you are in 2D, perhaps the best way of achieving complex effects and motions (spinning, scaling, etc.) is to use a scene graph. A planet has an orbiting ship, the ship has an orbiting drone. The calculation for this without using vector math is really, really ugly.

With vector math, we represent each as having a point and a 3x3 transform matrix. The planet uses its transform, the ship uses its transform and the planet's transform, and the drone uses its transform and the ship's transform and the planet's transform.

When the planet moves, you change its transform, and the ship and drone automatically get positioned "for free". Much cleaner code.

Still not convinced. Vectors are also the native representation for position, geometry, and motion used by nearly all graphics libraries--and certainly OpenGL and DirectX. You aren't likely to get away without having to use them.

Conclusion Vectors are a powerful tool for writing clear code that solves geometrical problems cleanly and elegantly.

A 2D example are screen coordinates, it identifies a pixel on the screen and has an x- and an y-component [x, y] i.e. Left upper screen position [0, 0]

Another example: Imagine a text scrolling from right screen border to the left screen border. Now you need to define the velocity of the scrolling text in pixel per second, i.e. [-20, 0] which means the text scrolls 20 pixels to the left per second and never changes the height.

Another more advanced example: Imagine a 2D game that is supposed to run on different screen resolutions 800x600, 1024x768 etc. This can easily be done by internally using a screen width from 0.0 to 1.0 and a height from 0.0 to 1.0 to decouple the game logic from the actual screen resolution. Now when you draw to the screen you just multiply the internal vector with the resolution vector:

screen_pos = internal_pos * screen_ressolution


note, all 3 variables are 2D vectors here, they have an x- and an y-component, i.e. for this internal_pos [0.5, 0.25]:

[400, 150] = [0.5, 0.25] * [800, 600]


So internal position [0.5, 0.25] is transformed to actual screen position [400, 150]

This was the basic stuff. The real advantage of vectors is the application in Linear Algebra where you can use matrices to transform your vertices (rotate, scale, mirror etc), i.e. to easily rotate all your internal position by 90 degrees, or you have to swap the screen-y position 0 from top to the bottom of the screen, because i.e. a third party library that you use, uses this convention.

• Isn't a vector a single dimension array, like a list of some sort? When we're talking about screen resolution, aren't we speaking of multidimensional array (one coordinate for each X and Y axis)? Just to make sure that 'vector' is not mixed up with a matrix here. =) – Will Marcouiller Jun 3 '11 at 18:28
• @Will the complete pixel data for the screen can be treated as a multidimensional array, basically a bitmap, but the values for width and height, in other words the resolution, can not – Maik Semder Jun 3 '11 at 18:43
• Note that a vector is often treated quite differently in mathematics and in programming. Mathematically speaking, a vector is not a multidimensional array, although its components with respect to some basis together define such an array. The vector itself is coordinate invariant. The operation screen_pos = internal_pos * screen_resolution is not coordinate invariant the way you have written it, it could more appropriately be written screen_pos = map_to_screen * internal_pos, where map_to_screen is a linear mapping (which can be written as a matrix, in this case a diagonal one). – leftaroundabout Jun 3 '11 at 18:55

Here's a great explanation of vectors in game development on Wolfire Games blog:

http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/

• This is currently a link-only answer. Please consider including a rough summary of the main points you hope a reader might glean from this link, so the answer can stand on its own even if the link changes, breaks, or becomes unavailable in future. – DMGregory Dec 15 '18 at 14:07

A vector is an entity that has both a value and a direction. Examples of vectors in the real world and physics based games include velocity and momentum. Properties that have only values but no direction are called scalars and include location, mass, density and so forth.

Vectors are needed for games that emulate physical properties that are vector like (as mentioned - speed, acceleration and so forth). The mathematics that are used for vector calculations are called linear algebra.

• Speed is a scalar, its the length of the velocity vector – Maik Semder Jun 3 '11 at 15:27
• Correct - fixed – Eran Galperin Jun 3 '11 at 15:34
• and location usualy is considered as vector, it's distance that is scalar. – Ali1S232 Jun 3 '11 at 16:34
• Position can be considered both a scalar (or a collection of scalars) or a vector that is pointing from the axis starting point. – Eran Galperin Jun 3 '11 at 17:26

Anywhere where you have a number for each dimension to represent something, the collection of these numbers can be considered a vector. Position, velocity and acceleration are the prime examples of vectors. It can in some cases also be practical to represent direction of facing as a vector.

For basic stuff it doesn't really matter whether or not you consider these numbers to be vectors, but if you want to do any kind of physics you ought to look into vector maths.

• Position is not a vector – Eran Galperin Jun 3 '11 at 15:09
• Neither is speed, its a scalar, velocity is a vector – Maik Semder Jun 3 '11 at 15:25
• @Eran Galperin I know that is a pretty widespread view amongst mathematicians. The distinction between a point and it's corresponding position vector is however quite academic. There is no practical reason to make a fuss about the distinction. – aaaaaaaaaaaa Jun 3 '11 at 15:34
• There are practical reasons, once you work with 4D homogenious coordinates and matrices, you have to make that distinction. Though it is not relevant for the scope of this question. – Maik Semder Jun 3 '11 at 15:41
• @eBusiness it is not a "view" it is a fact. And I'm a physicist by education, not a mathematician. – Eran Galperin Jun 3 '11 at 16:07

Very simply, anything with a position, or a direction, which is everywhere in a game they use vectors. A vector is like a point

struct Point2
{
float x, y;
};

struct Vector2
{
float x, y;
};


However the difference really comes down to this. A point is just a dot, while a vector is an Arrow.

if you have

Point2.x = 5;

Point2.y = 10;


your saying that I mean at this location x 5 and y 10.

however when you declare a vector...

Vector2.x = 5;

Vector2.y = 10;


Your really saying im declaring an arrow from 0,0 to x 5 , y 10;

you can even have the point from which your vector is pointing from be a point in space from anywhere, for example lets use a point and a vector to move our object we'll use a Point2 to store its location and a vector2 to move it.

point2.x = 10;

point2.y = 15;


now you can use a vector to move this point, lets say we want to move this point up the x axis 10 units so you have

vector2.x = 10;

vector2.y = 0;

point2 += vector2;


now point has moved where your vector arrow told it to.

point now is

point2.x = 20;

point2.y = 15;


One last thing to note is sometimes a vector is used like a point and vice versa just because they hold the same type of data.

• A point is a vector. It's a vector from the origin to the point P. – The Communist Duck Jun 4 '11 at 8:57
• @Duck technically speaking that's not correct, referring to homogeneous coordinates that vector can be found by subtracting the origin_point from the position_point, but that does not make them equal: v = pos - origin so v != pos since origin is a point {0, 0, 0, 1} – Maik Semder Jun 4 '11 at 12:18
• @Duck: A point is not a vector but a point and the origin define a vector, which is just as good as most of the time if your origin is 0. – user744 Jun 4 '11 at 20:58
• @Duck then why did you call it your point P? LOL – EddieV223 Jun 7 '11 at 7:36