Table of Contents### Friction

#### Real Friction

#### Good-Enough Friction

Entire Site

Why do objects slow down? Because they are losing energy. Objects that slow down from the interaction with another object lose energy via heat. This heat is caused by the interaction between the two materials. If this sounds familiar, it probably should—what we're talking about here is simply friction.

Put in more technical terms, a frictional force is one that opposes the direction of motion and is caused by the interaction between two materials. Kinetic energy—the energy associated with the momentum of an object—is lost as heat from the friction, and the object slows down. For instance, when you are driving your car and slam on the brakes (OK, even if you don't actually slam them), the car will use friction to its advantage and slide to a stop. If you then feel the temperature of the tires, you will notice that they are hot. (We'll discuss kinetic energy further in Chapter 6, “Collision Reactions.”)

In Flash MX there are two ways to treat friction: the right way and the good-enough way. In this section we'll discuss both.

If you slide a box to the right across the floor, then a frictional force points to the left, opposing the velocity vector. The velocity of the box will approach 0 with a constant deceleration. The equation for sliding friction is:

F = u*mass*gravity

The quantity mass*gravity is the weight of the object. So the greater the object's weight, the greater the friction that will oppose the motion of the sliding object. The u factor is known as the frictional coefficient (also sometimes called the coefficient of sliding friction).

A frictional coefficient is a numerical value between 0 and 1. In real life, this factor is found by experimentation and is different for each surface-object interaction. For instance, a wet ice cube on a rubber floor may have a very low frictional coefficient (.01), whereas a tennis shoe on the same floor may have a higher frictional coefficient (.2). In ActionScript you can simply choose a value for u depending on the type of surface you are dealing with.

Using Newton's second law, we can determine the deceleration due to friction:

F = mass*accel = u*mass*gravity

Canceling out the mass on both sides, we get:

accel = u*gravity

This `accel` variable will be used with the velocity equations we have been working with. Here are the steps you use to apply friction:

1. |
Find the acceleration due to friction using the equation
accel=u*gravity. |

2. |
Apply the
accel
value to the velocity in every frame (just as we have been doing with acceleration) until the velocity reaches 0. |

Stop applying the variable when the velocity reaches 0. If you don't, the object will actually move in the opposite direction. |

Remember that negative acceleration is deceleration.

To see this in action, open roll.fla in the Chapter04 directory. In this file we have a ball moving in the x direction. It is slowed to a stop by friction. Here is the ActionScript used: |

1 xmov = 10; 2 gravity = 2; 3 u = .2; 4 accel = u*gravity; 5 _root.onEnterFrame = function() { 6 if (Math.abs(xmov) >= Math.abs(accel)) { 7 if (xmov>0) { 8 xmov -= accel; 9 } else if (xmov<0) { 10 xmov += accel; 11 } 12 } else { 13 xmov = 0; 14 } 15 ball._x += xmov; 16 };

In line 1 we give the ball an initial velocity so it has something to slide to a stop from. We then define a gravity variable and the friction constant u. In line 4, we use the gravity and the friction constant to find the value of the acceleration due to the friction. The `if` statements in the `onEnterFrame` event are there to make sure that we either add or subtract the acceleration correctly (depending on the direction of motion). The `if` statement also controls what happens to the ball if the velocity is less than the acceleration; if that is true, then in the next frame the ball should be stopped.

While the method discussed above is the correct way to handle frictional forces, it can be a little confusing and clunky with all those `if` statements. Here is a faster and easier way to handle friction and achieve a very similar look.

1. | Choose a number between 0 and 1. Let's call this number the decay. |

2. | Multiply the decay by the velocity in every frame. |

That's it!

If the decay is 1, then the velocity never changes. If the decay is 0, then the velocity is 0 after frame 1. If the decay is between 0 and 1, then it will get closer to 0 in every frame. To see an example, open roll2.fla in the Chapter04 directory on the CD. As in the previous example, this movie shows a ball with an initial velocity sliding to a stop … well, sort of. First let's look at the ActionScript: |

1 xmov = 10; 2 decay = .95; 3 _root.onEnterFrame = function() { 4 xmov *= decay; 5 ball._x += xmov; 6 };

As you can see, this is a much simpler way to treat friction (which probably explains why it's also the most commonly used method). The result is a ball that slides from an initial velocity to almost 0 velocity. It is important to note that no matter how many times you multiply a non-zero number by a non-zero number, you will never get zero. This one of the pitfalls of using this method. Later in the book, when we get into some more specific situations with velocity, we will discuss some ways to make the ball stop when a minimum velocity is reached. |

To sum up the differences between real friction and good-enough friction, as shown in the examples in the sections above, the correct frictional implementation decreases the velocity linearly—that is, by the same amount in every frame. The good-enough method decreases the velocity by a percentage of the current velocity—a nonlinear decrease. In most circumstances, the difference between these is not going to be worth the amount of coding you'd have to put into the ActionScript to arrive at the “correct” implementation.

This chapter introduced topics that will be used frequently throughout the book, including velocity, acceleration, gravity, and friction. So don't think you've read the last on physics; you will learn more about physics in Chapter 6, “Collision Reactions.”