vector-math-for-video-games

JSON

rev 26  |  foxhop  |  1308706298000  |  JSON

rev 25
rev 26
143143
144.. code-block:: python144.. code-block:: python
tt145 :linenos:
145146
146 from math import sqrt147 from math import sqrt
rev 25  |  foxhop  |  1308493719000  |  JSON

rev 24
rev 25
12Analog clocks have hands to represent time.  These hands could also represent ve12Analog clocks have hands to represent time.  These hands could also represent ve
>ctors.  The hands much like vectors have different lengths or magnitudes.  The h>ctors.  The hands much like vectors have different lengths or magnitudes.  The h
>ands also have different directions just like the vector.>ands also have different directions just like the vector.
1313
t14|t
15|14|
1615
rev 24  |  foxhop  |  1308493709000  |  JSON

rev 23
rev 24
1111
12Analog clocks have hands to represent time.  These hands could also represent ve12Analog clocks have hands to represent time.  These hands could also represent ve
>ctors.  The hands much like vectors have different lengths or magnitudes.  The h>ctors.  The hands much like vectors have different lengths or magnitudes.  The h
>ands also have different directions just like the vector.>ands also have different directions just like the vector.
tt13 
14|
15|
1316
14.. contents::17.. contents::
rev 23  |  foxhop  |  1308493695000  |  JSON

rev 22
rev 23
4.. image:: http://www.foxhop.net/attachment/clock.png4.. image:: http://www.foxhop.net/attachment/clock.png
5  :alt: analog clock vector example 5  :alt: analog clock vector example 
t6  :align: rightt6  :align: left
77
8This document outlines the common vector math formulas and terminology that is u8This document outlines the common vector math formulas and terminology that is u
>sed for building 2d games.>sed for building 2d games.
rev 22  |  foxhop  |  1308493372000  |  JSON

rev 21
rev 22
2=============================2=============================
33
n4.. image:: http://www.foxhop.net/attachment/clock.jpgn4.. image:: http://www.foxhop.net/attachment/clock.png
5  :alt: analog clock vector example 5  :alt: analog clock vector example 
6  :align: right6  :align: right
8This document outlines the common vector math formulas and terminology that is u8This document outlines the common vector math formulas and terminology that is u
>sed for building 2d games.>sed for building 2d games.
99
t10When introducing a new concepts such as vectors, real life examples assist in tht10When introducing a new concepts such as vectors, real life examples assist in th
>e learning process. A real life example of a vector is an analog clock.  Analog >e learning process. A real life example of a vector is an analog clock.  
>clocks have hands to represent time.  These hands could also represent vectors.  
> The hands much like vectors have different lengths or magnitudes.  The hands al 
>so have different directions just like the vector. 
11 
12Analog clocks have hands to represent time.  These hands could also represent ve
 >ctors.  The hands much like vectors have different lengths or magnitudes.  The h
 >ands also have different directions just like the vector.
1113
12.. contents::14.. contents::
rev 21  |  foxhop  |  1308493228000  |  JSON

rev 20
rev 21
6  :align: right6  :align: right
77
n8 n
9 
10This document outlines the common vector math formulas and terminology that is u8This document outlines the common vector math formulas and terminology that is u
>sed for building 2d games.>sed for building 2d games.
119
12When introducing a new concepts such as vectors, real life examples assist in th10When introducing a new concepts such as vectors, real life examples assist in th
>e learning process. A real life example of a vector is an analog clock.  Analog >e 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. >clocks have hands to represent time.  These hands could also represent vectors. 
> The hands much like vectors have different lengths or magnitudes.  The hands al> The hands much like vectors have different lengths or magnitudes.  The hands al
>so have different directions just like the vector.>so have different directions just like the vector.
tt11 
12.. contents::
1313
14Terminology14Terminology
rev 20  |  foxhop  |  1296380261000  |  JSON

rev 19
rev 20
144 class Vector( object ):144 class Vector( object ):
145145
n146     __slots__ = ('x','y','length')n146    __slots__ = ('x','y')
147147
n148     def __init__(self, x, y):n
149         self.x = x
150         self.y = y
151         self.length = self.getMagnitude() # distance
152148
nn149    def __init__(self, x, y):
150        self.x = x
151        self.y = y
152 
153     def getMagnitude( self ):153    def getLength( self ):
154         '''return the vectors magnitude (distance or length)'''154        '''return the vectors magnitude (distance or length)'''
155         return sqrt( pow( self.x,+ pow( self.y,2 ) )155        return sqrt( self.x ** 2 + self.y ** 2 )
156 
157    def _setLength( self, val ):
158        '''set the vectors magnitude and alter its x and y'''
159        l = self.getLength()
160        self.x *= val / l
161        self.y *= val / l
162 
163    length = property(
164        getLength, 
165        _setLength, 
166        None, "Gets or Sets vector length"
167    )
168 
169    def scale( self, val ):
170        '''Scale the vector (grow or shrink)'''
171        return Vector( self.x * val, self.y * val )
172 
173    def unit( self ):
174        return Vector( self.x / self.length, self.y / self.length)
175 
176    def __sub__( self, v ):
177        return Vector( self.x - v.x, self.y - v.y )
178 
179    def __add__( self, v ):
180        return Vector( v.x + self.x, v.y + self.y )
181 
182    def __str__( self ):
183        return '(' + str(self.x) + ', ' + str(self.y) + ')'
184 
185    # Unary Operations
186 
187    def __abs__( self ):
188        return Vector( abs(self.x), abs(self.y) )
189 
156 190 
t157     def scale( self, num ):t
158         '''Scale the vector (grow or shrink)'''
159         return Vector( self.x * num, self.y * num )
160 
161     def unit( self ):
162         return Vector( self.x / self.length, self.y / self.length)
163 
164     def __sub__( self, v ):
165         return Vector( v.x - self.x, v.y - self.y )
166 
167     def __add__( self, v ):
168         return Vector( v.x + self.x, v.y + self.y )
169 
170     def __str__( self ):
171         return '(' + str(self.x) + ', ' + str(self.y) + ')'
172 
rev 19  |  foxhop  |  1295652057000  |  JSON

rev 18
rev 19
104```````````````````104```````````````````
105105
t106In mathematics, a unit vector can be computed for any vector.  A unit vector hast106In mathematics, a unit vector can be computed for any vector.  A unit vector has
> the same direction as its parent its length is 1 (the unit length).  The unit v> the same direction as its parent but its length is 1 (the unit length).  The un
>ector is very important in video games.>it vector is very important in video games.
107107
108**Syntax:**108**Syntax:**
rev 18  |  foxhop  |  1295651753000  |  JSON

rev 17
rev 18
33--------------------------------33--------------------------------
3434
t35Vectors help keep track of space in games.  Vectors are used to move players andt35In video games, vectors are used to communicate relationships of objects in spac
> projectiles.  Once you learn how to do vector math, teaching the computer how i>e.  They can be used to move objects like players or even projectiles.  Vector m
>s lots of fun.  Yes, programming is sort of like teaching a computer how to perf>ath might be tedious by hand, but multiple iterations for a computer is non triv
>orm an action.>ial.  Computers are great at iteration!
3636
37What is a vector?37What is a vector?
rev 17  |  foxhop  |  1295651526000  |  JSON

rev 16
rev 17
42Math has developed many techniques to help describe objects in space.  I have do42Math has developed many techniques to help describe objects in space.  I have do
>cumented the most common vector operations used when creating 2d games.>cumented the most common vector operations used when creating 2d games.
4343
t44Vector Techniques for 2d gamest44Vector Operations for 2d games
45----------------------------------45----------------------------------
4646
rev 16  |  foxhop  |  1295651490000  |  JSON

rev 15
rev 16
40A vector consists of a point in space.  This document assumes knowledge about co40A vector consists of a point in space.  This document assumes knowledge about co
>ordinates of points (x,y).  >ordinates of points (x,y).  
4141
t42Math has developed many techniques to help discribe objects in space by using vet42Math has developed many techniques to help describe objects in space.  I have do
>ctor techniques.  I'm going to document the vector techniques used when creating>cumented the most common vector operations used when creating 2d games.
> 2d games. 
4343
44Vector Techniques for 2d games44Vector Techniques for 2d games
rev 15  |  foxhop  |  1295651137000  |  JSON

rev 14
rev 15
133133
134134
tt135Python Vector Class Object
136-------------------------------
137 
138*vector.py*
139 
140.. code-block:: python
141 
142 from math import sqrt
143 
144 class Vector( object ):
145 
146     __slots__ = ('x','y','length')
147 
148     def __init__(self, x, y):
149         self.x = x
150         self.y = y
151         self.length = self.getMagnitude() # distance
152 
153     def getMagnitude( self ):
154         '''return the vectors magnitude (distance or length)'''
155         return sqrt( pow( self.x,2 ) + pow( self.y,2 ) )
156 
157     def scale( self, num ):
158         '''Scale the vector (grow or shrink)'''
159         return Vector( self.x * num, self.y * num )
160 
161     def unit( self ):
162         return Vector( self.x / self.length, self.y / self.length)
163 
164     def __sub__( self, v ):
165         return Vector( v.x - self.x, v.y - self.y )
166 
167     def __add__( self, v ):
168         return Vector( v.x + self.x, v.y + self.y )
169 
170     def __str__( self ):
171         return '(' + str(self.x) + ', ' + str(self.y) + ')'
172 
rev 14  |  foxhop  |  1295650813000  |  JSON

rev 13
rev 14
f1Vector Math for Video Gamesf1Vector Math for Video Games
2=============================2=============================
tt3 
4.. image:: http://www.foxhop.net/attachment/clock.jpg
5  :alt: analog clock vector example 
6  :align: right
7 
8 
39
4This document outlines the common vector math formulas and terminology that is u10This document outlines the common vector math formulas and terminology that is u
>sed for building 2d games.>sed for building 2d games.
rev 13  |  foxhop  |  1295650330000  |  JSON

rev 12
rev 13
110 | Unit Vector = (3/5, 4/5)110 | Unit Vector = (3/5, 4/5)
111111
tt112 
113Scale a Vector
114````````````````````
115 
116A vector can be multiplied or scaled by a number (scalar) to grow or shrink its 
 >magnitude.  
117 
118**Syntax**
119    
120 | Scaled Vector = ( x * num, y * num )
121 
122**Example** 
123 
124 | number or scaler = 3
125 | v1 = (3,4)
126 | Scaled Vector = (3*3,4*3) = (9,12)
127 
128 
rev 12  |  foxhop  |  1295649225000  |  JSON

rev 11
rev 12
102**Syntax:**102**Syntax:**
103103
n104 | unit vector = ( x / magnitude, y / magnitude )n104 | Unit Vector = ( x / magnitude, y / magnitude )
105105
106**Example:**106**Example:**
108 | v1 = (3,4)108 | v1 = (3,4)
109 | Magnitude = 5109 | Magnitude = 5
t110 | unit vector = (3/5, 4/5)t110 | Unit Vector = (3/5, 4/5)
111111
rev 11  |  foxhop  |  1295649181000  |  JSON

rev 10
rev 11
94Magnitude doesn't always work out into a nice integer like 5 but the formula sho94Magnitude doesn't always work out into a nice integer like 5 but the formula sho
>uld make it easy to calculate.  >uld make it easy to calculate.  
9595
t96    t96 
97Unit Vector
98```````````````````
99 
100In mathematics, a unit vector can be computed for any vector.  A unit vector has
 > the same direction as its parent its length is 1 (the unit length).  The unit v
 >ector is very important in video games.
101 
102**Syntax:**
103 
104 | unit vector = ( x / magnitude, y / magnitude )
105 
106**Example:**
107  
108 | v1 = (3,4)
109 | Magnitude = 5
110 | unit vector = (3/5, 4/5)
111 
rev 10  |  foxhop  |  1295646384000  |  JSON

rev 9
rev 10
8484
85**Syntax**85**Syntax**
tt86 
86 | Magnitude = sqrt( x^2 + y^2 )87 | Magnitude = sqrt( x^2 + y^2 )
87  88  
rev 9  |  foxhop  |  1295646330000  |  JSON

rev 8
rev 9
8989
90 | v1 = (3,4)90 | v1 = (3,4)
t91 | Magnitude = sqrt( 3^2 + 4^2 )t
92 | Magnitude = sqrt( 9 + 16 ) = sqrt( 25 ) = 591 | Magnitude = sqrt( 3^2 + 4^2 ) = sqrt( 9 + 16 ) = sqrt( 25 ) = 5
9392
94Magnitude doesn't always work out into a nice integer like 5 but the formula sho93Magnitude doesn't always work out into a nice integer like 5 but the formula sho
>uld make it easy to calculate.  >uld make it easy to calculate.  
rev 8  |  foxhop  |  1295646250000  |  JSON

rev 7
rev 8
7878
7979
nn80Vector Magnitude
81```````````````````
82 
83A vectors magnitude (distance or length) can be calculated by the following form
 >ula:
84 
85**Syntax**
86 | Magnitude = sqrt( x^2 + y^2 )
80  87  
tt88**Example**
89 
90 | v1 = (3,4)
91 | Magnitude = sqrt( 3^2 + 4^2 )
92 | Magnitude = sqrt( 9 + 16 ) = sqrt( 25 ) = 5
93 
94Magnitude doesn't always work out into a nice integer like 5 but the formula sho
 >uld make it easy to calculate.  
8195
82    96    
rev 7  |  foxhop  |  1295644095000  |  JSON

rev 6
rev 7
1010
11**Vector**11**Vector**
nn12 
12  A quantity possessing both magnitude and direction, represented by an arrow th13  A quantity possessing both magnitude and direction, represented by an arrow th
>e direction of which indicates the direction of the quantity and the length of w>e direction of which indicates the direction of the quantity and the length of w
>hich is proportional to the magnitude.  We can represent vectors in our games to>hich is proportional to the magnitude.  We can represent vectors in our games to
> determine how to move entities in relation to each other.  > determine how to move entities in relation to each other.  
1314
n14|n15**Magnitude**
1516
n16**Magnitude**n
17  The size, extent, or length of a Vector.  17  The size, extent, or length of a Vector.  
1818
n19|n
2019
21**Direction**20**Direction**
tt21 
22  The position or orientation of a vector.  Vectors point into different directi22  The position or orientation of a vector.  Vectors point into different directi
>ons in space.>ons in space.
2323
rev 6  |  foxhop  |  1295644036000  |  JSON

rev 5
rev 6
46Two vectors can be added together to form a new vector.  To perform vector addit46Two vectors can be added together to form a new vector.  To perform vector addit
>ion, add the x and y coordinates.>ion, add the x and y coordinates.
4747
n48Syntax:   n48**Syntax:**   
4949
n50  ( v1.x + v2.x, v1.y + v2.y ) = ( v3.x, v3.y )n50 | ( v1.x + v2.x, v1.y + v2.y ) = ( v3.x, v3.y )
51  51  
n52Example:  n52**Example:**  
5353
n54|  v1 = (3,4)n54 |  v1 = (3,4)
55|  v2 = (4,6)55 |  v2 = (4,6)
56|  v3 = (3+4,4+6) = (7,10)56 |  v3 = (3+4,4+6) = (7,10)
5757
58Vector addition is just addition of coordinate pairs. Simple right?58Vector addition is just addition of coordinate pairs. Simple right?
6666
67**Syntax**67**Syntax**
nn68 
68  ( v1.x - v2.x, v1.y - v2.y ) = ( v3.x, v3.y )69 | ( v1.x - v2.x, v1.y - v2.y ) = ( v3.x, v3.y )
6970
70**Example**71**Example**
7172
t72|  v1 = (4,2)t73 | v1 = (4,2)
73|  v2 = (3,1)74 | v2 = (3,1)
74|  v3 = (4-3,2-1) = (1,1)75 | v3 = (4-3,2-1) = (1,1)
7576
76Vector addition is just subtraction of coordinate pairs. Again, simple right?77Vector addition is just subtraction of coordinate pairs. Again, simple right?
rev 5  |  foxhop  |  1295643885000  |  JSON

rev 4
rev 5
41In the following examples we will describe two vectors, v1 and v2.41In the following examples we will describe two vectors, v1 and v2.
42 42 
n43**Vector Addition**n43Vector Addition
44  Two vectors can be added together to form a new vector.  To perform vector add44``````````````````
>ition, add the x and y coordinates. 
4545
nn46Two vectors can be added together to form a new vector.  To perform vector addit
 >ion, add the x and y coordinates.
47 
46  Syntax:   48Syntax:   
49 
47  ( v1.x + v2.x, v1.y + v2.y ) = ( v3.x, v3.y )50  ( v1.x + v2.x, v1.y + v2.y ) = ( v3.x, v3.y )
48  51  
n49  Example:  n52Example:  
50  v1 = (3,4)
51  v2 = (4,6)
52  v3 = (3+4,4+6) = (7,10)
5353
n54  Vector addition is just addition of coordinate pairs. Simple right?n54|  v1 = (3,4)
55|  v2 = (4,6)
56|  v3 = (3+4,4+6) = (7,10)
5557
n56**Vector Subtraction**n58Vector addition is just addition of coordinate pairs. Simple right?
57  Two vectors can be subtracted from each other to form a new vector.  To perfor
>m vector subtraction, subtract the x and y coordinates. 
5859
n59  Syntax:n60 
61 
62Vector Subtraction
63`````````````````````````````
64 
65Two vectors can be subtracted from each other to form a new vector.  To perform 
 >vector subtraction, subtract the x and y coordinates.
66 
67**Syntax**
60  ( v1.x - v2.x, v1.y - v2.y ) = ( v3.x, v3.y )68  ( v1.x - v2.x, v1.y - v2.y ) = ( v3.x, v3.y )
6169
n62  Example:n70**Example**
63  v1 = (4,2)
64  v2 = (3,1)
65  v3 = (4-3,2-1) = (1,1)
6671
tt72|  v1 = (4,2)
73|  v2 = (3,1)
74|  v3 = (4-3,2-1) = (1,1)
75 
67  Vector addition is just subtraction of coordinate pairs. Again, simple right?76Vector addition is just subtraction of coordinate pairs. Again, simple right?
6877
6978
rev 4  |  foxhop  |  1295643556000  |  JSON

rev 3
rev 4
42 42 
43**Vector Addition**43**Vector Addition**
n44  Two vectors can be added together to form a new vector.  Perform vector additin44  Two vectors can be added together to form a new vector.  To perform vector add
>on add the x and y coordinates together.>ition, add the x and y coordinates.
4545
46  Syntax:   46  Syntax:   
52  v3 = (3+4,4+6) = (7,10)52  v3 = (3+4,4+6) = (7,10)
5353
t54  Simple right?  Basically it is just addition with pairs of numbers. t54  Vector addition is just addition of coordinate pairs. Simple right?
55 
56**Vector Subtraction**
57  Two vectors can be subtracted from each other to form a new vector.  To perfor
 >m vector subtraction, subtract the x and y coordinates.
58 
59  Syntax:
60  ( v1.x - v2.x, v1.y - v2.y ) = ( v3.x, v3.y )
61 
62  Example:
63  v1 = (4,2)
64  v2 = (3,1)
65  v3 = (4-3,2-1) = (1,1)
66 
67  Vector addition is just subtraction of coordinate pairs. Again, simple right?
68 
69 
55  70  
5671
rev 3  |  foxhop  |  1295642733000  |  JSON

rev 2
rev 3
29Vectors help keep track of space in games.  Vectors are used to move players and29Vectors help keep track of space in games.  Vectors are used to move players and
> projectiles.  Once you learn how to do vector math, teaching the computer how i> projectiles.  Once you learn how to do vector math, teaching the computer how i
>s lots of fun.  Yes, programming is sort of like teaching a computer how to perf>s lots of fun.  Yes, programming is sort of like teaching a computer how to perf
>orm an action.>orm an action.
3030
n31What is a vectorn31What is a vector?
32---------------------32---------------------
3333
n34A vector consists of two points in space.  This document assumes knowledge aboutn34A vector consists of a point in space.  This document assumes knowledge about co
> points (x,y) coordinates.  If you do not, please research graphing points on a >ordinates of points (x,y).  
>graph.   
3535
n36Math has developed many techniques to find out many things about space using vecn36Math has developed many techniques to help discribe objects in space by using ve
>tor techniques.  I'm going to document the techniques need to create 2d games.>ctor techniques.  I'm going to document the vector techniques used when creating
 > 2d games.
3737
38Vector Techniques for 2d games38Vector Techniques for 2d games
39----------------------------------39----------------------------------
4040
tt41In the following examples we will describe two vectors, v1 and v2.
42 
43**Vector Addition**
44  Two vectors can be added together to form a new vector.  Perform vector additi
 >on add the x and y coordinates together.
45 
46  Syntax:   
47  ( v1.x + v2.x, v1.y + v2.y ) = ( v3.x, v3.y )
48  
49  Example:  
50  v1 = (3,4)
51  v2 = (4,6)
52  v3 = (3+4,4+6) = (7,10)
53 
54  Simple right?  Basically it is just addition with pairs of numbers. 
55  
56 
41    57    
rev 2  |  foxhop  |  1295639207000  |  JSON

rev 1
rev 2
9--------------9--------------
1010
n11Vectorn11**Vector**
12  A quantity possessing both magnitude and direction, represented by an arrow th12  A quantity possessing both magnitude and direction, represented by an arrow th
>e direction of which indicates the direction of the quantity and the length of w>e direction of which indicates the direction of the quantity and the length of w
>hich is proportional to the magnitude.  We can represent vectors in our games to>hich is proportional to the magnitude.  We can represent vectors in our games to
> determine how to move entities in relation to each other.  > determine how to move entities in relation to each other.  
1313
n14[put a vector picture here]n14|
1515
n16Magnituden16**Magnitude**
17  The size, extent, or length of a Vector.  17  The size, extent, or length of a Vector.  
1818
n19[put a vector with large and small magnitude here]n19|
2020
n21Directionn21**Direction**
22  The position or orientation of a vector.  Vectors point into different directi22  The position or orientation of a vector.  Vectors point into different directi
>ons in space.>ons in space.
tt23 
24 
25 
26How do we use vectors in games?
27--------------------------------
28 
29Vectors help keep track of space in games.  Vectors are used to move players and
 > projectiles.  Once you learn how to do vector math, teaching the computer how i
 >s lots of fun.  Yes, programming is sort of like teaching a computer how to perf
 >orm an action.
30 
31What is a vector
32---------------------
33 
34A vector consists of two points in space.  This document assumes knowledge about
 > points (x,y) coordinates.  If you do not, please research graphing points on a 
 >graph.  
35 
36Math has developed many techniques to find out many things about space using vec
 >tor techniques.  I'm going to document the techniques need to create 2d games.
37 
38Vector Techniques for 2d games
39----------------------------------
40 
41    
rev 1  |  foxhop  |  1295638409000  |  JSON

empty
rev 1
tt1Vector Math for Video Games
2=============================
3 
4This document outlines the common vector math formulas and terminology that is u
 >sed for building 2d games.
5 
6When introducing a new concepts such as vectors, real life examples assist in th
 >e 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 al
 >so have different directions just like the vector.
7 
8Terminology
9--------------
10 
11Vector
12  A quantity possessing both magnitude and direction, represented by an arrow th
 >e direction of which indicates the direction of the quantity and the length of w
 >hich is proportional to the magnitude.  We can represent vectors in our games to
 > determine how to move entities in relation to each other.  
13 
14[put a vector picture here]
15 
16Magnitude
17  The size, extent, or length of a Vector.  
18 
19[put a vector with large and small magnitude here]
20 
21Direction
22  The position or orientation of a vector.  Vectors point into different directi
 >ons in space.