Skip to content

Instantly share code, notes, and snippets.

@CodeDraken
Created December 11, 2019 19:33
Show Gist options
  • Save CodeDraken/3de89ccab25a851f79a39a798ac25564 to your computer and use it in GitHub Desktop.
Save CodeDraken/3de89ccab25a851f79a39a798ac25564 to your computer and use it in GitHub Desktop.
// bottom bound / floor
if (ball.y + ball.radius >= canvas.height) {
// reverse direction and lose energy from bouncing
ball.velY *= -ball.bounce
// reset position
ball.y = canvas.height - ball.radius
// slow down the ball's X velocity with friction
ball.velX *= friction
}
// top bound / ceiling
if (ball.y - ball.radius <= 0) {
ball.velY *= -ball.bounce
ball.y = ball.radius
ball.velX *= friction
}
// left bound
if (ball.x - ball.radius <= 0) {
ball.velX *= -ball.bounce
ball.x = ball.radius
}
// right bound
if (ball.x + ball.radius >= canvas.width) {
ball.velX *= -ball.bounce
ball.x = canvas.width - ball.radius
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment