8

I've just implemented collision detection using SAT and this article as reference to my implementation. The detection is working as expected but I need to know where both rectangles are colliding.

I need the center of the intersection More examples

I need to find the center of the intersection, the black point on the image above (but I don't have the intersection area neither). I've found some articles about this but they all involve avoiding the overlap or some kind of velocity, I don't need this.

The information I've about the rectangles are the four points that represents them, the upper right, upper left, lower right and lower left coordinates. I'm trying to find an algorithm that can give me the intersection of these points.

I just need to put a image on top of it. Like two cars crashed so I put an image on top of the collision center. Any ideas?

Felipe Cypriano
  • 2,727
  • 22
  • 32
  • 1
    One possible way to get the intersection center is to construct the Minkowski Difference and look at point (0, 0). That works for arbitrary polygons but might be too complex for your problem. Take a look at this [page + interactive example](http://physics2d.com/content/gjk-algorithm). (The part on Minkowski Difference in specifically.) Sorry I can't be of specific help, I'm short on time right now. – TaZ Oct 02 '12 at 21:29
  • If I understood correctly. The Minkowski Difference (or the GJK Algorithm) will tell me if the polygons are colliding. Where is it telling me the point of collision? – Felipe Cypriano Oct 03 '12 at 15:27

4 Answers4

3

You need to do the intersection of the boundaries of the boxes using the line to line intersection equation/algorithm.

http://en.wikipedia.org/wiki/Line-line_intersection

Once you have the points that cross you might be ok with the average of those points or the center given a particular direction possibly. The middle is a little vague in the question.

Edit: also in addition to this you need to work out if any of the corners of either of the two rectangles are inside the other (this should be easy enough to work out, even from the intersections). This should be added in with the intersections when calculating the "average" center point.

ceorron
  • 1,230
  • 1
  • 17
  • 28
3

There is another way of doing this: finding the center of mass of the collision area by sampling points.

Create the following function:

bool IsPointInsideRectangle(Rectangle r, Point p);

Define a search rectangle as:

TopLeft = (MIN(x), MAX(y))
TopRight = (MAX(x), MAX(y))
LowerLeft = (MIN(x), MIN(y))
LowerRight = (MAX(x), MIN(y))

Where x and y are the coordinates of both rectangles.

You will now define a step for dividing the search area like a mesh. I suggest you use AVG(W,H)/2 where W and H are the width and height of the search area.

Then, you iterate on the mesh points finding for each one if it is inside the collition area:

IsPointInsideRectangle(rectangle1, point) AND IsPointInsideRectangle(rectangle2, point) 

Define:

Xi : the ith partition of the mesh in X axis.
CXi: the count of mesh points that are inside the collision area for Xi.

Then:

enter image description here

And you can do the same thing with Y off course. Here is an ilustrative example of this approach:

enter image description here

daniloquio
  • 3,822
  • 2
  • 36
  • 56
2

This one's tricky because irregular polygons have no defined center. Since your polygons are (in the case of rectangles) guaranteed to be convex, you can probably find the corners of the polygon that comprises the collision (which can include corners of the original shapes or intersections of the edges) and average them to get ... something. It will probably be vaguely close to where you would expect the "center" to be, and for regular polygons it would probably match exactly, but whether it would mean anything mathematically is a bit of a different story.

I've been fiddling mathematically and come up with the following, which solves the smoothness problem when points appear and disappear (as can happen when the movement of a hitbox causes a rectangle to become a triangle or vice versa). Without this bit of extra, adding and removing corners will cause the centroid to jump.

Here, take this fooplot.

The plot illustrates 2 rectangles, R and B (for Red and Blue). The intersection sweeps out an area G (for Green). The Unweighted and Weighted Centers (both Purple) are calculated via the following methods:

(0.225, -0.45):   Average of corners of G
(0.2077, -0.473): Average of weighted corners of G

A weighted corner of a polygon is defined as the coordinates of the corner, weighted by the sin of the angle of the corner.

This polygon has two 90 degree angles, one 59.03 degree angle, and one 120.96 degree angle. (Both of the non-right angles have the same sine, sin(Ɵ) = 0.8574929...

The coordinates of the weighted center are thus:

( (sin(Ɵ) * (0.3 + 0.6) + 1 - 1)   / (2 + 2 * sin(Ɵ)),  // x
  (sin(Ɵ) * (1.3 - 1.6) + 0 - 1.5) / (2 + 2 * sin(Ɵ)) ) // y
= (0.2077, -0.473)

With the provided example, the difference isn't very noticeable, but if the 4gon were much closer to a 3gon, there would be a significant deviation.

Wug
  • 12,956
  • 4
  • 34
  • 54
  • The way I read it, he's asking for the entire region, not just the center point. The first illustration is misleading if this is the case. – Zev Eisenberg Oct 02 '12 at 21:30
  • 1
    >I need to find the center of the intersection, the black point on the image above. – Wug Oct 02 '12 at 21:34
  • Ah, in that case I misunderstood the question. – Zev Eisenberg Oct 02 '12 at 21:39
  • Actually I need the center point (or something close to it) but I don't have the intersection area. Currently I have only the four points of each rectangle and I know if they colliding or not. – Felipe Cypriano Oct 03 '12 at 14:40
  • Do some maths to figure out the points of intersection, then some logics to figure out which of the points you've got are the boundries of the intersecting area. – Wug Oct 03 '12 at 14:43
  • That is what I'm trying to do. I don't said that you answer was wrong, I was just clarifying in which stage I'm. I think each answer here is helping me find a way to get there – Felipe Cypriano Oct 03 '12 at 14:52
0

If you don't need to know the actual coordinates of the region, you could make two CALayers whose frames are the rectangles, and use one to mask the other. Then, if you set an image in the one being masked, it will only show up in the area where they overlap.

Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
  • Is there any way to determine the center of the overlapping area, in order to tell the CALayer where to place the image? – Phssthpok Oct 02 '12 at 21:33