First off I must admit I'm really not good at math and am trying to learn but have had a hard time finding examples someone like myself can follow. I'm trying to figure out how to draw and calculate the parabolic arch from one point to hit a target.
I really appreciate any help on this, I have been struggling to figure out a this for the past 2 days and still am unable to come up with anything that works.
The best visual depiction I found was something like this but I am looking for a 2D solution.
<html>
<body>
<canvas id="myCanvas"></canvas>
<script>
function ranInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var canvas = document.getElementById("myCanvas");
canvas.width = 500;
canvas.height = 500;
let w = canvas.width;
let h = canvas.height;
document.body.appendChild(canvas);
var ctx = canvas.getContext("2d");
var ox = 20;
var oy = h - 20;
var cannon = { x: ox - 12, y: oy - 12, width: 50, height: 24 };
var target = { x: w - 150, y: h - 350, width: 42, height: 42 };
var ball = { x: ox, y: oy, r: 10, cx: ox, cy: oy };
function draw() {
ctx.fillStyle = "yellow";
ctx.fillRect(cannon.x, cannon.y, cannon.width, cannon.height);
ctx.fillRect(target.x, target.y, target.width, target.height);
ctx.fillStyle = "red";
ctx.fillRect(ball.cx, ball.cy, 4, 4);
}
var state = "ready";
function update() {
if (state === "flying") {
var x = ball.cx;
var y = ball.cy;
x += vx;
y += vy;
vy += 0.1;
ball.cx = x;
ball.cy = y;
ctx.fillStyle = "black";
ctx.fillRect(ball.cx, ball.cy, 4, 4);
if (ball.cy > h || ball.cx > w) {
state = "ready";
ball = { x: ox, y: oy, r: 10, cx: ox, cy: oy };
}
}
draw();
}
setInterval(update, 10);
var vx = 0;
var vy = 0;
document.addEventListener("click", function (e) {
ball = { x: ox, y: oy, r: 10, cx: ox, cy: oy };
ctx.clearRect(0, 0, canvas.width, canvas.height);
vy = -9.22;
vx = (e.pageX - ox) / 50;
state = "flying";
});
</script>
</body>
</html>