Collision detection of a ball with an arc

后端 未结 3 1535
[愿得一人]
[愿得一人] 2021-01-24 20:58

I am making a simple game in which i have a ball and an arc revolving around the center. When user touches the screen the ball moves in the direction of pointer and hits the arc

3条回答
  •  我在风中等你
    2021-01-24 21:52

    What is an arc? Simply: the difference of two circles, constrained within two vectors (or a triangle).

    A diagram may be helpful;

    The radius of the larger red circle equals the outer radius of the arc. The radius of the smaller blue circle equals the inner radius of the arc, minus the diameter of the ball. The triangle shows the edges of the arc.

    From here, just test the euclidean distance of the ball [from the center] against the radii of the circles, then find the two tangents from the origin to the ball and see if either of them fall pass through the angular measure of the arc.

    EDIT: Realized I need something like this in my own project, so I decided to write it up;

        double ball_radius = //Your radius of the ball
    
        //the start and end angles of the arc
        double start = //i.e -PI/4;
        double end = //i.e PI/4;
    
        double innerRadius = //inner radius of arc
        double outerRadius = innerRadius + [width of lines, 10 in your code]
    
        /* Now all the fun mathsy stuff */
    
        boolean collides = false;
    
        double dx = bx - cx; //[bx, by] = ball coords
        double dy = by - cy; //[cx, cy] = center coords
    
        //get distance and direction to ball from center
        double dist = Math.sqrt(dx * dx + dy * dy);
        double dir = Math.atan2(dy, dx);
    
        //angles for tangents to ball from center
        double tangent_angle =  Math.asin(ball_radius/ dist);
        double dir0 = dir + tangent_angle;
        double dir1 = dir - tangent_angle;
    
        //check if distance is good
        if (dist + ball_radius> innerRadius && dist - ball_radius < outerRadius)
        {
            //check edges of ball against start and end of arc
            boolean d = dir > start && dir < end;
            boolean d0 = dir0 > start && dir0 < end;
            boolean d1 = dir1 > start && dir1 < end;
    
            //if both tangents are inside the angular measure
            if (d || d0 && d1)
            {
                collides = true;
            }
            //otherwise if one tangent is inside
            //We need to test the outside corners more precisely
            else if (d0 != d1)
            {
                    double x0 = cx + outerRadius * Math.cos(start) - bx;
                    double y0 = cy + outerRadius * Math.sin(start) - by;
    
                    double x1 = cx + outerRadius * Math.cos(end) - bx;
                    double y1 = cy + outerRadius * Math.sin(end) - by;
    
                    /** And so on for the other 2 corners */
                    /** If the arc is very thick, you will need to test against
                        the actual line segments at the ends of the arc */
    
                    if (x0 * x0 + y0 * y0 < ball_radius * ball_radius
                        || x1 * x1 + y1 * y1 < ball_radius * ball_radius)
                        collides = true;
    
            }
        }
    

    If the ball is only ever going to hit the inside of the arc, or 3-4 pixels of imprecision is okay when hitting the corners of the arc, then you can replace the entire if-condition in the above code with this, which is way more efficient (but messes up on the corners very slightly);

    if (dist > innerRadius - ball_radius && dist + ball_radius < outerRadius)
    {
        //if any tangent falls within the arc
        collides = ((dir0 > start && dir0 < end) || (dir1 > start && dir1 < end));
    }
    

    End result:

提交回复
热议问题