29.桌球知识:两个圆之间的碰撞检测

29.Pooled Knowledge

In this post we’re going to start work on a pool game. Pool is quite a nice example for a game, because really it’s a two-dimensional game. The balls are always (trick shots aside!) on the table, so you can create the game as a 2D game viewed from above.

这篇帖子我们打算着手设计一个桌球游戏。桌球是游戏设计的一个绝佳示例,因为它实际上是一个二维游戏。球(球杆在一侧撞击)始终在球台上,因此你可以创建一个俯视角的2D游戏。

One of the key aspects of implementing a pool game is to detect when the balls collide with each other and make them bounce accordingly. When viewed in 2D, from above, the balls are simply circles. So in this post we’ll look at how to detect when two circles collide with each other.

实现桌球游戏的一个关键方面是检测球何时互相碰撞并让他们相应地反弹。如果用2D视角从上面观察,球可以简单看做是圆。因此在这篇帖子里我们将看看如何检测两个圆何时相互发生碰撞。

The Shortest Path

最短路径

Detecting whether circles collide is quite simple, but makes use of a crucial observation: if two circles collide at all, then they will collide along the line between their two centres.

检测两个圆是否碰撞是非常简单的,但是使用了一个至关重要的观察:如果两个圆确实碰撞了,那么它们将会沿着两者的中心连线进行碰撞。

To help you understand this, I’ve put together a little Greenfoot scenario. There are two circles, one stationary and one that you move around with the mouse. A line is drawn between their two centres. Move them around, and notice that you can’t make the circles touch without them overlapping along the line between the two centres.

为了帮助你理解,我设计了一个小Greenfoot剧本将两者放在一起。其中有两个圆,一个静止而另一个可以用鼠标来四处移动。它们两者的中心间绘制了一条直线。四处移动它们,并且注意你不可能让圆接触的同时不让它们沿着中心连线发生重叠。

So then, let’s consider that line between the two centres of two circles, which (as ever) can be viewed as the hypotenuse of a right-angled triangle formed by the X and Y axes:

于是接下来,让我们考虑那条连接两个圆中点的直线,它(一如既往)可以被看作是由x和y轴所形成的直角三角形的斜边:

We can work out the total length of the line between the centres using Pythagoras: we just calculate the X distance and Y distance between the centres using subtraction, then Pythagoras gives us the distance. So once we have the distance, how do we work out if the circles overlap? Well, we just need a little logic: if the distance between the two centres is smaller than the sum of the radiuses, the circles must be overlapping — as above. If, on the other hand, the distance is greater than the sum of the radiuses, the circles can’t be overlapping — as below:

我们可以使用勾股定理算出中心连线的总长度:我们使用减法计算出两中点的x距离和y距离,接着使用勾股定理求出中心连线的距离。那么一旦得到了距离值,我们怎么知道两个圆是否重叠呢?好吧,我们只需要一点逻辑判断:如果中心距离小于半径之和,两圆必定重叠了——如上图所示。另一方面,如果距离大于半径之和,两圆不可能重叠——如下图所示:

(If the distance equals the sum, the circles are only just touching: whether you count this as a collision is a matter of preference).

(如果距离等于和,两圆则刚好触碰到:你是否将其算作是碰撞则随自己喜好决定)

The earlier scenario displays all the numbers I’ve been referring to, in the top right, soyou can have a play if you want, over on the Greenfoot site. Next time we’ll start working on the actual pool scenario.

早期的剧本在右上角显示了涉及到的所有数字,因此如果你愿意可以在Greenfoot网站上玩一下。下次我们将开始设计真正的桌球剧本。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值