Friction
[ December 11, 2003 ] by Marco Lapi, a.k.a Lapo
Real world physics simulation in Flash games: FRICTION.


Simulating real world physics is something very popular among game developers: driving games, sport games, snooker, pinball etc... All these types of games need to simulate realistic object motion to a certain extent.

Friction is one of the basic real-world forces that affects the motion of a moving object.
Let's think of a ball being kicked: the energy given by the kick gradually decreases until the ball stops moving. What has happened? The friction given by the ground and the resistence of the air cause the deceleration of the object and the production of heat.

Usually friction is described as a coefficient (number) between 0 and 1 and represents the kind of surface on which the object is moving: on an ice surface the ball is affected by a very low friction value, while the same ball with same velocity will behave differently on a muddy surface.

The simple prototype in this page simulates the motion of a red ball on a surface with customizable friction values. Click on the ball and see what happens, then adjust the parameter and try again.

Download the source code of this prototype

The actionscript code is well commented and very simple to understand:

  • the initial speed is setup every time you click on the red sphere;
  • the onEnterFrame cycle multiplies the speed value by the friction value on every frame causing the deceleration: the lower the friction value is, the faster the ball will loose its velocity.

One note about the code at line 42:

if (sp < .01) sp = 0

This is just a trick to stop the enterFrame loop to calculate very small fractional values that will not affect the motion
of the ball on screen. Try commenting that line and see it by yourself.

In the end one last note about frictional coefficient: it has been described as a value between 0 and 1 where 0 is no friction and 1
is the highest resistence possible.
However the actionscript in this sample is using a reverse formula to simplify the calculations:

speed = speed * resistence

where resistence is higher for low friction values and viceversa.

Stay tuned for more articles related with physics simulation in games!



 
 
Name: Marco Lapi, a.k.a Lapo
Location: Fossano, Italy
Age: 34
Flash experience: started out with Flash 4 back in 1999
Job: web designer/developer
Website: http://www.gotoandplay.it/
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048