Quote:
You would first need to find the shape defined by the intersection of the two shapes...
That's it! But rather than using the physics forms, I'm using their display object counterparts -- not sure if that makes any difference really. So I whipped up the following function to find the center of the intersection. If there is no intersection (which turns out to happen occasionally) then it constructs a rectangle in the space between and uses the center of that.
Code:
public function findOverlapCenter(objectA:DisplayObject, objectB:DisplayObject):Point {
var rectA:Rectangle = objectA.getBounds(world);
var rectB:Rectangle = objectB.getBounds(world);
var intersection:Rectangle = rectA.intersection(rectB);
if (intersection.isEmpty()) {
// NO INTERSECTION - Invent a rectangle between the two
if (rectA.bottom < rectB.top) {
// A ABOVE B
intersection.right = (rectA.left > rectB.right) ? rectA.left : Math.min(rectA.right, rectB.right);
intersection.left = (rectA.right < rectB.left) ? rectA.right : Math.max(rectA.left, rectB.left);
intersection.top = rectA.bottom;
intersection.bottom = rectB.top;
} else if (rectA.top > rectB.bottom) {
// A BELOW B
intersection.right = (rectA.left > rectB.right) ? rectA.left : Math.min(rectA.right, rectB.right);
intersection.left = (rectA.right < rectB.left) ? rectA.right : Math.max(rectA.left, rectB.left);
intersection.top = rectB.bottom;
intersection.bottom = rectA.top;
} else {
if (rectA.right < rectB.left) {
// A LEFT OF B
intersection.top = (rectA.bottom > rectB.top) ? rectA.bottom : Math.min(rectA.top, rectB.top);
intersection.bottom = (rectA.top < rectB.bottom) ? rectA.top : Math.max(rectA.bottom, rectB.bottom);
intersection.left = rectA.right;
intersection.right = rectB.left;
} else {
// A RIGHT OF B
intersection.top = (rectA.bottom > rectB.top) ? rectA.bottom : Math.min(rectA.top, rectB.top);
intersection.bottom = (rectA.top < rectB.bottom) ? rectA.top : Math.max(rectA.bottom, rectB.bottom);
intersection.left = rectB.right;
intersection.right = rectA.left;
}
}
}
var center:Point = intersection.topLeft;
center.x += intersection.width * 0.5;
center.y += intersection.height * 0.5;
return center;
}
Thanks for the idea!
