Game Development Reference
In-Depth Information
If we want to find the distance vector from position a to position b , we use the following general
formula:
dba
=−
In other words, always subtract the start position from the end position. That's a little confusing
at first, but if you think about it, it makes absolute sense. Try it out on some graph paper!
We can also multiply a vector by a scalar (remember, a scalar is just a single value):
(
)
'
a
=
a scalar
*
=
a.x * scalar, a.y * sc l r
aa
We multiply each of the components of the vector by the scalar. This allows us to scale the
length of a vector. Take the direction vector in Figure 8-1 as an example. It's specified as
d = (0,-1). If we multiply it with the scalar s = 2, we effectively double its length:
d × s = (0,-1 × 2) = (0,-2). We can, of course, make it smaller, by using a scalar less than
1—for example, d multiplied by s = 0.5 creates a new vector d ' = (0,-0.5).
Speaking of length, we can also calculate the length of a vector (in the units it's given in):
(
)
a
=
sqrt a.x * a.x
+
a.y * a.y
The |a| notation simply explains that this represents the length of the vector. If you didn't
sleep through your linear algebra class at school, you might recognize the formula for the
vector length. It's simply the Pythagorean theorem applied to our fancy 2D vector. The x and y
components of the vector form two sides of a right triangle, and the third side is the length of the
vector. Figure 8-2 illustrates this.
Figure 8-2. Pythagoras would love vectors too
The vector length is always positive or zero, given the properties of the square root. If we apply
this to the distance vector between the red Bob and the green Bob, we can figure out how far
apart they are from each other (if their positions are given in meters):
(
)
( )
( )
pr
− =
pg
sqrt 5* 5
+− − =
3*
3
sqrt 25
+ =
9
sqrt 34 ~ 5.83m
=
Note that if we calculated | pg - pr |, we'd arrive at the same value, as the length is independent of
the direction of the vector. This new knowledge also has another implication: when we multiply
a vector with a scalar, its length changes accordingly. Given a vector d = (0,-1), with an original
length of 1 unit, you can multiply it by 2.5 and arrive at a new vector, with a length of 2.5 units.
 
Search WWH ::




Custom Search