ActionScript 3.0: Per-Pixel Collision Detection inna rub-a-dub Peggle Stylee

I’ve been working on writing a collision detection class, and it’s not as easy as you might think. It’s simple enough to boundary test (i.e. bounding-box overlap test), but to do per-pixel collisions you have to dump your symbol data into a BitmapData object, and you HAVE to use the top-left as the registration point for all your colliding symbol instances, which means you end up doing a lot of offsets & shiz… Ho hum.

Anyways, I knocked this Peggle / Pachinko thing together over the course of yesterday and today. You can place as many “pegs” as you want, wherever you want ’em, same with the “balls”, and in theory, I should be able to scale both pegs and balls to any size while still keeping the collisions accurate, but I’ve not tested that yet because after debugging this for a while I need a break.

But hey, look at the purty colours :)

No source-code for this one just yet as it’s not in a fit state (it has waaaay too much debug output per collision that I don’t want to strip out just yet, and enough swearing to make a salty sea-dog blush ;)). When I’ve knocked it into shape some more I’ll chuck the Peg, Ball and Collision classes here in an update.

Update: Source code and flash files now available after the jump. Some of the code is a bit yucky, so apologies in advance for my bodge-hackery =P

This project uses the FPSCounter class (which can be found here), but it’s not new so I’m not putting the source in this post. It is, of course, included in the zip file at the bottom.

Flash File Source Code:

Peg Class Source Code:

Ball Class Source Code:

CollisionDetection Class Source Code:

All source files can be found: here.

4 thoughts on “ActionScript 3.0: Per-Pixel Collision Detection inna rub-a-dub Peggle Stylee”

  1. Do you see where it says: “All source files can be found: here” at the bottom of the post?

    The here is a link to a zip file containing all the source code, which includes the .FLA file…

    You’re welcome!

Leave a Reply

Your email address will not be published.