Pixel Perfect Collision Detection in Actionscript 3
Hey guys finally time for another update. Lately progress on my latest game has slowed down a little bit as I’ve transitioned from throw the game together development to simply fine tuning things development. With the extra time I’ve been thinking about some new ideas and finally took a stab at one of them the other night. Unlike my current project — a word puzzle game — this new idea requires real collision detection, and unlike in Fluster where I was content to use simple AABB’s for my objects this time around I’d like to have pixel perfect collision detection. So after some looking searching I came across a great library to accomplish this. Check it out, a demo, and some minor adjustments below the fold.
Click for Focus and Move the car using the arrow keys
After looking at a few different options I finally settled on Troy Gilbert’s PixelPerfectCollisionDetection library. Unfortunately, I wasn’t able to simply copy and paste the file as it contained some mistakes (probably due to Troy’s CMS). I’ve corrected those and you can find the fixed version here:
After that the library was simple enough to use. I used the library used as my narrow phase collision detection, so first I call Flash’s built in hitTestObject to determine whether the sprites’ rectangles overlap. If so I follow through with the pixel perfect match. If I were writing a larger game with more objects I would most likely use my Crash library to further improve the performance of the broad phase collision detection.
Alright, there’s a quick and dirty demo showing off how to do some pixel perfect collision detection in Actionscript 3. I hope you found this useful and look forward to seeing what you cook up with it. Feel free to check out the source code which powers this by downloading it here (to run be sure to set the Actionscript 3 class path to point to both the src and lib folders):