假设我有:
int currentVelocity; //can be negative
int targetVelocity; //can also be negative
void updateVelocity() // called every 100ms
{
int limit = 5;
}
如何在每次迭代时使速度更接近目标速度,且最大绝对变化为 5?
假设我的当前速度是 -20,目标速度是 -26
我的最大绝对增幅是 5。
首次调用 updateVelocity() 时,currentVelocity 变为 -25 再次调用时,currentVelocity为-26
除非目标速度改变,否则将永远如此。
必须在更新函数中添加什么才能做到这一点?
谢谢
最佳答案
一种简单的方法。
int currentVelocity; //can be negative
int targetVelocity; //can also be negative
void updateVelocity() // called every 100ms
{
int limit = 5;
int delta = targetVelocity - currentVelocity;
if (delta > limit)
currentVelocity += limit;
else if (delta < -limit)
currentVelocity -= limit;
else
currentVelocity = targetVelocity;
}
关于c++ - 将更改限制为最大金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22276370/