Oval collision detection not working properly

后端 未结 4 1943
逝去的感伤
逝去的感伤 2021-01-24 01:54

So I\'m trying to implement a test where a oval can connect with a circle, but it\'s not working.

edist = (float) Math.sqrt(
    Math.pow((px + ((pwidth/2) )) -          


        
相关标签:
4条回答
  • 2021-01-24 02:19

    If you can find your foci you can check for collision with the pseudo code below. WARNING this only works for two ellipse collisions (ellipse and circle collisions work also).

    r = length of semi major axis
    a_x = x coordinate of foci 1 of the first ellipse
    a_y = y coordinate of foci 1 of the first ellipse
    b_x = x coordinate of foci 2 of the first ellipse
    b_y = y coordinate of foci 2 of the first ellipse
    
    c_x = x coordinate of foci 1 of the second ellipse
    c_y = y coordinate of foci 1 of the second ellipse
    d_x = x coordinate of foci 2 of the second ellipse
    d_y = y coordinate of foci 2 of the second ellipse
    
    p_x = (a_x+b_x+c_x+d_x)/4 // i.e. the average of the foci x values
    p_y = (a_y+b_y+c_y+d_y)/4 // i.e. the average of the foci y values
    
    if r >= ( sqrt( (p_x + a_x)^2+(p_y + a_y)^2 ) + sqrt( (p_x + a_x)^2+(p_y + a_y)^2 ) )
    then collision
    

    If you really want the derivation of this let me know and I'll provide it. But it uses the idea that the sum of the distances between the foci of an ellipse and any point on the edge of an ellipse are a set distance apart (the semi major axis). And solves for a point that is on the edge of both ellipsoids and if one exist then their is a collision.

    0 讨论(0)
  • 2021-01-24 02:25

    Is using ovals an absolute requirement? You can approximate collisions between fancier shapes by representing them with multiple circles. That way you can use very a simple collision detection between circles and still achieve a high level of accuracy for the viewer.

    collision(c1, c2) {
      dx = c1.x - c2.x;
      dy = c1.y - c2.y;
      dist = c1.radius + c2.radius;
    
      return (dx * dx + dy * dy <= dist * dist)
    }
    


    (source: strd6.com)

    0 讨论(0)
  • 2021-01-24 02:32

    Finding the intersection is harder than you think. Your col() method is a bit off, but that approach will at best be able to tell you if a single point is within the circle. It won't be able to really detect intersections.

    I Googled up some code for computing the actual intersections. I found one in JavaScript that's really interesting and really complicated. Take a look at the source.

    If you wanted something a bit simpler (but less accurate), you could check a few points around the ellipse to see if they're within the circle.

    private boolean isInCircle(float x, float y) {
        float r = bsize / 2;
        float center_x = bx + r;
        float center_y = by + r;
        float dist = (float) Math.sqrt(Math.pow(x - center_x, 2) + Math.pow(y - center_y, 2));
    
        return dist < r;
    }
    
    public boolean col() {
        return 
            isInCircle(px + pwidth / 2, py              ) || // top
            isInCircle(px + pwidth    , py + pheight / 2) || // right
            isInCircle(px + pwidth / 2, py + pheight    ) || // bottom
            isInCircle(px             , py + pheight / 2);   // left
    }
    
    0 讨论(0)
  • 2021-01-24 02:36

    If you plan on implementing more shapes and/or need the minimum distance between your shapes, you could start using GJK : you would only need to implement the support functions for each new shape. If computation time is also critical, GJK is definitely something you should look at, but it would surely require some more programming on your side.

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