android 2d 圆弧碰撞检测
我有一个使用 android 2d 图形绘制的旋转弧,
c.drawArc(new RectF(50, 50, 250, 250), 30, 270, true, paint);
当游戏运行时,弧将旋转,
我想知道如何检测是否有任何其他游戏对象(矩形、圆形)与其碰撞??
这是我第一次写游戏:)
我在 http://hakim.se/ 中看到了类似的东西Experiments/html5/core/01/
提前致谢
i have a rotated arc drawn using android 2d graphics
c.drawArc(new RectF(50, 50, 250, 250), 30, 270, true, paint);
the arc will rotate while the game is running ,
i wanna know how i can detect if any other game objects(rects ,circles) collide with it ??
this is the first time for me to write a game :)
i saw something like this in http://hakim.se/experiments/html5/core/01/
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
弧形碰撞比普通碰撞稍微困难一些,但是使用布尔代数,您可以轻松检查给定点是否在弧内。
看看下面的图片。
这里有 3 个对象。黑色球体,它可视化你的弧线,如果有东西与它碰撞,它可能在你的弧线内部。黑色球体顶部的红色球体,这可视化了弧线的“内部”,如果红色球体内部有东西,那么它肯定不在弧线“内部”。现在还有一个绿色三角形,可以直观地看到弧线的“截止”,绿色三角形内的任何东西也绝对不在你的弧线中。
测试黑球内是否有东西很容易。 (物体到球心的距离<=球体半径)。红色球体也是如此。绿色三角形有点棘手,你首先必须构建它。找到弧度的起点和终点弧度。并将单位向量旋转起始弧度。然后将单位向量旋转末端弧度。将这两个向量延长 2 * 黑色球体的半径。现在使用弧的中心点和两个向量的位置,并将中心位置添加为三角形的 3 个点。然后,您可以使用点三角形碰撞解算器之一:http://www.bing.com/search?q=point+triangle+collision&go=&form=QBLH&scope=web
所以记住:与 arc = 的碰撞(与黑色球体碰撞)&& !(与红色球体碰撞)&& !(与绿色三角形碰撞)。
Arc collisions are slightly harder then normal collisions, but using boolean algebra you can easily check if a given point is inside your arc.
Take a look at the following picture.
There are 3 objects here. The black sphere, this visualizes your arc, if something collides with it, it might be inside your arc. The red sphere on top of the black sphere, this visualizes the 'inside' of the arc, if something is inside the red sphere, it's definately not 'inside' the arc. Now there is also the green triangle that Visualizes the 'cut-off' of your arc, anything inside the green triangle is also definately not in your arc.
Testing if something is inside the black sphere is easy. (object's distance to center of sphere <= radius of sphere). Same for the red sphere. The green triangle is a bit tricky, you first have to construct this. Find the start and end radians of your arc. and rotate a unit vector by start radians. Then rotate a unit vector by end radians. Lengthen both these vectors by 2 * the radius of the black sphere. Now use the center point of your arc and the positions of two vectors with added the center position as the 3 points of the triangle. You can then use one of the point-triangle collision solvers: http://www.bing.com/search?q=point+triangle+collision&go=&form=QBLH&scope=web
So remember: collision with arc = (collision with black sphere) && !(collision with red sphere) && !(collision with green triangle).