Game Development Reference
In-Depth Information
2. The values of these new variables are used to modify the velocity:
[rt'9ikraT7
[ru'9ikraU7
3. Use these new [rt and [ru values to help you find the total distance required to move:
r]nikra@eop]j_a6Jqi^an9I]pd*omnp\$[rt&[rt'[ru&[ru%7
4. You can use this new distance value along with the OLAA@ constant to find the correct
velocity:
[rt9OLAA@&[rt+ikra@eop]j_a7
[ru9OLAA@&[ru+ikra@eop]j_a7
5. Finally, you can rotate the object toward the target. This is the same formula you've been using
for rotation throughout the topic. The addition of + 90 is there to offset the rotation of the
robot object by 90 degrees. Without that, the leading edge of the robot would be its right side
because of the way the object was drawn in the symbol (with its “front” being the cone on the
robot's head). Any objects you use with this code might be oriented differently, so you'll prob-
ably want to adjust 5, to another number that you can figure out by trial and error when you
see the direction toward which your object rotates:
nkp]pekj9-4,&I]pd*]p]j.\$[ru([rt%+I]pd*LE'5,7
If the object is not within the robot's range, these directives kick in, which gradually slow it down by
using friction:
++=llhubne_pekj
[rt&9BNE?PEKJ7
[ru&9BNE?PEKJ7
++Ikra
t'9[rt7
u'9[ru7
A bit of simple logic, a few careful adjustments to the easing formula, and you have a very effective
following behavior.
Running away from the player
It's very easy to create the exact opposite behavior: make the robot run away from the player. To see
this at work, bind the Nk^kp symbol to the Nk^kp[Nqj class. Test the project and you'll see the robot
flee from the player, as illustrated in Figure 10-19.
When I say that this is an opposite behavior, I mean that in the most literal sense imaginable. The
Nk^kp[Nqj class is exactly the same as Nk^kp[Bkhhks, except that three plus signs have been made
negative.
The rotation is negative so that the robot points in the opposite direction:
nkp]pekj9-4,&I]pd*]p]j.\$[ru([rt%+I]pd*LE)5,7