JS物理加速衰变

时间:2017-05-13 02:52:15

标签: javascript physics

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

箱加速度/速度衰减方程:

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

1 个答案:

答案 0 :(得分: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以停止更新,因为将分数乘以数字永远不会使其为零。为了应用的目的,应检查最小值是否为零。