Vector Math for Video Games
This document outlines the common vector math formulas and terminology that is used for building 2d games.
When introducing a new concept such as vectors, real life examples assist in the learning process. A real life example of a vector is an analog clock.
Analog clocks have hands to represent time. These hands could also represent vectors. The hands much like vectors have different lengths or magnitudes. The hands also have different directions just like the vector.
A quantity possessing both magnitude and direction, represented by an arrow the direction of which indicates the direction of the quantity and the length of which is proportional to the magnitude. We can represent vectors in our games to determine how to move entities in relation to each other.
The size, extent, or length of a Vector.
The position or orientation of a vector. Vectors point into different directions in space.
In video games, vectors are used to communicate relationships of objects in space. They can be used to move objects like players or even projectiles. Vector math might be tedious by hand, but for a computer, multiple iterations trivial. Computers are great at iteration!
A vector consists of a point in space. This document assumes knowledge about coordinates of points (x,y).
Math has developed many techniques to help describe objects in space. I have documented the most common vector operations used when creating 2d games.
In the following examples we will describe two vectors, v1 and v2.
Two vectors can be added together to form a new vector. To perform vector addition, add the x and y coordinates.
( v1.x + v2.x, v1.y + v2.y ) = ( v3.x, v3.y )
v1 = (3,4)v2 = (4,6)v3 = (3+4, 4+6) = (7, 10)
Vector addition is just addition of coordinate pairs. Simple right?
Two vectors can be subtracted from each other to form a new vector. To perform vector subtraction, subtract the x and y coordinates.
( v1.x - v2.x, v1.y - v2.y ) = ( v3.x, v3.y )
v1 = (4, 2)v2 = (3, 1)v3 = (4-3, 2-1) = (1, 1)
Vector subtraction is just subtraction of coordinate pairs. Again, simple right?
A vectors magnitude (distance or length) can be calculated by the following formula:
Magnitude = sqrt( x^2 + y^2 )
v1 = (3,4)Magnitude = sqrt( 3^2 + 4^2 ) = sqrt( 9 + 16 ) = sqrt( 25 ) = 5
Magnitude doesn't always work out into a nice integer like 5 but the formula should make it easy to calculate.
In mathematics, a unit vector can be computed for any vector. A unit vector has the same direction as its parent but its length is 1 (the unit length). The unit vector is very important in video games.
Unit Vector = ( x / magnitude, y / magnitude )
v1 = (3,4)Magnitude = 5Unit Vector = (3/5, 4/5)
A vector can be multiplied or scaled by a number (scalar) to grow or shrink its magnitude.
Scaled Vector = ( x * num, y * num )
number or scaler = 3v1 = (3,4)Scaled Vector = (3*3,4*3) = (9,12)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
from math import sqrt class Vector( object ): """ complete 2d vector class. __slots__ prevent a dict from being created when adding attributes to a class. reference http://docs.python.org/reference/datamodel.html#slots """ __slots__ = ('x','y') def __init__(self, x, y): self.x = x self.y = y def get_length( self ): '''return the vectors magnitude (distance or length)''' return sqrt( self.x ** 2 + self.y ** 2 ) def _set_length( self, val ): '''set the vectors magnitude and alter its x and y''' l = self.get_length() self.x *= val / l self.y *= val / l length = property( get_length, _set_length, None, "Gets or Sets vector length" ) def scale( self, val ): '''Scale the vector (grow or shrink)''' return Vector( self.x * val, self.y * val ) def unit( self ): return Vector( self.x / self.length, self.y / self.length) def __sub__( self, v ): return Vector( self.x - v.x, self.y - v.y ) def __add__( self, v ): return Vector( v.x + self.x, v.y + self.y ) def __str__( self ): return '(' + str(self.x) + ', ' + str(self.y) + ')' # Unary Operations def __abs__( self ): return Vector( abs(self.x), abs(self.y) )