Search code examples
javacollision-detectioncollisiongeometryangle

Collision detection : rounded object


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.

enter image description here

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.


Solution

  • 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.