圆弧球的碰撞检测

Collision detection of a ball with an arc

我正在制作一个简单的游戏,其中有一个球和一条围绕中心旋转的弧线。当用户触摸屏幕时,球会向指针方向移动并击中弧线。但是我找不到任何方法来检测该碰撞 附上一张图片以便更好地理解

游戏图片

调试快照..

我的球周围有一个圆圈... 我正在做的是

Detecting the point of intersection of ball center and circle on which arc is >revolving. But i am not able to detect whether the arc was there when ball intersected the circle?? please help...:'(

制作圆弧的代码:

 public void arc (float x, float y, float radius, float start, float degrees,int segments) {
  //  int segments = (int)(6 * (float)Math.cbrt(radius) * (degrees / 360.0f));

    if (segments <= 0) throw new IllegalArgumentException("segments must be > 0.");
    float colorBits = color.toFloatBits();
    float theta = (2 * MathUtils.PI * (degrees / 360.0f)) / segments;
    float cos = MathUtils.cos(theta);
    float sin = MathUtils.sin(theta);
    float cx = radius * MathUtils.cos(start * MathUtils.degreesToRadians);
    float cy = radius * MathUtils.sin(start * MathUtils.degreesToRadians);

    for (int i = 0; i < segments; i++) {
        renderer.color(colorBits);
        Gdx.gl20.glLineWidth(10);
        Gdx.gl.glEnable(GL20.GL_BLEND);
        renderer.vertex(x + cx, y + cy, 0);
        float temp = cx;
        cx = cos * cx - sin * cy;
        cy = sin * temp + cos * cy;
        renderer.color(colorBits);
        renderer.vertex(x + cx, y + cy, 0);
    }
}

如果您可以找到组成圆弧的线段,那么您可以使用 Intersector class 检查圆是否与每个线段相撞。我不知道你是怎么画弧线的,所以我不知道你能不能得到这些片段。

arc (float x, float y, float radius, float start, float degrees

好像x,y是圆心,start是起始角,degrees是掠角,所以结束角是end = start + degrees

如果你的交点是ix, iy,那么你可以查看这些叉积的符号:

cp_1 = (ix-x)*sin(start)-(iy-y)*Cos(start) 
cp_2 = (ix-x)*sin(end)-(iy-y)*Cos(end) 

如果cp1为负号,cp2为正(对于正弧方向度数),则(ix,iy)交点在(start,end)弧端点之间。 (适用于 Abs(degrees) < Pi=180

Delphi 示例生成从-15 度到+30 度的弧,并检查从cx,cy 到某些点的光线是否与此弧相交:

  function IsPointInArcLimits(cx, cy, ix, iy, StartAngle, Deg: Double): Boolean;
  var
    EndAngle, cp_1, cp_2: Double;
  begin
    EndAngle := DegToRad(StartAngle + Deg);
    StartAngle := degToRad(StartAngle);
    cp_1 := (ix - cx) * Sin(StartAngle) - (iy - cy) * Cos(StartAngle);
    cp_2 := (ix - cx) * Sin(EndAngle) - (iy - cy) * Cos(EndAngle);
    Result := (cp_1 <= 0) and (cp_2 >= 0);
  end;

var
  cx, cy, ix, iy, StartAngle, Degrees: Integer;
begin
  cx := 0;
  cy := 0;
  ix := 10;
  StartAngle := -15;
  Degrees := 45;
  for iy := -5 to 7 do
    if IsPointInArcLimits(cx, cy, ix, iy, StartAngle, Degrees) then
      Memo1.Lines.Add(Format('Yes  y=%d an=%f    arc %d+%d',
        [iy, RadToDeg(ArcTan(iy / ix)), StartAngle, Degrees]))
    else
      Memo1.Lines.Add(Format('No y=%d an=%f    arc %d+%d',
        [iy, RadToDeg(ArcTan(iy / ix)), StartAngle, Degrees]));

输出:

No y=-5 an=-26.57    arc -15+45
No y=-4 an=-21.80    arc -15+45
No y=-3 an=-16.70    arc -15+45
Yes  y=-2 an=-11.31    arc -15+45
Yes  y=-1 an=-5.71    arc -15+45
Yes  y=0 an=0.00    arc -15+45
Yes  y=1 an=5.71    arc -15+45
Yes  y=2 an=11.31    arc -15+45
Yes  y=3 an=16.70    arc -15+45
Yes  y=4 an=21.80    arc -15+45
Yes  y=5 an=26.57    arc -15+45
No y=6 an=30.96    arc -15+45
No y=7 an=34.99    arc -15+45

什么是圆弧?很简单:两个圆的差,限制在两个向量(或三角形)内。

图表可能会有帮助;

大红圈的半径等于圆弧的外半径。较小的蓝色圆圈的半径等于圆弧的内半径减去球的直径。三角形显示圆弧的边缘。

从这里开始,只需测试球[距中心]的欧几里得距离与圆的半径的关系,然后找到从原点到球的两条切线,看看它们中的任何一条是否落在 angular 弧度的测量。

编辑:意识到我在自己的项目中需要这样的东西,所以我决定写下来;

    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;

        }
    }

如果球只会击中弧线内侧,或者击中弧线角时有 3-4 个像素的不精确度是可以的,那么您可以替换整个 if-condition上面的代码,这样效率更高(但在角落里有点乱);

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));
}

最终结果: