Game Development Reference
In-Depth Information
Once you have that value, you can use it with the I]pd*_ko and I]pd*oej functions, which are two
specialized trigonometry functions that return the ratio of two sides of the triangle formed by the
measurement of the angle in the preceding step. If you multiply these numbers by an acceleration
value, you can apply the resulting number to the []__ahan]pekjT and []__ahan]pekjU variables:
[]__ahan]pekjT9I]pd*_ko\$[]jcha%&[]__ahan]pekj7
[]__ahan]pekjU9I]pd*oej\$[]jcha%&[]__ahan]pekj7
These two variables are the basis of what you need to start moving the object in the right direction.
You just need to apply them to the [rt and [ru variables, which in turn are applied to the t and u
positions:
++=llhu]__ahan]pekj
[rt'9[]__ahan]pekjT7
[ru'9[]__ahan]pekjU7
++=llhubne_pekj
[rt&9[bne_pekj7
[ru&9[bne_pekj7
++Ikrapdalh]uan
t'9[rt7
u'9[ru7
The code does one more thing. You want the object to move only if the mouse is farther than
75 pixels from Button Fairy's center point. To do this, you first need to find out the distance of her center
point from the mouse. You can calculate it using the same formula you used earlier in the chapter:
r]n`t6Jqi^an9op]ca*ikqoaT)pdeo*t7
r]n`u6Jqi^an9op]ca*ikqoaU)pdeo*u7
r]n`eop]j_a6Jqi^an9I]pd*omnp\$`t&`t'`u&`u%7
Once you've got the distance, the rest is easy. You just need to find out whether the distance is greater
than or equal to 75. If it is, you can accelerate the object. If it's less, you can cut the acceleration to
zero and put on the brakes with a bit of friction (remember that a friction value of - means that no
friction is being applied):
eb\$`eop]j_a:931%
w
[]__ahan]pekj9),*17
[bne_pekj9-7
y
ahoa
w
[]__ahan]pekj9,7
[bne_pekj9,*527
y
And that's all there is to making Button Fairy move around the stage. To keep the code simple in this
example, I haven't implemented any speed limit checks, but you could easily do that based on the
examples from the previous chapter.