JS物理加速衰减

JS Physics Acceleration Decay

我正在创建一个物理引擎,它目前使用鼠标加速(随着时间的推移)在屏幕上移动框。我的目标是让应用于盒子的鼠标加速度随时间衰减 0.8 倍,但是我当前的方程式不会使鼠标加速度收敛到零。

Box acceleration/velocity 衰减方程:

_this.vx 是我希望衰减 0.8,但它复合(在图像中)。

Log of velocity values

 _this.update = function (t) {


        _this.x += _this.vx * 0.8 * t;
        _this.vx += (_this.ax *0.8) * t;



        console.log("Velocity: " + _this.vx);


    _this.y += _this.vy * t;
    _this.vy += (_this.ay + 440) * t;

鼠标加速度捕捉:

    var mouse = {

    update: function (t)
    {  
        mouse.ox = mouse.x;
        mouse.oy = mouse.y;

    },
    x: 0,
    y: 0,
    ox: 0,
    oy: 0,
    vx: 0,
    vy: 0,
    click: false
}

var now, after,timediff;

window.onmousemove = function (e)
{
    mouse.ox = mouse.x;
    mouse.oy = mouse.y;
    mouse.x = e.x;
    mouse.y = e.y;

    now = performance.now();
    timediff = now - after;
    mouse.vx = ((mouse.x - mouse.ox) / timediff)*100;
    mouse.vy = ((mouse.y - mouse.oy) / timediff)*100;
    after = now; 

    timediff = 0;
}
 _this.vx += (_this.ax *0.8) * t;

应该是

 _this.vx = (_this.vx *0.8) * t;//Assignment not addition

我还假设 _this.ax *0.8_this.vx *0.8

的拼写错误

此外,您应该检查 _this.vx 的最小值以停止更新,因为将分数乘以数字永远不会使它为零。为了应用程序的目的,应检查与零相同的最小值。