Game Development Reference
In-Depth Information
screen. Because it's a screen space position, it will be an (x, y) coordinate that we
need to convert into a 3D world space coordinate for the purposes of rendering the
3D arrow in the correct spot. In order to do this, we will need to use an unpro-
jection, much like in the “Picking” section of Chapter 8 , Cameras . ” Finally, the
waypoint position is the target at which the arrow will point.
In order to update the waypoint arrow, in every frame we must construct a vector
from the player's position to the target; once normalized, this will give us the dir-
ection the arrow should be facing. Then it's a matter of using the dot product
and cross product to determine the angle and axis of rotation between the original
(straight into the screen) facing vector and the new facing vector. The rotation can
then be performed with a quaternion, using a lerp if we want the arrow to more
smoothly rotate to the target. Figure 10.4 shows the basic calculations that must be
done to update the waypoint arrow.
Figure 10.4 Waypoint arrow calculations, assuming old and new are normalized.
An implementation of this type of waypoint arrow is provided in Listing 10.1 .
There are a couple of things to keep in mind with this implementation. First of
all, it assumes that the camera is updated prior to the waypoint arrow being up-
dated. That's because it's the only way to really guarantee that the waypoint arrow
is always at the same spot on the screen. Otherwise, when the camera changes, the
waypoint arrow will always be one frame behind.
Search WWH ::




Custom Search