Collision detection : rounded object

后端 未结 4 1834
再見小時候
再見小時候 2021-01-15 11:08

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 o

相关标签:
4条回答
  • 2021-01-15 11:48

    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.

    0 讨论(0)
  • 2021-01-15 11:48

    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

    0 讨论(0)
  • 2021-01-15 11:52

    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.

    0 讨论(0)
  • 2021-01-15 12:04

    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.

    0 讨论(0)
提交回复
热议问题