Game Development Reference
In-Depth Information
To find the cross product of two 2D vectors, the vectors first need to be
converted into 3D coordinates. This is as easy as adding a zero z value. For
example, v = (1,0) will become v = (1,0,0) and w = (0.39, 0.92) will become
w = (0.39, 0.92,0). The value of v × w would equate to (0 ´ 0 - 0 ´ − 0.92)(1,0,0) +
(0 ´ 0.39 - 1 ´ 0)(0,1,0) + (1 ´ −0.92 - 0 ´ 0.39)(0,0,1) = 0(1,0,0) + 0(0,1,0) +
0.92(0,0,1) = (0,0,0) + (0,0,0) + (0,0, 0.92) = (0,0, 0.92). This vector only has
a z coordinate and therefore is directed along the z axis. It is therefore
coming out of the page.
An interesting thing to note about the cross product is that if the order of
the equation is reversed, the resulting vector is different. w × v would equal
(0,0,0.92) (check this out!), which is a vector the same length as the one
produced by v × w , but traveling in the exact opposite direction. This differs
from the calculation of the dot product that yields the same answer no matter
what the order of the vectors.
How does this help the pirate determine the direction in which to turn?
If he starts by facing in the direction of v and wishes to turn to face w , we
can calculate v × w . If we examine Figure 2.3 it can be seen that w would
be on the pirate's right and therefore would require a clockwise turn. We
know from the previous example that a clockwise turn between two vectors
produces a cross product result with a negative z value. The opposite is true
for an anticlockwise turn. Therefore, we can say that if z is positive it means an
anticlockwise turn and if z is negative, a clockwise turn.
The pirate now knows to turn to his right 67° clockwise and travel 7.62
kilometers in a straight line to reach the treasure.
This may all seem obvious by looking at the map. However, objects in a
game environment that have no visual point of reference, such as artificially
controlled bots or vehicles, require these very calculations in order to move
around successfully in a virtual environment.
Unity Specifics
Vectors
Every object in Unity has a number of vectors associated with it. A Game
Object's transform component has three: position, rotation, and scale.
Figure 2.4 shows the layout of a typical game environment with a car
model as a game object. Usually, in 3D, the y axis represents up, the x axis
to the side, and the z axis forward. Both the environment and all game
objects have their own transforms. The axes are displayed in the Scene as
red, green, and blue arrowed lines, as shown in Figure 2.4 . The y /up axis
is green, the x /side axis is red, and the z /forward axis is blue.
The environment has its own axes, and the orientation is set by the way
you change the scene around to look at different objects. In the Game,