我正在尝试使用"阻尼"属性逐渐降低对象的速度.之后,我想使用速度移动对象的位置.它看起来像这样:
velocity.x *= velocity.damping;
velocity.y *= velocity.damping;
x += velocity.x;
y += velocity.y;
Run Code Online (Sandbox Code Playgroud)
可能没有那么简单,它工作正常,但这是我的问题:我正在使用deltaTime变量,它包含我的游戏循环的最后一次更新所花费的时间(以秒为单位).应用速度很容易:
x += velocity.x * deltaTime;
y += velocity.y * deltaTime;
Run Code Online (Sandbox Code Playgroud)
但是,当我乘以阻尼属性时,如何计算deltaTime?我的想法是,找到x或y中的位移并将deltaTime乘以,如下所示:
velocity.x += (velocity.x * velocity.damping - velocity.x) * deltaTime;
velocity.y += (velocity.y * velocity.damping - velocity.y) * deltaTime;
Run Code Online (Sandbox Code Playgroud)
原来这不起作用.我真的不明白为什么,但是当我测试它时,我会得到不同的结果.如果我只是忽略阻尼或将其设置为1.0,一切正常,所以问题必须在最后两行.