Create a circle of radius r initially in the center of the screen, which moves with a certain speed velX and velY.
Initialization: x = width / 2; y = height / 2;
x = x + velX; y = y + velY;
Make it bounce off the edges of the canvas:
if (x> width-r) {velX = -velX;}…
Add a function that changes the direction of the velocity when the mouse is pressed, making the velocity proportional to the distance the mouse is located and towards the mouse position.
You can remove the if statement at the end of void draw(), instead add the void mousePressed() method from my previous response.
I’m not quite sure what you mean by
as it is implemented almost 100% correctly. Not only do you need to invert the x / y velocity when the ball hits the edge, but you also need to set the x / y position to the outermost possible position, as otherwise it would get stuck. It should look something like this:
if (x < r) {
velx *= -1;
x = r;
}
if (x > width-r) {
velx *= -1;
x = width-r;
}
if (y < r) {
vely *= -1;
y = r;
}
if (y > height-r) {
vely *= -1;
y = height-r;
}