vector-math-for-video-games
| rev 25 | rev 26 | ||||
|---|---|---|---|---|---|
| 143 | 143 | ||||
| 144 | .. code-block:: python | 144 | .. code-block:: python | ||
| t | t | 145 | :linenos: | ||
| 145 | 146 | ||||
| 146 | from math import sqrt | 147 | from math import sqrt | ||
| rev 24 | rev 25 | ||||
|---|---|---|---|---|---|
| 12 | Analog clocks have hands to represent time. These hands could also represent ve | 12 | Analog 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. | ||
| 13 | 13 | ||||
| t | 14 | | | t | ||
| 15 | | | 14 | | | ||
| 16 | 15 | ||||
| rev 23 | rev 24 | ||||
|---|---|---|---|---|---|
| 11 | 11 | ||||
| 12 | Analog clocks have hands to represent time. These hands could also represent ve | 12 | Analog 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. | ||
| t | t | 13 | |||
| 14 | | | ||||
| 15 | | | ||||
| 13 | 16 | ||||
| 14 | .. contents:: | 17 | .. contents:: | ||
| rev 22 | rev 23 | ||||
|---|---|---|---|---|---|
| 4 | .. image:: http://www.foxhop.net/attachment/clock.png | 4 | .. image:: http://www.foxhop.net/attachment/clock.png | ||
| 5 | :alt: analog clock vector example | 5 | :alt: analog clock vector example | ||
| t | 6 | :align: right | t | 6 | :align: left |
| 7 | 7 | ||||
| 8 | This document outlines the common vector math formulas and terminology that is u | 8 | This document outlines the common vector math formulas and terminology that is u | ||
| > | sed for building 2d games. | > | sed for building 2d games. | ||
| rev 21 | rev 22 | ||||
|---|---|---|---|---|---|
| 2 | ============================= | 2 | ============================= | ||
| 3 | 3 | ||||
| n | 4 | .. image:: http://www.foxhop.net/attachment/clock.jpg | n | 4 | .. image:: http://www.foxhop.net/attachment/clock.png |
| 5 | :alt: analog clock vector example | 5 | :alt: analog clock vector example | ||
| 6 | :align: right | 6 | :align: right | ||
| 8 | This document outlines the common vector math formulas and terminology that is u | 8 | This document outlines the common vector math formulas and terminology that is u | ||
| > | sed for building 2d games. | > | sed for building 2d games. | ||
| 9 | 9 | ||||
| t | 10 | When introducing a new concepts such as vectors, real life examples assist in th | t | 10 | When 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 | |||||
| 12 | Analog 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. | ||||
| 11 | 13 | ||||
| 12 | .. contents:: | 14 | .. contents:: | ||
| rev 20 | rev 21 | ||||
|---|---|---|---|---|---|
| 6 | :align: right | 6 | :align: right | ||
| 7 | 7 | ||||
| n | 8 | n | |||
| 9 | |||||
| 10 | This document outlines the common vector math formulas and terminology that is u | 8 | This document outlines the common vector math formulas and terminology that is u | ||
| > | sed for building 2d games. | > | sed for building 2d games. | ||
| 11 | 9 | ||||
| 12 | When introducing a new concepts such as vectors, real life examples assist in th | 10 | When 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. | ||
| t | t | 11 | |||
| 12 | .. contents:: | ||||
| 13 | 13 | ||||
| 14 | Terminology | 14 | Terminology | ||
| rev 19 | rev 20 | ||||
|---|---|---|---|---|---|
| 144 | class Vector( object ): | 144 | class Vector( object ): | ||
| 145 | 145 | ||||
| n | 146 | __slots__ = ('x','y','length') | n | 146 | __slots__ = ('x','y') |
| 147 | 147 | ||||
| n | 148 | def __init__(self, x, y): | n | ||
| 149 | self.x = x | ||||
| 150 | self.y = y | ||||
| 151 | self.length = self.getMagnitude() # distance | ||||
| 152 | 148 | ||||
| n | n | 149 | 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,2 ) + 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 | ||||
| t | 157 | 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 18 | rev 19 | ||||
|---|---|---|---|---|---|
| 104 | ``````````````````` | 104 | ``````````````````` | ||
| 105 | 105 | ||||
| t | 106 | In mathematics, a unit vector can be computed for any vector. A unit vector has | t | 106 | In 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. | ||
| 107 | 107 | ||||
| 108 | **Syntax:** | 108 | **Syntax:** | ||
| rev 17 | rev 18 | ||||
|---|---|---|---|---|---|
| 33 | -------------------------------- | 33 | -------------------------------- | ||
| 34 | 34 | ||||
| t | 35 | Vectors help keep track of space in games. Vectors are used to move players and | t | 35 | In 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! | ||
| 36 | 36 | ||||
| 37 | What is a vector? | 37 | What is a vector? | ||
| rev 16 | rev 17 | ||||
|---|---|---|---|---|---|
| 42 | Math has developed many techniques to help describe objects in space. I have do | 42 | Math 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. | ||
| 43 | 43 | ||||
| t | 44 | Vector Techniques for 2d games | t | 44 | Vector Operations for 2d games |
| 45 | ---------------------------------- | 45 | ---------------------------------- | ||
| 46 | 46 | ||||
| rev 15 | rev 16 | ||||
|---|---|---|---|---|---|
| 40 | A vector consists of a point in space. This document assumes knowledge about co | 40 | A vector consists of a point in space. This document assumes knowledge about co | ||
| > | ordinates of points (x,y). | > | ordinates of points (x,y). | ||
| 41 | 41 | ||||
| t | 42 | Math has developed many techniques to help discribe objects in space by using ve | t | 42 | Math 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. | ||||
| 43 | 43 | ||||
| 44 | Vector Techniques for 2d games | 44 | Vector Techniques for 2d games | ||
| rev 14 | rev 15 | ||||
|---|---|---|---|---|---|
| 133 | 133 | ||||
| 134 | 134 | ||||
| t | t | 135 | Python 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 13 | rev 14 | ||||
|---|---|---|---|---|---|
| f | 1 | Vector Math for Video Games | f | 1 | Vector Math for Video Games |
| 2 | ============================= | 2 | ============================= | ||
| t | t | 3 | |||
| 4 | .. image:: http://www.foxhop.net/attachment/clock.jpg | ||||
| 5 | :alt: analog clock vector example | ||||
| 6 | :align: right | ||||
| 7 | |||||
| 8 | |||||
| 3 | 9 | ||||
| 4 | This document outlines the common vector math formulas and terminology that is u | 10 | This document outlines the common vector math formulas and terminology that is u | ||
| > | sed for building 2d games. | > | sed for building 2d games. | ||
| rev 12 | rev 13 | ||||
|---|---|---|---|---|---|
| 110 | | Unit Vector = (3/5, 4/5) | 110 | | Unit Vector = (3/5, 4/5) | ||
| 111 | 111 | ||||
| t | t | 112 | |||
| 113 | Scale a Vector | ||||
| 114 | ```````````````````` | ||||
| 115 | |||||
| 116 | A 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 11 | rev 12 | ||||
|---|---|---|---|---|---|
| 102 | **Syntax:** | 102 | **Syntax:** | ||
| 103 | 103 | ||||
| n | 104 | | unit vector = ( x / magnitude, y / magnitude ) | n | 104 | | Unit Vector = ( x / magnitude, y / magnitude ) |
| 105 | 105 | ||||
| 106 | **Example:** | 106 | **Example:** | ||
| 108 | | v1 = (3,4) | 108 | | v1 = (3,4) | ||
| 109 | | Magnitude = 5 | 109 | | Magnitude = 5 | ||
| t | 110 | | unit vector = (3/5, 4/5) | t | 110 | | Unit Vector = (3/5, 4/5) |
| 111 | 111 | ||||
| rev 10 | rev 11 | ||||
|---|---|---|---|---|---|
| 94 | Magnitude doesn't always work out into a nice integer like 5 but the formula sho | 94 | Magnitude 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. | ||
| 95 | 95 | ||||
| t | 96 | t | 96 | ||
| 97 | Unit Vector | ||||
| 98 | ``````````````````` | ||||
| 99 | |||||
| 100 | In 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 9 | rev 10 | ||||
|---|---|---|---|---|---|
| 84 | 84 | ||||
| 85 | **Syntax** | 85 | **Syntax** | ||
| t | t | 86 | |||
| 86 | | Magnitude = sqrt( x^2 + y^2 ) | 87 | | Magnitude = sqrt( x^2 + y^2 ) | ||
| 87 | 88 | ||||
| rev 8 | rev 9 | ||||
|---|---|---|---|---|---|
| 89 | 89 | ||||
| 90 | | v1 = (3,4) | 90 | | v1 = (3,4) | ||
| t | 91 | | Magnitude = sqrt( 3^2 + 4^2 ) | t | ||
| 92 | | Magnitude = sqrt( 9 + 16 ) = sqrt( 25 ) = 5 | 91 | | Magnitude = sqrt( 3^2 + 4^2 ) = sqrt( 9 + 16 ) = sqrt( 25 ) = 5 | ||
| 93 | 92 | ||||
| 94 | Magnitude doesn't always work out into a nice integer like 5 but the formula sho | 93 | Magnitude 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 7 | rev 8 | ||||
|---|---|---|---|---|---|
| 78 | 78 | ||||
| 79 | 79 | ||||
| n | n | 80 | Vector Magnitude | ||
| 81 | ``````````````````` | ||||
| 82 | |||||
| 83 | A 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 | ||||
| t | t | 88 | **Example** | ||
| 89 | |||||
| 90 | | v1 = (3,4) | ||||
| 91 | | Magnitude = sqrt( 3^2 + 4^2 ) | ||||
| 92 | | Magnitude = sqrt( 9 + 16 ) = sqrt( 25 ) = 5 | ||||
| 93 | |||||
| 94 | Magnitude doesn't always work out into a nice integer like 5 but the formula sho | ||||
| > | uld make it easy to calculate. | ||||
| 81 | 95 | ||||
| 82 | 96 | ||||
| rev 6 | rev 7 | ||||
|---|---|---|---|---|---|
| 10 | 10 | ||||
| 11 | **Vector** | 11 | **Vector** | ||
| n | n | 12 | |||
| 12 | A quantity possessing both magnitude and direction, represented by an arrow th | 13 | 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. | ||
| 13 | 14 | ||||
| n | 14 | | | n | 15 | **Magnitude** |
| 15 | 16 | ||||
| n | 16 | **Magnitude** | n | ||
| 17 | The size, extent, or length of a Vector. | 17 | The size, extent, or length of a Vector. | ||
| 18 | 18 | ||||
| n | 19 | | | n | ||
| 20 | 19 | ||||
| 21 | **Direction** | 20 | **Direction** | ||
| t | t | 21 | |||
| 22 | The position or orientation of a vector. Vectors point into different directi | 22 | The position or orientation of a vector. Vectors point into different directi | ||
| > | ons in space. | > | ons in space. | ||
| 23 | 23 | ||||
| rev 5 | rev 6 | ||||
|---|---|---|---|---|---|
| 46 | Two vectors can be added together to form a new vector. To perform vector addit | 46 | Two 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. | ||
| 47 | 47 | ||||
| n | 48 | Syntax: | n | 48 | **Syntax:** |
| 49 | 49 | ||||
| n | 50 | ( v1.x + v2.x, v1.y + v2.y ) = ( v3.x, v3.y ) | n | 50 | | ( v1.x + v2.x, v1.y + v2.y ) = ( v3.x, v3.y ) |
| 51 | 51 | ||||
| n | 52 | Example: | n | 52 | **Example:** |
| 53 | 53 | ||||
| n | 54 | | v1 = (3,4) | n | 54 | | 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) | ||
| 57 | 57 | ||||
| 58 | Vector addition is just addition of coordinate pairs. Simple right? | 58 | Vector addition is just addition of coordinate pairs. Simple right? | ||
| 66 | 66 | ||||
| 67 | **Syntax** | 67 | **Syntax** | ||
| n | n | 68 | |||
| 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 ) | ||
| 69 | 70 | ||||
| 70 | **Example** | 71 | **Example** | ||
| 71 | 72 | ||||
| t | 72 | | v1 = (4,2) | t | 73 | | 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) | ||
| 75 | 76 | ||||
| 76 | Vector addition is just subtraction of coordinate pairs. Again, simple right? | 77 | Vector addition is just subtraction of coordinate pairs. Again, simple right? | ||
| rev 4 | rev 5 | ||||
|---|---|---|---|---|---|
| 41 | In the following examples we will describe two vectors, v1 and v2. | 41 | In the following examples we will describe two vectors, v1 and v2. | ||
| 42 | 42 | ||||
| n | 43 | **Vector Addition** | n | 43 | Vector Addition |
| 44 | Two vectors can be added together to form a new vector. To perform vector add | 44 | `````````````````` | ||
| > | ition, add the x and y coordinates. | ||||
| 45 | 45 | ||||
| n | n | 46 | Two vectors can be added together to form a new vector. To perform vector addit | ||
| > | ion, add the x and y coordinates. | ||||
| 47 | |||||
| 46 | Syntax: | 48 | Syntax: | ||
| 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 | ||||
| n | 49 | Example: | n | 52 | Example: |
| 50 | v1 = (3,4) | ||||
| 51 | v2 = (4,6) | ||||
| 52 | v3 = (3+4,4+6) = (7,10) | ||||
| 53 | 53 | ||||
| n | 54 | Vector addition is just addition of coordinate pairs. Simple right? | n | 54 | | v1 = (3,4) |
| 55 | | v2 = (4,6) | ||||
| 56 | | v3 = (3+4,4+6) = (7,10) | ||||
| 55 | 57 | ||||
| n | 56 | **Vector Subtraction** | n | 58 | Vector 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. | ||||
| 58 | 59 | ||||
| n | 59 | Syntax: | n | 60 | |
| 61 | |||||
| 62 | Vector Subtraction | ||||
| 63 | ````````````````````````````` | ||||
| 64 | |||||
| 65 | Two 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 ) | ||
| 61 | 69 | ||||
| n | 62 | Example: | n | 70 | **Example** |
| 63 | v1 = (4,2) | ||||
| 64 | v2 = (3,1) | ||||
| 65 | v3 = (4-3,2-1) = (1,1) | ||||
| 66 | 71 | ||||
| t | t | 72 | | 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? | 76 | Vector addition is just subtraction of coordinate pairs. Again, simple right? | ||
| 68 | 77 | ||||
| 69 | 78 | ||||
| rev 3 | rev 4 | ||||
|---|---|---|---|---|---|
| 42 | 42 | ||||
| 43 | **Vector Addition** | 43 | **Vector Addition** | ||
| n | 44 | Two vectors can be added together to form a new vector. Perform vector additi | n | 44 | 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. | ||
| 45 | 45 | ||||
| 46 | Syntax: | 46 | Syntax: | ||
| 52 | v3 = (3+4,4+6) = (7,10) | 52 | v3 = (3+4,4+6) = (7,10) | ||
| 53 | 53 | ||||
| t | 54 | Simple right? Basically it is just addition with pairs of numbers. | t | 54 | 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 | ||||
| 56 | 71 | ||||
| rev 2 | rev 3 | ||||
|---|---|---|---|---|---|
| 29 | Vectors help keep track of space in games. Vectors are used to move players and | 29 | Vectors 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. | ||
| 30 | 30 | ||||
| n | 31 | What is a vector | n | 31 | What is a vector? |
| 32 | --------------------- | 32 | --------------------- | ||
| 33 | 33 | ||||
| n | 34 | A vector consists of two points in space. This document assumes knowledge about | n | 34 | A 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. | ||||
| 35 | 35 | ||||
| n | 36 | Math has developed many techniques to find out many things about space using vec | n | 36 | Math 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. | ||||
| 37 | 37 | ||||
| 38 | Vector Techniques for 2d games | 38 | Vector Techniques for 2d games | ||
| 39 | ---------------------------------- | 39 | ---------------------------------- | ||
| 40 | 40 | ||||
| t | t | 41 | In 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 1 | rev 2 | ||||
|---|---|---|---|---|---|
| 9 | -------------- | 9 | -------------- | ||
| 10 | 10 | ||||
| n | 11 | Vector | n | 11 | **Vector** |
| 12 | A quantity possessing both magnitude and direction, represented by an arrow th | 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 | > | 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. | ||
| 13 | 13 | ||||
| n | 14 | [put a vector picture here] | n | 14 | | |
| 15 | 15 | ||||
| n | 16 | Magnitude | n | 16 | **Magnitude** |
| 17 | The size, extent, or length of a Vector. | 17 | The size, extent, or length of a Vector. | ||
| 18 | 18 | ||||
| n | 19 | [put a vector with large and small magnitude here] | n | 19 | | |
| 20 | 20 | ||||
| n | 21 | Direction | n | 21 | **Direction** |
| 22 | The position or orientation of a vector. Vectors point into different directi | 22 | The position or orientation of a vector. Vectors point into different directi | ||
| > | ons in space. | > | ons in space. | ||
| t | t | 23 | |||
| 24 | |||||
| 25 | |||||
| 26 | How do we use vectors in games? | ||||
| 27 | -------------------------------- | ||||
| 28 | |||||
| 29 | Vectors 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 | |||||
| 31 | What is a vector | ||||
| 32 | --------------------- | ||||
| 33 | |||||
| 34 | A 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 | |||||
| 36 | Math 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 | |||||
| 38 | Vector Techniques for 2d games | ||||
| 39 | ---------------------------------- | ||||
| 40 | |||||
| 41 | |||||
| empty | rev 1 | ||||
|---|---|---|---|---|---|
| t | t | 1 | Vector Math for Video Games | ||
| 2 | ============================= | ||||
| 3 | |||||
| 4 | This document outlines the common vector math formulas and terminology that is u | ||||
| > | sed for building 2d games. | ||||
| 5 | |||||
| 6 | When 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 | |||||
| 8 | Terminology | ||||
| 9 | -------------- | ||||
| 10 | |||||
| 11 | Vector | ||||
| 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 | |||||
| 16 | Magnitude | ||||
| 17 | The size, extent, or length of a Vector. | ||||
| 18 | |||||
| 19 | [put a vector with large and small magnitude here] | ||||
| 20 | |||||
| 21 | Direction | ||||
| 22 | The position or orientation of a vector. Vectors point into different directi | ||||
| > | ons in space. | ||||
Remarkbox