【问题标题】:Collision detection of a ball with an arc圆弧球的碰撞检测
【发布时间】:2016-08-08 08:55:24
【问题描述】:

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

游戏画面

调试快照..

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

检测球心与圆弧旋转的圆的交点。 但是我无法检测球与圆相交时是否存在弧线?? 请帮忙...:'(

弧线制作代码:

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

【问题讨论】:

  • 你的弧是如何定义的?您可以比较相交角度是否在弧线范围内。

标签: math libgdx game-physics


【解决方案1】:

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

图表可能会有所帮助;

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

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

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

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

最终结果:

【讨论】:

  • 喜欢您的回复....非常感谢..它给了我另一种方法,我在某处读到了这件事,但不知道它是如何从我脑海中跳过的...非常感谢.. :)
  • 在这种情况下radius 是什么??
  • 啊,我的错,所有的radius都和ball_radius一样。我在将代码粘贴到答案中时修改了代码,看起来有些事情搞砸了。已编辑和修复。
  • 我不知道为什么,但if (d || d0 &amp;&amp; d1) { collides = true; } 每次都是真的
  • 这很奇怪......如果它仍然被一些额外的括号打破; if (d || (d0 &amp;&amp; d1)) 然后知道为什么会发生这种情况。
【解决方案2】:

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

看来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) &lt; 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

【讨论】:

  • 我一定会试一试,如果它解决了我的问题,肯定会支持你的答案。谢谢
  • Abs(degrees)
  • 圆弧小于 PI,同样——圆弧小于 180 度
  • 你确实给了我一种方法......我试过了......但它没有帮助......我正在尝试别的东西让希望它表现得像我想要的那样......会发布它很快..:)
  • 我对这些东西很陌生...非常感谢...:) 我所做的是:double ngle = Math.atan2(pointerbound.y - pointerorigin.y,pointerbound.x - pointerorigin.x); 找到指针的投影角度...之后我开始了结束角....我找到了圆弧和我的指针的交点,我比较了指针角度是否在圆弧角内...
【解决方案3】:

如果您可以找到组成圆弧的线段,那么您可以使用Intersector 类来检查圆是否与每个线段发生碰撞。我不知道你是如何制作弧线的,所以我不知道你是否可以得到分段。

【讨论】:

    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多