PDA

View Full Version : Advanced rectangle collision


prisma
08-29-2004, 03:08 PM
While working on a game I tried to create a rectangles-rectangle collision detection, that is superior to Flash's hitTest.

Problem: hitTest uses the bounding boxes of two objects to check collision. If both both rectangles have a 45° ratation it happens that hitTest detects a collsision although there might still quite some space inbetween the rectangles.

Soltution: My collision detection procedure checks the bounding boxes too, but in the inert system of one of the rectangles. If this is done once for each of the two rectangles the result is a complete true form collsision detection between two rectangles.

The best thing: It doesn't produce high CPU load at all.
While executing hitTest 10.000 time takes ~322ms on my machine, executing my advanced collision function 10.000 times takes ~670ms. Because it has to be executed twice to get a complete true form detection it produces aproximatly ~4 times the CPU load of hitTest, which isn't really much if you think about it.
However, if performence is crucial you can still do a standard hitTest before my advanced collision detection.

You'll most likely understand the reason for it's good performence once you see the code. It's generally extremely simple:

function advCollision(mc1:MovieClip, mc2:MovieClip):Boolean {

var mc1Bounds:Object = mc1.getBounds(mc1);
var mc2Bounds:Object = mc2.getBounds(mc1);

return ((mc1Bounds.xMin <= mc2Bounds.xMax)
&& (mc1Bounds.xMax >= mc2Bounds.xMin)
&& (mc1Bounds.yMin <= mc2Bounds.yMax)
&& (mc1Bounds.yMax >= mc2Bounds.yMin));
}

That's all.

Here's an example file, that demonstrates the 2 different types of collision tests, and can give you a hint of how to use it.
http://www.dalord.yi.org/prisma/flash/collision/rectangleCollision.swf
(If above swf doesn't work, my server is down. You can download the file and it's fla below)

I hope anyone besides me can make good use of it.
(Note: All this is AS2, but can be easily converted to AS1)

puppy
08-30-2004, 02:20 PM
prisma, I'm not sure I do understand this correct, but wouldn't it be easier to use vertex hit test? e.g., you take closest vertix of 2nd rectangle and apply hitTest "in the inert system of [1st] of the rectangles"? Sure, there can be a situation like


+----+
| |
+---+---+---+
| | | |
+---+---+---+
| |
+----+


but, how often, really ?

EDIT: one can think of arbitrary shape collisions... like, you define several "check-points", then take closest, and hitTest it. "check-points" might be MCs nested-into that shape, so Flash will take care of all math... just a thought, never tried that, though.

FlashMove
09-01-2004, 02:13 AM
Thanks prisma! That is an extremely useful code, how about applying this to a multiple collision detector?

prisma
09-01-2004, 07:52 AM
Puppy:
I suppose you're talking about a node based true-form collsision detection. (I'm sorry, but my native language isn't english, so maths-talk is quite hard for me ;))
I've actually done something like this quite a while ago; I posted a thread (http://www.flashmove.com/forum/showthread.php?s=&threadid=8503
) that didn't get any replies though.



FlashMove:
Not sure what you mean by 'mutiple collsision detector'. A function that does a hitTest and then, if neccesary, check collision with my method whould be simple:

function rectangleCollsision(mc1:MovieClip, mc2:MovieClip):Boolean {
return (mc1.hitTest(mc2) && advCollision(mc1,mc2) && advCollsision(mc2,mc1));
}

Note: If you use '&&' the latter 2 function wont be executed if hitTest returns false. This is not the case with 'and'!

Gargoyle
09-01-2004, 04:32 PM
well done

puppy
09-08-2004, 11:24 AM
...in searching results (hitTest, BSP, vertex based true form collision detection, etc.)

Originally posted by prisma
I've actually done something like this quite a while ago; I posted a thread (http://www.flashmove.com/forum/showthread.php?s=&threadid=8503
) that didn't get any replies though.

Well, it did now ;) It has over 500 views, and marked as hot in thread lists. It's worth of looking at, if not for great Prisma's sources, then because I replied there (just kidding - but you know... )

:cheers:

btmash
07-04-2007, 04:38 PM
I've just started learning flash and I was trying to understand the file because it initially wasn't working for me. I was trying to understand how your flash file was doing the rotations to grab the bounds in the inert system of one of the rectangles. But once I understood how getBounds works (and noticed the parameters), I understood your code. Very elegant :D

puppy
07-04-2007, 05:45 PM
dude, prisma last post was in 01-27-2005, 04:36 PM (I have almost forgot there ever was such a guy).

Scottae
08-22-2008, 03:44 PM
Me too. There used to be so many familiar faces around here. Now it seems more like a ghost town.