带弧的球的碰撞检测

时间:2016-04-16 10:51:26

标签: math libgdx game-physics

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

Game Image 游戏图片

Debug File

调试快照..

我的球周围有一个圆圈...... 我在做什么

  

检测球心与圆弧相交的圆的交点。   但是,当球与圆相交时,我无法检测到弧是否存在?   请帮助......:'(

制作弧的代码:

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

3 个答案:

答案 0 :(得分:3)

什么是弧?简单地说:两个圆的差异,约束在两个向量(或三角形)内。

图表可能会有所帮助;

Diagram

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

从这里开始,只需测试球[从中心]到圆的半径的欧氏距离,然后找到从原点到球的两条切线,看看它们中的任何一条是否通过角度测量弧线。

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

    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条件,这是更有效的方式(但在角落非常轻微地混乱);

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

答案 1 :(得分:2)

  

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

答案 2 :(得分:0)

如果您可以找到构成弧的线段,则可以使用Intersector类检查圆是否与每个线段发生碰撞。我不知道你是如何制作曲目的,所以我不知道你是否可以获得这些片段。