June 8th, 2007Skinner collision detection in AS3
We’ve been using Grant Skinners collision detection class for many of our projects.
It’s simple but genius :-).
Basically this is how it works:
1) finds the boundary intersection of both clips
2) creates a bitmap the same size as the intersection
3) draws the first clip into the bitmap in red
4) draws the second clip into the bitmap in white, using a difference blend
5) returns the boundaries of cyan in the bitmap (white difference blended on red makes cyan), or null if there is no cyan
We ported it to actionscript 3 for us and for you to enjoy!
But be aware, use at your own risk as it has not yet been tested to it’s limits.
If you have comments or improvements, let us know right here.
Download it here.
Thanks to Grant Skinner!
Technorati Tags: as3 collision detection, grant skinner




June 25th, 2007 at 7:23 am
[...] from Andre Michelle [.zip]. A slightly more robust version was written for AS2 by Grant Skinner, then ported to AS3 by Boulevart. I grabbed their version to start [...]
July 1st, 2007 at 11:01 am
Thanks Troy, very nice work on our class
July 17th, 2007 at 1:41 pm
thanks
July 29th, 2007 at 2:34 pm
this is very good collision detection in AS3, I have ever read. 10x 10x 10x
August 24th, 2007 at 6:29 pm
I ran some test on this collision detection and it seems that it can suport checkin the collision between 50 object in a enviromment with screen wraping;Put more objects and you will get low framerate thou 50 is a decent number.
To check the collision of 50 object you would actually must do more than 50 hittest per frame!
October 2nd, 2007 at 8:43 pm
the class does not work properly if you rotate one of the objects. If you in the example included in the zip add target2_mc.rotation = 40; nothing work as expected.
October 17th, 2007 at 8:28 am
yea, having a bit of trouble using it with my current setup, but thanks a LOT for porting this class to AS3.0 :O
October 23rd, 2007 at 9:32 pm
Hey! thanks! this is what i needed….well sort of. Im using it to click through .png with transparency.
December 7th, 2007 at 5:58 am
Hey there Troy. I love this class very good work. I was playing around with it a bit. It’s a little bit more advanced for me than I’d like to admit. I was just wondering what the 2 public functions at the end do (getCollisionPoint and isColliding). I removed them from the class and the detection still worked.
December 31st, 2007 at 2:06 am
Is it possible to get this working with rotated objects? I’m not sure why it doesn’t work when they’re rotated.
February 23rd, 2008 at 9:24 pm
Thanks for the code!
It was not working well at first because one of the target MovieClip was scaled. The scale was not considered by the getCollision function. I added a displayObject as a child of the MovieClip to contain the graphics.
I can rotate and scale the child and the collisions work perfectly on the MovieClip.
February 25th, 2008 at 7:27 am
FYI, I’ve got a newer version of this code that handles scaling, rotation, etc., on my blog (check the trackbacks at the beginning of the comments).
And to answer Justin’s question (which is about my code, not the code on this site): those are convenience methods for common types of queries, they are optional.
April 8th, 2008 at 10:22 pm
Hello -
I just wanted to extend my thanks for your work on excellent collision detection. I have included Troy Gilbert’s revision of your class in an open-source piece I just released. A link to the project is below:
https://netfiles.umn.edu/users/frah0005/www/dynamicSpace/dynamicSpace.html
April 16th, 2008 at 4:18 pm
Hi! I tried to download the source code , but it is not available anymore. Can you post an updated link? Thank you.
April 21st, 2008 at 2:19 pm
yep, please update the link or upload it again! thanks!
April 23rd, 2008 at 9:59 am
Hi all,
Sorry for the inconvenience, but the demo file is back up:
I have updated the link in the post and here it is once more:
http://labs.boulevart.be/media/CollsisionDetectionAS3.zip
Cheers!
July 11th, 2008 at 3:41 pm
Hi, nice class,thx!
Here is a tip to prevent it from slowing down: Dispose the bitmapdata.
For example in the getCollision function add:
alpha1.dispose();
alpha2.dispose();
right before “return res;”
makes quite some difference when your using large and many…
greetz!
July 18th, 2008 at 2:25 pm
Thanks! This will be very useful for a recent AS3 project I have. I have taken a look at the code and should be able to easily work this in. Thanks again!
October 10th, 2008 at 12:25 am
[...] Skinner collision detection in AS3 | labs.boulevart [...]
December 8th, 2008 at 4:49 pm
I port the class in haXe. I can post it on my website ?
March 27th, 2009 at 12:33 pm
[...] http://labs.boulevart.be/index.php/2007/06/08/skinner-collision-detection-in-as3/ [...]
May 27th, 2009 at 5:45 am
Great, It worked fine
June 19th, 2009 at 6:42 pm
[...] runtime errors due to null object references (and I liked that one too ). Finally, I settled on yet another class that seems to be working well so far. (I haven’t played with Corey O’Neil’s [...]
July 29th, 2009 at 9:27 pm
hey how do i use your class examples plz
September 2nd, 2009 at 11:11 pm
awesome tool, it looks that it work very well.it will be usefull for many projects for students specially
thoi trang
t
September 21st, 2009 at 8:19 pm
[...] que se ha tomado la molestia en portarlo a AS3, cosa que evidentemente no parecía tarea difícil. La gente del estudio belga Boulevart tiene ya su código portado a AS3. (nota del que escribe: Elad, si estabas pensando en ello, no hace falta que [...]
October 17th, 2009 at 1:45 am
you sure it works the same like the as2 file from gskinner? because when i put a trace in the as2 it only traces when it hits the area that is drawn within the movieclip. With this it traces already when it collides to the movieclip boundries…
November 18th, 2009 at 8:58 am
Great, It worked fine
December 9th, 2009 at 5:23 am
Hey there.
For all having the rotation issue, check out the Bitmap.draw method in AS3 - it allows you to pass a matrix object to the bitmap data - something you can use to transform the bitmap data to rotation, scale, etc.
Just to point it out - hopefully someone can implement it!
`SiN
January 30th, 2010 at 4:34 pm
Please correct me if I’m wrong, but it looks like, the less tollerance value is passed as argument, your class is less restrictive to alpha values as well. I mean, if my object has some semitransparent parts that collide with another object, they won’t be found if tollerance is 1 but will be if tollerance is 0.
January 30th, 2010 at 9:49 pm
Hi,
I’m wondering why only collisions between two objects with same parent are detected? I think it should work between any display objects - if they have same parent or not.
Regards,
Gregor
March 5th, 2010 at 11:53 pm
[...] Skinner collision detection in AS3 | labs.boulevart (tags: collision as3 flash) [...]