Written by Patrik Strandell - mail: Last Edit: March 9, 2011

Vectoring it up – The basic of Vectors and Physics

About this Article

This is an article about vectors and game physics. It is supposed to give you a good base for understanding on how vector physics works in general and how you later can use them in your game. The basics of vectors are pretty simple and they provide a powerful way to add physics to your game. Here I will address what vectors are and how you can make use of them to describe simple physics. I will assume you know the basics of math and the basics of programming while I will assume you don’t know “any” physics. The examples are coded in BlitzMax (see www.blitzmax.com).

Why use vectors?

The reason to use vectors is mainly to simplify mathematical calculations. The vectors don’t cut the amount of math done by the computer, but it’s a very good way to cut the amount of math we need to do and write ourselves. This tutorial will help you realize their simplicity and potential, and give you a good base to build upon which will help you the day you decide to add more advanced physical effects to your game, whether you mimic real-life physics or make up your own.

Where is vectors used?

Vectors are first and foremost used in science and programming. This could be games, quantum physics, any simulations, 3D graphics and mathematical theories. You probably already use them in your game, unaware of the fact that any movement can be (or are) described with vectors. Also do not forget that 3D graphics are built on the very foundation of vectors and projections of vectors. It may look 3D but obviously it is still (most likely) displayed on a flat 2D screen. That transformation from 3D to 2D is done with heavy vector calculations. Read on and you will be well on your way to knowing the full potential of vectors!

What do they do?

Vectors show velocity, force and acceleration and they make it very easy to work with these. To describe a force or acceleration you can’t just use a number. The number only tells you how much we are accelerating, you also have to know from where or to where. Here is where vectors have to be used, because they also specify the direction. With vector we can simplify and say our acceleration is V or A, and if we want to combine two accelerations we just add them V + A = T. Many of the quantities in physics such as velocity, acceleration and force require both an amount and a direction to be fully described; therefore vectors are used to fully describe these quantities. A vector also has the unique ability to exist in any number of dimensions. Lost? No worries, it will become clear soon enough, keep reading!

What is a dimension?

The use of dimensions (or rooms), is way to describe the world and space around us. To understand the concept of dimensions I suggest we start with some examples of how our world would look like in different dimensions and how we in each dimension would do to specify a location.

Point in one dimension (1D)

Imagine you are a train and that you are traveling on a railroad. In this world you can not steer left or right. For you there is only forwards or backwards. Where you are; your location, can be described by how far away you are from a train station. To describe your movement we use a value which tells us your speed. Negative if you travel backwards. Positive if you go forward. Any point on the railroad can in other words be described by a number that says how far away from any other point on the rail. 1D problems are very important because they are so simple and many 2D and 3D problems can be simplified and solved in 1D.

Point in two dimensions (2D)

In two dimensions we have a field, grid or plane instead of a linear rail. Imagine yourself playing a game of chess. You play on a grid of 8 squares wide and 8 squares high. Every square of the grid can be determined by combining the length (in squares) of a game piece if from the top left corner of the grid. Example: Your King is at E3. We know it is 3 squares up and E squares left, where E represents the fifth letter. In a computer that would mean location (3,5). Let us also take your screen as an example. You are probably aware of its resolution. The resolution is the amount of pixels that fits on the screen. So your screen is a grid of pixels. With the top left corner (also referred to as the origin) being (0, 0). The bottom-right pixel would be your resolution, which, for example, could be (1024, 768) or (1600, 1200).

Point in three dimensions (3D)

If we want to know an object’s location in three dimensions, think a fish in a simple square transparent aquarium. The bottom of the aquarium has a flat 2D surface. In this case our aquarium just happens to have the same size as our chess board. So imagine putting the aquarium on top of the chess board and don’t forget to remove the pieces. Now if we look from the top down into our aquarium we see a chess board. Fill the aquarium with water if you haven’t already and put a little fish in there. To determine the location of the fish or a specific point in the aquarium we can simply look at our chess grid. My fish is in square D5. D is the letter five, so with coordinates (4, 5). Wait! There is something missing! We don’t know the height of the fish from the bottom! No problem, we easily can measure the fish swimming 3 “chess grids” above the bottom which makes its height coordinate equal to 3. So our fish’s coordinates in three dimensions is ( 4, 5 ,3 ) - ( X, Y, Z) respectively. X is the amount of squares from the left of the board. Y is the squares from the top of the board and Z is the height of the fish from the bottom.

Point in four dimensions (4D)

I guess the easiest way to describe this is to use time as the fourth dimension. Simply note the time in which the fish where at a certain location and you have 4D: ( X, Y , Z , time). In other words we have a vector with four fields; four dimensions. Just consider that our measurement of time is different to our measurement of space. Anyhow I will not dwell deeper into it.

Point in five or more dimensions

We can keep adding dimensions, mathematically it’s not a problem, but it will be hard to visualize and we won’t have any use for it today. I’ll only work with two dimensions to show you physics and vectors here.

Vectors in 2D

You may visualize a vector in 2D as an arrow; a line from one point to another. In opposite to a line, the vector itself does not contain any data of where it begins or ends. The vector is merely the difference between these two points.

When you work with vectors in school you will write them like this A= (2,3). A is the vector, 2 and 3 are the values of the vector; 2 is the length of the vector in X and 3 the length in Y. It’s common to show that a variable is a vector by writing it in bold or having a line or arrow over it. I’ll write vectors as bold letters.

For example, when you update the movement of your player in your game you add to his x and y position. To go from X=2 to X=3 you add 1 to X. If you want to go from Y=2 to Y=4 you add 2 to y. This movement can be described as the difference from the start point to the end point. Instead of adding one to X and two to Y we combine them and we add a vector called (1, 2) to our current position. (1,2) is the vector seen in the picture to the right. Where this vector start we don’t know. We can move this vector however we want, as long as we keep its direction and length. If you move the vector in the picture to (1,1) the end-point of the vector would point to (2,3). The vector itself would still remain the same; (1, 2).

Say our acceleration from a thruster is vector A. If we want to combine A with the acceleration from gravity G, we could add them G + A = T where T is the resulting acceleration vector. Note that the direction of the gravity often point downwards, but this does not mean gravity is a negative force. The length of a vector can never be negative. Therefore, when we add a vector that points down with a vector that point left we simply get a vector that points down-left. However also note that the values of the vector can be negative.

In the end we added the vectors length and direction and got a vector somewhere in between. I’ll now show how to add vectors mathematically.

Vector Math : Addition

A and B are 2D vectors. A = (2, 4) and B = (3, 5). If we wish to add them we add the X and Y values separate. A + B = (2, 4) + (3, 5). A’s X value = 2, B’s X value = 3. And 2 + 3 = 5. Do the same with Y and you have: (2+3, 4+5), which is the same as (5, 7) and we have a combined vector equal to A + B.

You can also add vectors visually. It is very easy indeed. If you want to add the vectors above on a paper you simply move one of the vectors to the end point of the other. Then you draw a new vector from A’s start point to B’s end point. Erase A and B and we are left with the resulting vector C. See the graphical example below. (here I moved B’s start point to A’s endpoint).

Note that the direction of the C vector is something in-between the A and B vector.

Let us take another example: Imagine a spaceship, it is affected by gravity which pulls it down, the only thing that stops the ship from crashing is the force from the thrusters. You want to get the resulting force on the ship so that we know where/how/if it is accelerating. To do that you simply take the gravity, G-vector and add it to your thrust, T-Vector and you have the total force acting on your ship.

In the example above the resulting vector is called D. If you look at the D vector you will notice it is very short and that it points left and up. In other words, the ship will accelerate along the D vector because it is being affected by two forces (the Trust, and the Gravity!)

Take a look at this example (copy N paste into Blitzmax).

VelocityA:= New Vector

VelocityB:= New Vector

VelocityA.Set( 2, 2 ) 'Down Right

VelocityB.Set( 0,-4 ) 'Up

Print "We have two Vectors:"

VelocityA.Write("A")

VelocityB.Write("B")

Print "Add B to A"

VelocityA.Add( VelocityB )

Print "And A is now:"

VelocityA.Write("A")

Class Vector

Field X,Y

Method Add( Vector:Vector2D )

X:+Vector.X

Y:+Vector.Y

End

Method Set( X, Y)

Self.X = X

Self.Y = Y

End

Method Write( Name$ )

Print "Vector "+Name+" ("+X+", "+Y+" )"

End

End

How to code with vectors?

We want to make practical use of vectors in our game. The best solution would be to use a vector library (or you will have to make one yourself). I’ll show some code where I will be using a Vector Library to show you how the end result may look like:

'Taken from the asteriods demo:

If KeyDown( Key_Up )

Thruster = New Vector

Thruster.SetLength( Ship_Acceleration )

Thruster.SetDir( Ship_Direction )

Speed.Add( Thruster )

End

'Add Gravity – Note this is a special case, where Acceleration = Force

Speed.Add( Gravity )

Turning vectors into physics and mechanics

Let’s say you have a fair understanding that a vector has a length and a direction, that it is used to describe such things as forces, accelerations, positions and velocity. Now it’s time to put this system together so we can have some real use of them. We are now going to model (i.e. describe and simulate with code) physics. You must understand that to model physics we have to make idealizations to approximate the real world and this is no-where near any exact science. When it comes to game programming, the most important thing is that the feeling is right. I will start with the theories and end with a huge example.

Physics in game programming

Index ------

·  Position

·  Velocity

·  Acceleration

·  Forces and inertia

o  Gravity and Weight

o  Friction

o  Wind/Air resistance

o  Bounce – elasticity

Position

An objects position can be described as a vector from the world origin to the object itself. A 2D vector consists of two values, X and Y. A position or point in 2D have the same data, so what we do is that we use some vectors to describe positions. The top-left corner of the screen is our origin and is therefore position (0, 0).

We need one position vector for each object we want to draw. To draw your spaceship with a position vector you could do:

DrawImage( Ship.Image, Ship.Position.X, Ship.Position.Y )

Imagine yourself buying a map of the local area in a store. After having walked away from the store for a couple of minutes you check your new map. You mark to store and you mark your position. To determine your position you could draw a line from the store to you. This is if we use the store as our origin. You can use the North Pole as your origin if you want, but then you are very likely to get very large numbers. The store being the origin and this line is you position vector. Every step you take will add to this position vector, and the position vector will get longer as you get further away to the store. The magnitude or length of the position vector is the distance away from the store you are, as if you measured the line, to you from the store, on the map. Also consider that the distance to the store from you never can get negative, right?