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. Real FrictionIf 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:
Remember that negative acceleration is deceleration.
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. Good-Enough FrictionWhile 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.
That's it!
1 xmov = 10; 2 decay = .95; 3 _root.onEnterFrame = function() { 4 xmov *= decay; 5 ball._x += xmov; 6 };
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." |