Game Development Reference
In-Depth Information
Figure 10.7 A top-down view of the player facing to the east (a), so east should
correspond to the topmost point on the radar (b).
To solve this problem, we can take the normalized facing vector of the player and
project it, too, onto the plane of the radar. If we then take the dot product between
this projected vector and the “forward” vector of the radar onscreen, which will
usually be 0, 1 , we can determine the angle of rotation. Then, much as in the
Rotating a 2D Character ” sample problem in Chapter 3 , Linear Algebra for
Games ,” we can convert both vectors into 3D vectors with a z-component of 0
and use the cross product to determine whether the rotation should be performed
clockwise or counterclockwise. Remember that if the cross product between two
converted 2D vectors yields a positive z-value, the rotation between the two vec-
tors is counterclockwise.
Armed with the angle of rotation and whether the rotation should be clockwise or
counterclockwise, we can use a 2D rotation matrix to rotate to face the correct
direction. A 2D rotation matrix only has one form, because the rotation is always
about the z-axis. Assuming row-major vectors, the 2D rotation matrix is
In this case, the θ we pass in will be either positive or negative, depending on
whether the rotation should be counterclockwise (positive) or clockwise (negat-
ive), which we know based on the cross product result.
Search WWH ::




Custom Search