Oval collision detection not working properly

后端 未结 4 2005
逝去的感伤
逝去的感伤 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: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
    }
    

提交回复
热议问题