PDA

View Full Version : Collision Detection Kit


Corey
05-15-2008, 07:58 PM
A short while ago I had a need for shape-based collision detection and decided to write up a class to handle it. As the class developed, I discovered other things that would be nice to have but weren't available, so I worked it out and added the functionality to my class.

Long story short, it developed into a small package of code I call the Collision Detection Kit. It's been so useful for me that I've decided to put it under an MIT license and share it with all of you. :)

It uses bitmapdata to find overlapping pixels of display objects. Not a new idea, I realize, but I wrote mine out from scratch anyways to handle things the way I needed them handled. This allowed me to figure out a few other nifty features that I include in the kit. Here are the big ones that me and my beta testers/coworkers have been loving:

- Set an alpha threshold so that any pixels on a display object below that alpha won't be checked for collisions.
- Exclude colors and color ranges from collision detection.
- The kit can approximate the angle of a collision, allowing you to keep your physics convincing when colliding with odd/organic shapes. It took awhile to figure out a good way of doing this. It's my favorite part of the kit.
- The kit takes nesting into account, so it doesn't matter where your display objects are on the display list.
- Takes transforms into account for individual instances, so things like scaling, rotating, and performing color transforms on your objects are no problem.
- Works with all display objects: MovieClips, Sprites, TextFields (very cool), FLVs, etc.

I've created pre-compiled examples to show off these features and give you a better idea of what this package is capable of doing. They aren't terribly pretty, but they make it clear just what this kit can do. I also have all classes documented, available online and for download (includes some rudimentary source code examples).

Feedback is welcomed! I don't have a discussion list for this yet, but plan on adding that or a forum later this evening (via Google Code). In the meanwhile, post any questions you might have here. I hope this is of as much help to all of you as it has been for me! :D

THE CODE (http://code.google.com/p/collisiondetectionkit)

EXAMPLES (http://www.coreyoneil.com/Flash/CDK/index.html)

DOCUMENTATION (http://www.coreyoneil.com/Flash/CDK/documentation/index.html)

asf8
05-15-2008, 08:09 PM
Hi Corey, welcome to the forums.

Looks pretty interesting, with some cool features. It will be interesting to see where you take it from here. Thanks for sharing and good work.

fitz
05-21-2008, 06:43 PM
Looks great, will give it a whirl!

Corey
05-21-2008, 08:08 PM
Thanks, fitz! :D

skinnykid
05-21-2008, 10:21 PM
Hi Corey,

This is some amazing stuff, I think there is a lot of potential here. I was wondering if you would be willing to give out some of the source code for your examples, particularly the Video example. I've played around trying to replicate it but can't seem to get my head around it. PM me if you wish. Thanks.

Corey
05-22-2008, 12:17 AM
Hey, skinnykid! I have received one or two requests like this, and I do plan on releasing the source code for the examples on my site. I hope to find the time to do that this weekend, at which point they'll be available both on my site and on Google Code. :)

Tomgupper
05-22-2008, 12:49 AM
Hi Corey,
This is fantastic.

This is pretty much exactly the sort of thing I was looking for. I saw the gskinner variations ( and the AS3 port ) however they were fairly simple in nature, love the functionality that comes with this , esp. the angle of collision.

Will have to grab this when I get home from work and have a play.

Kingslayer
05-28-2008, 01:30 AM
that looks awesome i will try that... thx soo much

ASWC
05-28-2008, 04:05 AM
the example where you draw a line is actually buggy since some of the balls go through the line. There's a lot of tutorials out there for line rider type game that can show you how to fix that.

Corey
05-28-2008, 12:58 PM
Absolutely, ASWC. I've been wanting to correct that example, it's just a matter of finding the time (and then remembering I wanted to do that). :) Hectic schedules and such. Thanks for pointing it out, though! Maybe I'll remember to take care of it now...

fitz
06-02-2008, 03:16 PM
Absolutely, ASWC. I've been wanting to correct that example, it's just a matter of finding the time (and then remembering I wanted to do that). :) Hectic schedules and such. Thanks for pointing it out, though! Maybe I'll remember to take care of it now...

shouldn't be too hard to fix, it's just as matter of anticipating the next move and checking ahead to see if it actually went "through" a line

Corey
06-02-2008, 06:57 PM
As I said, not a problem to fix it. Just need to find the time (and more importantly remember to fix it when I have that time). Lots of other things going on for me so it's very much in the back of my mind and I keep forgetting to get around to it. :P

abeall
07-05-2008, 10:27 PM
This is nice.

How are you approximating collision angle?

Corey
07-06-2008, 02:15 PM
I take a resampling of the two objects from the site of the collision, then find the average slope of all the pixels involved in the collision relevant to the smallest object's center.

RinseWashRepeat
07-06-2008, 03:18 PM
This is great. Really impressed by the video example.

abeall
07-06-2008, 11:30 PM
I take a resampling of the two objects from the site of the collision, then find the average slope of all the pixels involved in the collision relevant to the smallest object's center.

Sounds great. I am working on a game right now that involves regular hitTestPoint collisions, and to find a collision angle was very clumsy.

Have you done any benchmarking? How does it compare to hitTestPoint?

Corey
07-07-2008, 03:28 PM
I never tried benchmarking it comparatively to hitTestPoint since the CDK deals with collisions against two display objects (rather than one display object and a single point).
Generally I've found that you can have about 30 to 50 objects checking for collisions with one another without feeling a performance hit. Exceptions to this would be if you're checking for collisions against multiple large objects (taking up most of the stage), or using the excludeColor() method when working with a large number of objects. Typically, however, it works well with real-life situations such as checking for weapons fire hitting an object, or having an object move along varying surfaces.

abeall
07-08-2008, 07:33 AM
True, it's a bit comparing apples to oranges, still there are methods of using hitTestPoint for pixel based environments and I was wondering how this method holds up. I remember testing gskinner's bitmap collision test way back and felt it was really slow. Your demos seem to go good with a lot of balls, except when the balls are "resting" on something -- I guess that's the overhead of the collision angle approximation?

Anyway, thanks for your work. This is really lightweight, the way I like things, not some massive engine that does a million things I don't need, keep it that way. :)

Corey
07-08-2008, 12:21 PM
I agree entirely. hitTestPoint has its time and place, and I wasn't interested in trying to reinvent that wheel - it does its job well enough. :)

Yeah, when the balls are in a "resting" state in those demos, they're still checking against collisions. So when you have most of them lying immediately on a surface, there are significantly more checks coming back. Obviously this could be improved upon in the user's code by checking for the loss of momentum and then killing the checks for those balls, but for the purposes of the demos I figured I'd leave it on constant.

I'm glad you like the kit! And no worries, it'll definitely remain lightweight. I wrote it for personal use originally, and I kept that in mind the entire time. I'm not a big fan of bloat, either. :D

evride
07-08-2008, 12:45 PM
ya, this is quite good. love the video example like the rest of the people on here. The only thing i have a problem with is the physics part. you may want to look into the box2d class and maybe combine your code with it.

Corey
07-08-2008, 01:11 PM
ya, this is quite good. love the video example like the rest of the people on here. The only thing i have a problem with is the physics part. you may want to look into the box2d class and maybe combine your code with it.

Yea, I wrote out some very basic physics for the demos. Please realize that the physics ARE NOT PART OF THE KIT. :) The kit deals with the collision detections only - the physics are just some quick and dirty vector manipulations that I do for the demos based on the information I have returned to me from the kit.

I have some larger projects that I'm working on right now, but when I get a chance there's a few experiments I want to work on that will be making use of the kit in a more fanciful way. Should look prettier than the physics I wrote up for the original demos. ;) And yes, I'd love to see the CDK used in combination w/ a nice 2d physics package like box2d. I'll add that to my wishlist. :D

Stiegosaurus
08-01-2008, 06:40 PM
Hey man

been looking at your source for a few hours, and reading the posts. Any chance of those demos coming soon?

adrian

Corey
08-01-2008, 07:52 PM
Hey, Stiego. :) I've been thoroughly distracted by work, life, and other pet projects. I can say, however, that there are some optimizations I'm going to make to the CDK and then rerelease, at which time I'll make a point of including source code for some of the more involved examples that make use of the kit. I understand there are those that would like to see source that includes integration of some simple physics, so I'll be sure to include it. But in the end, the CDK's functionality is fully documented along with simple source examples of how to use it, so I haven't been in much of a rush. In the meantime, I would strongly recommend Keith Peter's book AS3 Animation: Making Things Move (http://www.amazon.com/Foundation-Actionscript-3-0-Animation-Making/dp/1590597915/ref=pd_bbs_1?ie=UTF8&s=books&qid=1217620283&sr=8-1). His techniques in combination with the CDK would easily recreate the compiled examples I have online. :D

Corey
06-06-2009, 08:52 PM
A new version of the CDK is now available!

I've just released version 1.5 of the Collision Detection Kit. I'm floored that it's been as popular as it is, and very happy that it could be of so much use to fellow devs out there. It's for that reason that I finally put some time aside and made some changes that were long overdue. They're mostly user requests, but also includes an overall optimization of the code. Here's what's new:

* Instead of just getting the number of overlapping pixels in a collision, the CDK now gives you those pixels in stage coordinates in an array. Real, real handy when wanting to do off-center rotation or adjust an object's position to get it back onto a surface.
* You no longer need to have your display objects on the stage for the CDK to use them! This is great, because now you can use the CDK completely off the stage. Works beautifully alongside a blitting engine.
* Parent/container display objects now have their transformations taken into account. So if you're detecting collisions with a bunch of stuff inside a parent container, and you scale that parent container on the fly, the CDK will adjust for it no problem.
* Code optimizations made throughout, leading to an average 15% improvement in speed.


Some new examples are available on my site. More importantly, however, is that I'll soon be writing up full step-by-step tutorials for these examples. This should be helpful for anyone needing a jump start on how to incorporate physics with the CDK.
I'll be sure to announce here, on my site, and on Google Code once the examples are available.

I've given this new release out to a few people in the past month or so to test drive it, and they're loving it. I strongly recommend anyone who already uses the CDK to grab the new version and start using it. Enjoy!

THE CODE (http://code.google.com/p/collisiondetectionkit)
THE EXAMPLES (http://www.coreyoneil.com/portfolio/index.php?project=5)
THE DOCUMENTATION (http://coreyoneil.com/Flash/CDK/documentation)

abeall
06-06-2009, 11:13 PM
I'm glad I'm subscribed to this thread and that you took the time to post back with a new update. Brilliant stuff, thanks again!

Corey
06-08-2009, 12:32 PM
I'm glad I'm subscribed to this thread and that you took the time to post back with a new update. Brilliant stuff, thanks again!

You bet, abeall! I wish I had taken the time to do it sooner, but glad to get some user-requested stuff out in the wild. :)

AmiAction
12-17-2009, 03:05 PM
Hi, Corey

great class!!!


What's the best way to check if the object "a" is colliding with object "b", having numerous object to check.

Thank you.

Best regards
P.S.
There are cases where vector could be more fast than array in your class?