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):

Comments

4 Responses to “Pixel Perfect Collision Detection in Actionscript 3”
  1. Max says:

    Clever, very clever. There’s not much point to detecting if two things collide with no resolution to the collision though.

    Ah, but here’s the bad news. This library does both. http://code.google.com/p/collisiondetectionkit/ From what little I know about it you may have a faster technique though.

  2. aschearer says:

    Yep, like you say it’s often not enough to simply get a boolean for a collision. I’ll definitely have to check out that library for my next project, thanks! But in cases where a boolean result is sufficient I think this is a pretty good method.

  3. Anthony says:

    Hey, I am using this but there’s one thing I just cant figure out how to do!! how do I do it? – I’m frustrated now.

    I want to target a movie clip (separate from the objects colliding)in my library and move it wen the two objects collide. For instance, if block 1 and 2 collides, then move block 3.

    What I had done is in the part of the pixelperfectcollission.as where it detects if there is a collision, I changed the “return true” and put return bushmc += 20; and the error gotten is that access of undefined property. (I gave the bush an instance name of bushmc)

    I tried using import.flash.display.MovieClip at the top of the class file. Didn’t work.

    I must admit though, I am not that advanced in actionscript as yet so.. Help plzz ! :p

  4. Max says:

    I believe based on the code for pixel perfect collision, there is no need to do a hitTestObject, as it’s essentially comparing bounding boxes, and if there is not intersection of bounding boxes then it doesn’t do pixel perfect test.

    So, unless I’m reading this code wrong, in the case that there is a bounding box collision, there is a double check for bounding box collision — once in your code, once in the pixelperfect code.

    if you look in the getCollisionRect function , at the first if statement , you’ll see what i mean.

Leave A Comment