Collision detection : rounded object

2019-02-19 11:49发布

问题:

I'm developing a Java game (but the dev. language doesn't really matter) including rounded objects like balls or pucks, and now working on collisions. I use a timer, so on every frame I check if a collision happens.

Here is a graph that represents the top-right piece of an object.

The center of the object is represented by the point [0,0], its radius is 10px and the units are pixels.

Now if my object (for example, obj_1) is square/diamond-shaped (blue line), to find if another one (obj_2) collides it I just have to get their coordinates and by checking Math.abs(obj_1.x - obj_2.x) + Math.abs(obj_1.y - obj_2.y) <= radius I will know if there is a collision.

But the problem is more tricky with a circle shape (red line), because it takes more space and this occupied space is not bounded by a straight line. Of course I will have to round some values (for example in the previous graph, if I want to check a collision at x = 2 I will have to round the y value which looks like 9.5 to 10). But I simply have no idea of how to get this formula. Any help will be very appreciated.

回答1:

As you mentioned that the implementation language does not matter, I will give you a generic solution for detecting collision of round objects.

Also, from what I gather, all the objects in the scene are circles. The solution below does not apply for detecting collision between a circle and some other shape.

Suppose you have two circles c1 and c2. Suppose the corresponding radii are c1.r and c2.r, and the centers are (c1.x,c1.y) and (c2.x, c2.y), then the following function will tell whether c1 and c2 are in collision

boolean areColliding(Circle c1, Circle c2){

   center_distance = sqrt((x1-x2)^2 +(y1-y2)^2);  //this is the distance between the centers of the two circles.

  if((c1.r+c2.r) < center_distance)
           return false;
  else
          return true;

}

This pseudo-code function will return true if the circles are colliding, false otherwise.

Basically, what the function does is check whether the distance between the centers of the circles is greater than the sum of their respective radius.



回答2:

In Java, you have a java.awt.Polygon.

The Polygon class has multiple contains methods and intersect methods.

Defining a circle as a Polygon is a pain, depending on how accurate you want the circle to be. But once you've defined all of your objects as Polygons, you can use the defined methods to detect collisions.



回答3:

you can set your rounded shape up in a bounding box. This will yield less accurate collisions but has huge performance benefits off other methods



回答4:

An addition to @Ankit's solution:

boolean areColliding(Circle c1, Circle c2){

    center_distance = (x1-x2)^2 +(y1-y2)^2;  //this is the distance between the centers of the two circles.

    if((c1.r+c2.r)^2 < center_distance)
        return false;
    else
        return true;
}

This just compares the squared distances. Result is the same, but no square root and huge performance benefit.