简体   繁体   English

在Y秒内加速到X速度

[英]Accelerate to X velocity in Y seconds

How can I get something to go from X to Xgoal velocity in Y seconds? 如何在Y秒内获得从X到Xgoal速度的变化? The goalVelocity is set to (100, 100) and it does approach it but it takes way too long to get there. goalVelocity设置为(100,100),并且确实达到了目标速度,但到达目标位置所需的时间太长。

I can multiply frameDelta by some number like 20 or 100 but I want to find out what to multiply frameDelta by to get it to reach goalVelocity in some number of seconds. 我可以将frameDelta乘以某个数字,例如20或100,但是我想找出将frameDelta乘以frameDelta ,以使其在一定秒数内达到goalVelocity速度。

velocity, goalVelocity and origin are all Vec2f and frameDelta is a float 速度,目标速度和原点都是Vec2f ,frameDelta是float

Right now I have this code: 现在我有以下代码:

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

The code for approach is: 该方法的代码是:

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));
}

也许我误解了您的问题,但是加速度只是速度差除以时间,因此只需将dt与X / Y相乘即可。

A basic physics engine should look something like 基本的物理引擎应该看起来像

   Vec2f position, velocity, acceleration;

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

if you want to go from 0 to X velocity in Y seconds, then thrust = X/Y . 如果要在Y秒内从0变为X速度,则thrust = X/Y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM