我正在進行一個簡單的2D遊戲。我正在做一個圖像的線性移動動畫到各個方向。涉及加速/減速的運動動畫
移動圖像的功能(move_to_right
)需要2個參數
Distance
:總行駛距離Duration
:持續時間,以覆蓋該距離
保持該圖像具有的類這些變量需要做線性運動:
distance_total
:商店總距離frame_total
(從「距離」參數採取):存儲總幀所需frame_spent
(由「持續時間」參數採取):多少幀動畫期間傳遞speed
:如何許多像素每幀的圖像移動,最初爲0- 基本圖像變量如x,y座標,寬度,高度等
粗糙代碼:
class Animated
def initialize
#all things needed to show a picture properly in the screen along with its properties such as x, y coordinates.
end
def move_to_right (distance,duration)
@distance_total = distance
@frame_total = duration
@frame_spent = 0
@speed = @distance_total/@frame_total
#the animation runs in game's loop, but for this time, to make it simple and easy to understand, I'll just use while statement
while @frame_spent <= @frame_total
self.x += @speed
@frame_spent += 1
end
end
end
$test = Animated.new
$test.move_to_right (100,60)
上面的代碼運行良好,但就是這樣,只是一個簡單的線性運動動畫。圖像移動100 pixel
向右in 60 frames
但
現在,我需要涉及的加速/減速的動態運動,這裏的情景:
- 持續時間的前30%涉及加速度。速度從0開始,並從幀提高到幀
- 接下來的40持續時間invoves 線性運動的%。速度不變並且不變
- 最後30%的持續時間涉及減速。速度從線性移動的最後一個速度開始,然後逐幀下降,直到最後停止。
- 那些場景肯定不是對總距離和持續時間有任何影響。這意味着,所有這些都必須在60幀內覆蓋100個像素。
問題是: 1.我需要什麼變量來執行上述場景?他們用於什麼? 2.什麼是步驟?如果可以的話,還請包括方程式/計算以使其清楚
您不必用代碼回答;文本解釋或方向將是一個很大的幫助。沒有語言限制,所以如果你想使用任何絕對沒問題的語言來幫助代碼。在此先感謝:)
如果這是特定語言的代碼,則應該添加一些[語法高亮顯示](http://meta.stackexchange.com/questions/981/syntax-highlighting-language-hints)。 – Dukeling