6.通過加速度來實現跳躍和下落.

6.What Goes Up Must Come Down: implementation falling and jumping through acceleration.

This post is about making things jump up and fall down (e.g. for platform games), or: modelling acceleration.

這篇帖子討論如何跳躍及下落(例如平臺遊戲),或者說:模擬加速度。

When building 2D games and simulations in environments like Scratch or Greenfoot, you can either use the 2D view to provide a top-down view on the world, or a side-on view. Platform games (Sonic, Mario, etc) are the main examples of the latter, and in these games, you often want to make things fall down when there is no solid ground beneath them, and to allow the character to jump around. This post will look at how to do this: it’s actually best to start by implementing falling first, and then implement jumping.

在類似於Scratch或Greenfoot這樣的環境中創建2D遊戲或仿真程序的時候,你可以在遊戲世界中提供俯視的2D視角,或者一個側視的視角。平臺遊戲(索尼克,超級瑪麗等)是後者的主要代表,在這些遊戲中,當物體下方沒有堅實的地面時你通常希望它們能夠下落,同時希望讓遊戲角色能夠跳躍。這篇帖子將探討如何去實現:實際上最好從實現物體下落開始,然後實現跳躍。

Falling Down

下落

One way to make things fall is to move them down the screen at a constant rate. However, this often doesn’t look quite right — because it’s not the way that the real world works! Gravity produces an acceleration downwards, so when things fall down, they increase speed as they fall. In order to model the effect of acceleration, you must keep track of the current speed in a variable. Here’s the code in Greenfoot:

一種實現物體下落的辦法是讓它們以固定的速率向屏幕下方移動。然而這通常看起來不是很合適——因爲現實世界中的情形不是這樣的!由於重力造成了一個向下的加速度,因此當物體下落時,它們的速度是不斷增加的。爲了模仿加速度的效果,必須定義一個變量來保存當前的速度值。在Greenfoot中可用以下代碼實現:

public class AccFall extends Actor
{
    double speed = 0;
    double exactY;

    public void act() 
    {
        speed += 0.4;
        exactY += speed;
        setLocation(getX(), (int)exactY);
    }    

    protected void addedToWorld(World world)
    {
        exactY = getY();
    }
}

This code is a bit more fiddly than it needs to be, due toGreenfoot using integers to store coordinates, and me using an acceleration to fall at that’s not a whole number (otherwise it’s a bit too fast). To visualise the difference between falling at a constant rate, and falling at an accelerating rate,I’ve coded both in the same scenario. You can watch by clicking the apples below — the green apple on the left-hand side falls at a constant rate, while the red apple on the right-hand side falls at an accelerating rate. Try covering up one side with your hand and just watch the other side, to try to see the difference:

以上代碼中我們沒使用整數來實現加速,實際上並不需要如此精確,因爲Greenfoot使用整型來存儲座標值(因此顯得稍微有點快)。爲了可視化地顯示勻速下落和加速下落的區別,我將兩者編寫到了同一個遊戲劇本中。你可以通過點擊下圖所示的蘋果來觀察——左邊的綠蘋果勻速下落,右邊的紅蘋果加速下落。試着用手蓋住一個蘋果然後觀察另一個,看看兩者有何區別:

Jumping Up

跳躍

The other half of implementing a platform game is allowing the character to jump. It turns out that this is quite simple to model: you can make the character jump up by setting the speed variable to be a large negative value (in effect, an instantaneous upwards acceleration). This causes the character to move upwards, and thereafter gravity takes over by reducing the speed (downwards acceleration is equivalent to a slowing of upwards speed) until the speed takes the character downwards again, just like the red apple was falling.

編寫平臺遊戲的另一個任務是實現角色的跳躍。這很容易實現:可以通過將速度變量設爲一個很大的負數值來實現角色的跳起(相當於一個向上的瞬時速度)。這將導致角色向上移動,在那之後重力將使得速度減慢(此時向下加速相當於向上減速)直到速度改變到足以使得角色重新下落,就如同紅蘋果下落的情形。

You may not have followed all that in text, so here’s how you can see it in action. I madea scenario with a frog on a log that can jump (press up to jump) . If you watch carefully, you can see that the frog slows until it hits the peak of its jump and then accelerates as it comes back down. Greenfoot has a useful “Inspect” feature that allows us to see exactly what is happening.

或許你曾經在書本上已經瞭解了這些原理,在這我們看看如何在實際中去應用。我製作了一個遊戲劇本,其中一隻站在木頭上的青蛙可以跳躍(按up鍵起跳)。如果你仔細觀察,你會發現青蛙緩慢上升直到跳躍的最高點,然後加速向下掉落。Greenfoot中有一個有用的“查看”功能讓我們去精確地觀察發生了什麼。

 

If you want to play along yourself, open the scenario in Greenfoot. Hit compile in the bottom-right, and make sure you can see the frog on the log. Right-click on the Frog and go to “Inspect”. Move the inspect window so you can see it alongside the main Greenfoot window:

如果你想自己遊戲,可以在Greenfoot中將它打開。點擊右下角的編譯按鈕,確定你能看到木頭上得青蛙。用鼠標右鍵點擊青蛙並選擇“查看”。 移動查看窗口以便你能夠在Greenfoot主窗口旁邊看到它,如圖:

The two variables of interest are “int y” (currently 222) and “private int verticalSpeed” (currently 0). Right-click on the frog and select the method “void jump()” to call it. You’ll notice the frog will jump up (by 30: y becomes 192, and verticalSpeed becomes -30). That’s the instant upwards acceleration. Press the Act button once. You’ll see that verticalSpeed becomes -27, and y becomes 165 (which is 192 + (-27), old position plus vertical speed). Press it again, and you get -24 and 141 and so on. Press Act a few more times in quick succession and you’ll be able to see (looking at the frog) that the speed of the frog’s rise slows until it hits the peak of its jump, when verticalSpeed is zero. After that, verticalSpeed increases and the frog falls faster and faster downwards (keep clicking Act) until it hits the log again, when the speed is set back to zero.

需要關注的兩個變量是“整型變量 y”(當前值是222)和“私有整型變量verticalSpeed"(當前值爲0)。在青蛙上單擊鼠標右鍵並調用“void jump()” 方法。你會發現青蛙將跳起來(跳起30個單位:y座標值變爲192,verticalSpeed值變爲-30)。那便是向上的瞬時加速。按一次“Act”按鈕,你將發現verticalSpeed 值變爲-27,y值變爲165(192+(-27),即舊座標加上垂直速度值)。再按一次,上兩個值將變爲-24和141,以此類推。 快速地多按幾次Act按鈕,你將發現(觀察青蛙)青蛙的高度緩慢上升直到它抵達跳躍的最高點,此時verticalSpeed 值爲0.在那之後,verticalSpeed 值增加,同時青蛙下落得越來越快(持續點擊Act按鈕來觀察)直到它再次碰到木頭,此時速度值重新設爲0.

So the whole time, the verticalSpeed increases at a rate of +3. On the way up this causes the negative speed to get closer to zero, slowing the frog, but on the way down it increases the positive speed, causing the frog to fall faster and faster. This can seem a bit magical, but this is simply the effect of a constant acceleration from gravity, causing the arc of the frog’s jump. If you don’t quite understand, I encourage you to play more with the scenario to get a feel for it .

因此在整個過程中,verticalSpeed 值每次增加+3個單位。在跳起過程中這將使得負速度值趨向於0,青蛙上升放緩,但是在下落過程中它卻增加正速度值,使得青蛙掉落越來越快。這看起來有一點神奇,但這是重力造成的簡單的恆加速效果,使得青蛙跳躍成弧線。如果你不是十分清楚,我建議你多玩一下去感受它。

發佈了0 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章