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