I am writing a game in Javascript, Canvas, HTML5, and I just found a big problem, related to advanced maths. The game is in flat 2D, so you see the world from over. That means there is no gravity, only friction.
CODE:
var friction = 0.97
var target = {
x: 70,
y: 90,
}
var ball = {
x: 10,
y: 20,
vx: 0,
vy: 0,
}
var dx = ball.x - target.x
var dy = ball.y - target.y
var angle = Math.atan2(dy, dx)
var dist = Math.sqrt(dx * dx + dy * dy) // <--- AS FAR AS I'VE COME
var speed = ??? // <--- HERE IS THE PROBLEM
ball.vx = Math.cos(angle) * speed
ball.vy = Math.sin(angle) * speed
function update() {
ball.vx *= friction
ball.vy *= friction
ball.x += ball.vx
ball.y += ball.vy
// Drawing and other unneccesary things to involve here
window.requestAnimationFrame(update)
}
update()
So, my question is: How do I calculate the speed in the section where I create the ball, to make the ball stop at exactly the targets x and y position? As far as I've come I've only calculated the starting distance between the ball and the targets position.. Soo, any ideas? Is there an equation for this?