34.像香蕉一样飞行的水果:抛掷运动

34.Fruit Flies Like A Banana: Projectile Motion

Simple projectiles have been popular in games for a long time.Angry Birds recently combined it with collision physics to great success, but long ago, games like Worms and (my personal favourite) Scorched Earth were using projectiles as their central game mechanic. A projectile, for the purpose of this post, refers to an object that is launched through the air with a certain initial velocity, but after launch the motion is only affected by gravity, which makes the projectile follow a curve through the air known as aparabola:

简单的抛掷已经普遍运用于游戏中有很长时间了。愤怒的小鸟将其与碰撞物理结合取得了巨大成功,但是很久以前,像Worms以及(我自己喜欢的)Scorched Earth这类游戏正是使用抛掷作为其核心游戏机制。这篇帖子里探讨的抛掷,指的是某个物体以一个给定的初速度向空中发射,但是在发射后其运动仅仅受重力影响,这使得抛掷在空中沿着一条曲线运动,即所谓的抛物线:

The Game

游戏

Our next game will involve monkeys throwing bananas at each other. The monkeys will stand still, and our bananas will act like simple projectiles.

我们的下一个游戏将涉及猴子互相投掷香蕉。猴子将站立不动,而我们的香蕉将类似简单的抛掷运动。

To implement a projectile, we must first give the projectile an initial X and Y motion — in our case, that’s decided by where the player clicks to launch a banana. The projectile then keeps its horizontal (X) motion constant — it’s always flying sideways at the same speed. What varies is its vertical (Y) motion. It starts off being a high velocity upwards. Then, each frame, the vertical velocity is reduced by gravity, until it tips past zero and the velocity becomes negative, heading downwards. Let’s illustrate this.

为了实现一个抛掷,我们必须首先为抛掷设置一个初始的x和y运动——在我们的例子中,这由玩家鼠标点击的香蕉发射位置所决定。该抛掷接着保持其水平(x)运动不变——它将始终以相同的速度向侧面飞行。需要改变的是它的垂直(y)运动。它以一个很大的向上速度开始。接着,在每一帧里,垂直速度在重力影响下减小,直到减少为零,此后速度变为负数,并开始向下运动。让我们描述这个过程。

First, the banana is thrown, with an initial velocity — it is moving 5 squares upwards and 2 squares sideways each frame:

首先,香蕉以一个初始速度被抛出——它向上方移动5个方格而向侧面每帧移动2个方格:

We will set gravity to be one square reduction in velocity per frame. So the second frame, the banana still moves 2 squares sideways — its horizontal velocity is never altered — but only four squares upwards:

我们将设置重力以便在每一帧里为速度减少一个方格。于是在第二帧,香蕉仍然向侧面移动2个方格——它的水平速度从未改变——但是向上却仅仅只有4个方格:

The same happens in the next few frames — same sideways velocity, but upwards velocity is reduced by one square each frame, until it reaches zero:

在接下来的一些帧里会发生同样的事情——相同的侧面速度,但是向上的速度每一帧减少一个方格,直到为零:

So the vertical velocity has been 5, then 4, 3, 2, 1 and now 0 squares upwards. The pattern doesn’t alter just because the banana reached its peak — the next frame it becomes -1:

于是垂直速度向上数开始是5,接下来是4,3,2,1而现在是0个方格。运动形式并没有仅仅因为香蕉达到顶点而改变——下一帧它将变为-1:

Then it becomes -2, -3, -4 and -5, until the banana hits the ground:

接着它变为-2,-3,-4和-5,直到香蕉碰到地面:

So in summary: you can see that the banana in the diagram starts off with an upwards velocity of five. Each turn the velocity is reduced by one, and the effect this has is to reduce the banana’s velocity to zero, then make it increasingly negative. These two simple components — high initial upwards velocity, then constant reduction each frame — give the flight this parabola.

因此总结一下:你可以看到图中的香蕉以一个向上的速度5开始运动。每一步速度减1,这种效果将会使香蕉的速度减为零,接着将使其逐渐向负数增加。这两个简单的因素——很大的初始向上速度,以及每一帧持续的减少值——形成了这种抛物线的飞行。

The code to implement the banana’s flight is very short:

实现香蕉的飞行代码非常简单:

    private void fly()
    {
        velY -= ProjectileWorld.GRAVITY;
        moveTo(curX + velX, curY - velY);
    }

I take positive Y velocity to be upwards, so I subtract the effect of gravity, but then because Greenfoot has Y going downwards, I must subtract the upwards velocity for everything to work out. You can see how this works by playing with the example scenario on the Greenfoot website.

我使用y速度的正方向为向上,于是减去重力效果,但是由于Greenfoot使用向下的y正方向,于是我必须减去向下的速度使得程序正常运行。你可以在Greenfoot网站上玩一下示例游戏剧本来了解这是如何工作的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值