У меня есть точка, которая движется по линейной траектории с ускорением c. Я использую 2 подхода для прогнозирования положения точки -
вычисление по формуле:
vx = v0 + a * t;
x = (vx ^ 2 - v0 ^ 2) / (2a)
моделирование с:
скорость + = ускорение;
положение + = скорость;
И у меня другой результат. Не могу понять, что происходит, см. Код:
<!DOCTYPE html>
<html>
<head>
</head>
<body style="margin: 0;padding: 0">
<canvas id="id" width="1000" height="1000" style="border: 1px solid black;"></canvas>
<script>
(function(){
var canvas = document.getElementById('id');
var ctx = canvas.getContext('2d');
var fps = 6;
function Point(x, color) {
this.x = x;
this.acceleration = {x: 0};
this.speed = {x: 0};
this.color = color;
}
Point.prototype.update = function(ctx) {
this.speed.x += this.acceleration.x;
this.x += this.speed.x;
}
Point.prototype.draw = function(ctx) {
ctx.beginPath();
ctx.arc(this.x, 30, 5, 0, Math.PI * 2);
ctx.stroke();
ctx.fillStyle = this.color;
ctx.fill();
}
var ix = 10;
var p = new Point(ix, 'red');
p.acceleration.x = 1;
var p2 = new Point(ix, 'blue');
function calculateShift(v0, a, t) {
return (Math.pow((v0 + a * t), 2) - Math.pow(v0, 2)) / (2 * a)
}
var iteration = 0;
function step() {
iteration++;
if(iteration > 20) {
return;
}
ctx.clearRect(0, 0, 1000, 1000);
p.update();
var shift = calculateShift(0, p.acceleration.x, iteration);
p.draw(ctx);
p2.x = ix + shift;
p2.draw(ctx);
console.log(p.x, p2.x);
}
setInterval(function(){
window.requestAnimationFrame(step);
}, 1000 / fps)
})()
</script>
</body>
</html>