polaryeti Posted January 19, 2023 Share Posted January 19, 2023 function checkBoundaryCollision() { if (x - ballRadius <= 0 || x + ballRadius >= canvasWidth) { dx = -dx; } if (y - ballRadius <= 0 || y + ballRadius >= canvasHeight) { dy = -dy; } } Full code here: https://codepen.io/pelko/pen/gOjXdaq Quote Link to comment Share on other sites More sharing options...
kicken Posted January 19, 2023 Share Posted January 19, 2023 x is the center of the ball horizontally, y is the center of the ball vertically. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted January 19, 2023 Solution Share Posted January 19, 2023 The closest x or y can be to the edge is the radius of the ball. When either of them reach that position, change direction. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.