# Box2D Forums

 It is currently Tue May 21, 2013 2:46 pm

 All times are UTC - 8 hours [ DST ]

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Center of Contact AreaPosted: Fri Feb 11, 2011 1:09 pm

Joined: Mon Apr 07, 2008 3:06 am
Posts: 99
I thought I knew how to do this, but it seems I'm getting unexpected results however I try to do it-- Given two objects, one of which is a sensor, how can I find the center of the overlapping area, as illustrated below?

Top

 Post subject: Re: Center of Contact AreaPosted: Fri Feb 11, 2011 2:33 pm

Joined: Fri Dec 14, 2007 8:07 pm
Posts: 913
You would first need to find the shape defined by the intersection of the two shapes, which is not easy:

http://stackoverflow.com/questions/2272 ... tersection

Then you would have to find the center of that shape.

Or maybe this might work: imagine a line between the two shapes center of mass. Project the contact point onto that line. Might work?

Top

 Post subject: Re: Center of Contact AreaPosted: Fri Feb 11, 2011 4:11 pm

Joined: Mon Apr 07, 2008 3:06 am
Posts: 99
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!

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC - 8 hours [ DST ]

#### Who is online

Users browsing this forum: Google [Bot] and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Search for: