2014-12-19 65 views
0

我怎樣才能得到的東西從X去Xgoal速度在Y秒? 的goalVelocity設置爲(100,100)和它接近它,但它需要的時間太長到那裏。加速到X速度在Y秒

我可以將frameDelta乘以20或100之類的數字,但我想知道要乘以frameDelta以在幾秒鐘內達到goalVelocity

速度,goalVelocity和起源都是Vec2f和frameDelta是float

現在我有這樣的代碼:

velocity = approach(goalVelocity, velocity, frameDelta); 
origin = origin + velocity * frameDelta; 

的方法的代碼是:

inline float approach(float flGoal, float flCurrent, float dt) 
{ 
    float flDifference = flGoal - flCurrent; 

    if (flDifference > dt) 
     return flCurrent + dt; 
    if (flDifference < -dt) 
     return flCurrent - dt; 

    return flGoal; 
} 

inline Vec2f approach(Vec2f flGoal, Vec2f flCurrent, float dt) 
{ 
    return Vec2f(approach(flGoal.x, flCurrent.x, dt), approach(flGoal.y, flCurrent.y, dt)); 
} 
+1

你知道爲什麼你使用'inline'? – 2014-12-19 05:58:21

+0

我得到這個代碼在網上,我假設它只是因爲它是如此簡單的功能。 – 2014-12-19 05:59:40

+0

你說'在Y秒內加速到X速度'。我看到你將'X'傳遞給'approach'(以'flGoal'的形式),但我沒有看到你傳遞了'Y'。 – 2014-12-19 06:00:23

回答

0

也許我誤解了你的問題,但加速只是速度差除以時間,所以只乘dt與X/Y。

0

一個基本的物理引擎應該是這個樣子

Vec2f position, velocity, acceleration; 

    while (true) 
    { 
    acceleration = button ? thrust : 0; 
    velocity += acceleration * timeDelta; 
    position += velocity * timeDelta 
    redraw space ship at position; 
    sleep (timeDelta); 
    } 

,如果你想從0到去X速度在Y秒,然後thrust = X/Y