Game Development Reference
In-Depth Information
Friction
Friction is the exact opposite of acceleration: it causes the object to gradually slow down. Let's see
how it works with the cat:
1. Select the ?]p symbol in the Library and click the Properties button. Change the Class field to
Player_Friction and click the OK button.
2. Test the project.
3. Move the cat around the screen with the arrow keys. You can move it in delightfully smooth
swoops and arcs, as if it were floating.
The Lh]uan[Bne_pekj class is almost identical to Lh]uan[=__ahan]pekj except for a few small addi-
tions. Double-click it in the Project panel to open it and take a look.
First, is the new variable, [bne_pekj, which is initialized to ,*52:
[rt9,7
[ru9,7
[]__ahan]pekjT9,7
[]__ahan]pekjU9,7
[olaa`Heiep917
[bne_pekj9,*527
A value of - amounts to “no friction,” so anything less than 1 gradually slows the object down. Values
from ,*50 to ,*54 apply friction very gradually, for very fluid movement. Values such as ,*3 or ,*2
slow the object very quickly.
All you need to do now is add the [bne_pekj value to the [rt and [ru properties:
[rt&9[bne_pekj7
[ru&9[bne_pekj7
It multiplies the velocities by a number less than -, which gradually reduces them. It's very simple and
very effective. There's one technical detail you have to fix, however.
Here's the scenario. Imagine that the object's velocity is 1. Its friction is ,*2. The object needs a veloc-
ity of zero to stop completely. You apply some friction every frame, multiplying the velocity by ,*2,
hoping to finally reach zero. Here's what the first five frames might look like.
5 * 0.6 = 3
3 * 0.6 = 1.8
1.8 * 0.6 = 1.08
1.08 * 0.6 = 0.648
0.648 * 0.6 = 0.3888
But you're not at zero yet. How long do you think it will take before you get there? Well, you can keep
going all day—you never will!

Search Nedrilad ::

Custom Search