# BOUNCING circle in radius

please format code with </> button * homework policy * asking questions

The assignment is:

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.

How far have you gotten with the assignment? What are the parts you don’t understand? Try and elaborate.
As for the second part of the task:

``````float factor = 0.05;

//void setup(), void draw()

void mousePressed() {
velX = (mouseX-x)*factor;
valY = (mouseY-y)*factor;
}
``````

Simon

// clarification of variables

int x = 300/2; // position of the circle

int y = 300/2; // position of the circle

int velx = 2; // speed in X direction

int vely = 4; // velocity in the Y direction

int r = 10; // radius of the circle

void setup () {

size (300,300);

}

void draw () {

background (0,0,0);

ellipse (x, y, r, r); //circle

x = x + velx; // equations of motion

y = y + vely; // equations of motion

if (y> height-r) {// height and width as a function of radius

`````` vely = -vely;
``````

}

if (y <0 + r) {

`````` vely = -vely;
``````

}

if (x> width-r) {

`````` velx = -velx;
``````

}

if (x <0 + r) {

`````` velx = -velx;
``````

}

if (mousePressed == true) {

`````` velx = velx + 2;

vely = velx + 2;
``````

}

}

This is what i have so far but i dont know how to comment it because i dont really understand it.
And apparently The variable radius is not well used.

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;
}
``````

Simon