我正在創建一個物理引擎,它目前使用鼠標加速(隨着時間的推移)在屏幕上移動框。我的目標是將鼠標加速度應用於盒子隨時間衰減0.8倍,但是我的當前等式不會使鼠標加速度收斂到零。JS物理加速衰減
盒加速度/速度衰變方程:
_this.vx是我希望由0.8至有衰減,但它的化合物(在圖像)。
_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.update'處的't'是什麼? – guest271314
我的歉意,它是一個包含來自時鐘功能的時間的變量,它在更新中用於測量以秒爲單位的時間。如px/s –
't'只會增加,是嗎? – guest271314