我正在制作一个简单的游戏,其中我有一个球和一条围绕中心旋转的弧线。当用户触摸屏幕时,球会向指针方向移动并击中弧线。但是我找不到任何方法来检测该碰撞 附上一张图片以便更好地理解
游戏图片
调试快照..
我的球周围有一个圆圈...... 我正在做的是
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);
}
}
最佳答案
什么是圆弧?很简单:两个圆的差,限制在两个向量(或三角形)内。
图表可能会有帮助;
较大的红色圆圈的半径等于圆弧的外半径。较小的蓝色圆圈的半径等于圆弧的内半径减去球的直径。三角形显示弧的边缘。
从这里开始,只需根据圆的半径测试球 [距中心] 的欧几里得距离,然后找到从原点到球的两条切线,看看它们中的任何一条是否落在角度测量处圆弧。
编辑:意识到我在自己的项目中需要这样的东西,所以我决定把它写下来;
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));
}
最终结果:
关于math - 圆弧球的碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663160/