home page icon Flash Bestiary / Physics / Clumping


This movie demonstrates an interesting collision avoidance system that can be used for simulating such things as floating objects or dancers on a dance floor.

I originally created a simpler variant of this movie when trying to help someone create a virtual "mosh pit". Later it occured to me to use a more sophisticated collision avoidance system similar to Craig Reynold's famous "flocking" algorithm.

In this variant of flocking, each ball is controlled by two principle factors: Repulsion and Centering. Repulsion causes each ball to avoid occupying the 'personal space' of any other ball. The repulsion from the other balls is inversely proportional to the square of their distance. The centering force pulls the balls to the center of the stage.

As the balls find equillibrium between these two forces, they tend to find an efficient packing method.

You may drag any of the balls around the dance floor, and the other balls will attempt to move out of the way. In the center of the dance floor, this may be more difficult.

This is an expensive algorithm and slows down geometrically as you add more objects.

UPDATE: The source code below now contains an Actionscript 3 version, in addition to the original Flash MX code. The algorithm runs much faster in AS3, you'll find.


sourcecode iconDownload the flash project
book iconJim's Favorite Actionscript Books
wiki iconAsk Jim about Actionscript
Next: Wheel of Trinkets

Having trouble opening the project? You may need to Upgrade to Flash CS3

Copyright © 2003,2008 by Jim Bumgardner. All Rights Reserved.        Leave Jim some feedback