RegExr v2: Build, Test, & Lear…
A Meandering Path to an Evolvi…Quick as a FlashThings Every ActionScript Deve…Making Money with Adobe AIRUsing Flash for 3D Game MMO UIUtilizing Flash for Game UI De…Why I (Still) Love FlashMy Favourite Things
Posted on April 12, 2004 by Grant Skinner
Often, in games or visual experiments, you have to track the proximity of a large number of MCs relative to one another, and carry out actions when they are within a certain distance of one another.
With grid-based proximity management, the screen is broken into a grid, and every sprite is registered to a tile on that grid each frame. You can then generate a list of sprites that are in the same tile, or an adjacent tile to any other sprite. This way, you are only carrying out calculations on the sprites that are within the nearby area – this is vastly more efficient as you increase the screen size and the number of sprites. Rather than CPU usage scaling in a largely exponential fashion (as in the iterative approach), the grid approach scales in a nearly linear manner
I’ve been playing with the idea of grid-based proximity management for awhile now (a couple of years actually), but just recently got around to building out a proper class to handle it cleanly and effectively. I’m not going to release ProximityManager just yet, because I want to play with some ideas I have for using in simulations, but I did want to show a simple example of it’s power.
In the following example, 120 sprites move randomly on-screen. Each frame, the proximity of every sprite is checked in relation to every other sprite, and those within adjacent tiles (25x25px square) are linked with lines. The more links a sprite has, the brighter and larger it appears, and the brighter it’s links are drawn. I tested this same scenario with iterative distance checking, and it slowed to a crawl (about 1 fps on my 12″ powerbook). You can see the results using the grid-based ProximityManager below (about 10fps on my 12″).
The number in the bottom right shows the number of links being drawn each frame.
I should be releasing the ProximityManager class in the next few weeks, once I have a chance to play with it further.
UPDATE: The source code is now available here.
Follow @gskinner on Twitter for more news and views on interactive media.