圆圈圈的碰撞 [英] circle-circle collision

查看:124
本文介绍了圆圈圈的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建立一个2-D球游戏,两个球(圆)发生碰撞。现在我与确定所述碰撞点(实际上,确定它们是否碰撞在x轴/ Y轴)的问题。我有一个想法,当y之间的差的2球坐标大于x坐标差然后他们在y轴上碰撞,否则,它们在X轴碰撞。是我的想法是否正确?我在游戏实现了这个事情。通常情况下它工作得很好,但有时,它会失败。谁能告诉我,我的想法是否正确?如果没有,那么为什么,是没有更好的办法?

I am going to develop a 2-d ball game where two balls (circles) collide. Now I have the problem with determining the colliding point (in fact, determining whether they are colliding in x-axis/y-axis). I have an idea that when the difference between the y coordinate of 2 balls is greater than the x coordinate difference then they collide in their y axis, otherwise, they collide in their x axis. Is my idea correct? I implemented this thing in my games. Normally it works well, but sometimes, it fails. Can anyone tell me whether my idea is right? If not, then why, and is any better way?

在X轴的碰撞,我的意思是圆的第1,第4,第5,第8或八分之一,Y轴表示圆的第2,第3,第6,第7或八分之一。

By collision in the x axis, I mean the circle's 1st, 4th, 5th, or 8th octant, y axis means the circle's 2nd, 3rd, 6th, or 7th octant.

在此先感谢!

推荐答案

圈的碰撞很容易。想象一下,有两个圆:

Collision between circles is easy. Imagine there are two circles:

  • 在C1与中心(X1,Y1)和半径R1;
  • C2与中心(x2,y2),半径为R2。

想象一下,有这两个中心点之间运行的线路。从中心指向任一圆的边缘的距离是根据定义,等于其相应的半径。所以:

Imagine there is a line running between those two center points. The distance from the center points to the edge of either circle is, by definition, equal to their respective radii. So:

  • 如果圆的边缘碰触,中心之间的距离为R1 + R2;
  • 在任何更大的距离和圈不要接触或碰撞;和
  • 在任何少,然后就发生碰撞。

所以,你可以进行碰撞检测,如果:

So you can detect collision if:

(x2-x1)^2 + (y1-y2)^2 <= (r1+r2)^2

意的中心点之间的距离小于半径的总和。

meaning the distance between the center points is less than the sum of the radii.

同样的原理可以应用于检测在三维球之间的碰撞。

The same principle can be applied to detecting collisions between spheres in three dimensions.

编辑:如果您要计算碰撞点,一些基本的三角可以做到这一点。你有一个三角形:

if you want to calculate the point of collision, some basic trigonometry can do that. You have a triangle:

        (x1,y1)
        |\
        | \
        |  \ sqrt((x2-x1)^2 + (y2-y1)^2) = r1+r2
|y2-y1| |   \
        |    \
        |   X \
(x1,y2) +------+ (x2,y2)
         |x2-x1|

这位前pressions | X2-X1 | | Y2-Y1 | 的绝对值。因此对于角X

The expressions |x2-x1| and |y2-y1| are absolute values. So for the angle X:

        |y2 - y1|
sin X =  -------
         r1 + r2

        |x2 - x1|
cos X =  -------
         r1 + r2

        |y2 - y1|
tan X =  -------
        |x2 - x1|

一旦你的角度,你可以把它们应用到一个新的三角形计算交点:

Once you have the angle you can calculate the point of intersection by applying them to a new triangle:

  +
  |\
  | \
b |  \ r2
  |   \
  |  X \
  +-----+
     a

其中:

        a
cos X = --
        r2

所以

a = r2 cos X

从previous公式:

From the previous formulae:

       |x2 - x1|
a = r2 -------
        r1 + r2

一旦你有A和B就可以计算出在(x2,y2)抵消条款(A,B)在适当的碰撞点。你甚至都不需要计算所有正弦,余弦或反正弦余弦或本。或者有什么平方根为此事。所以速度非常快。

Once you have a and b you can calculate the collision point in terms of (x2,y2) offset by (a,b) as appropriate. You don't even need to calculate any sines, cosines or inverse sines or cosines for this. Or any square roots for that matter. So it's fast.

不过,如果你并不需要碰撞的精确角度或点,只是想八分,你可以通过了解一些关于切线,这是优化此进一步:

But if you don't need an exact angle or point of collision and just want the octant you can optimize this further by understanding something about tangents, which is:

  • 0℃=黄褐色X - 其中= 1为0℃= X - 其中= 45度;
  • 谭X> = 1为45℃; = X&LT; = 90
  • 0> =谭X> = -1 0> = X => -45;
  • 谭X'LT = -1 -45> = X => -90;和
  • 谭X = TAN(X + 180)=棕褐色(X-180)。

这四个度范围对应四个八分圆的cirlce的。其他四个180度偏移。如上所证实的,切线可以简单地计算为:

Those four degree ranges correspond to four octants of the cirlce. The other four are offset by 180 degrees. As demonstrated above, the tangent can be calculated simply as:

        |y2 - y1|
tan X =  -------
        |x2 - x1|

失去了绝对值这个比例会告诉你四个八分圆的碰撞事故中(由上述切线范围)。要制定出详细的八分只是比较x1和x2,以确定哪些是最左边。

Lose the absolute values and this ratio will tell you which of the four octants the collision is in (by the above tangent ranges). To work out the exact octant just compare x1 and x2 to determine which is leftmost.

在其它单碰撞的八分圆偏移(C1上八分圆1装置C2上,2和6,图3和7,4和8等八分圆5)。

The octant of the collision on the other single is offset (octant 1 on C1 means octant 5 on C2, 2 and 6, 3 and 7, 4 and 8, etc).

这篇关于圆圈圈的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆