Why are my balls disappearing? [closed]
Solution 1:
Your error comes from this line initially:
var direction1 = Math.atan2(ball1.velocitY, ball1.velocityX);
You have ball1.velocitY
(which is undefined
) instead of ball1.velocityY
. So Math.atan2
is giving you NaN
, and that NaN
value is propagating through all your calculations.
This is not the source of your error, but there is something else that you might want to change on these four lines:
ball1.nextX = (ball1.nextX += ball1.velocityX);
ball1.nextY = (ball1.nextY += ball1.velocityY);
ball2.nextX = (ball2.nextX += ball2.velocityX);
ball2.nextY = (ball2.nextY += ball2.velocityY);
You don't need the extra assignments, and can just use the +=
operator alone:
ball1.nextX += ball1.velocityX;
ball1.nextY += ball1.velocityY;
ball2.nextX += ball2.velocityX;
ball2.nextY += ball2.velocityY;
Solution 2:
There's an error in the collideBalls
function:
var direction1 = Math.atan2(ball1.velocitY, ball1.velocityX);
It should be:
var direction1 = Math.atan2(ball1.velocityY, ball1.velocityX);