Game Development Reference
In-Depth Information
// Grab player position
...
// The new facing of the arrow is the normal-
ized vector
// from the player's position to the waypo-
int.
facing
=
waypoint
-
playerPosition
facing
.Normalize()
// Use the dot product to get the angle
between the original
// facing (0, 0, 1) and the new one
float
angle
= acos(DotProduct(
Vector3
(0, 0,
1),
facing
))
// Use the cross product to get the axis of
rotation
Vector3
axis
= CrossProduct(
Vector3
(0, 0, 1),
facing
)
Quaternion
quat
// If the magnitude is 0, it means they are
parallel,
// which means no rotation should occur.
if
axis
.Length() < 0.01f
quat
=
Quaternion
.Identity
else
// Compute the quaternion representing
this rotation
axis
.Normalize()
quat
= CreateFromAxisAngle(
axis
,
angle
)
end
// Now set the final world transform of the
arrow
ComputeMatrix(
worldPos
,
quat
)
end
end