# BOUNCING circle in radius

**URL:** <https://discourse.processing.org/t/bouncing-circle-in-radius/27535>\
**Category:** Beginners\
**Created:** [February 1, 2021, 7:57pm UTC](https://discourse.processing.org/t/bouncing-circle-in-radius/27535 "2021-02-01T19:57:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jimena](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jimena](https://discourse.processing.org/u/Jimena)\
**Post date:** [February 1, 2021, 7:57pm UTC](https://discourse.processing.org/t/bouncing-circle-in-radius/27535/1 "2021-02-01T19:57:14Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

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.

---

<div class="post-metadata">

**Author:** ![WeinSim](https://avatars.discourse-cdn.com/v4/letter/w/db5fbb/32.png) [@WeinSim](https://discourse.processing.org/u/WeinSim)\
**Post date:** [February 1, 2021, 11:28pm UTC](https://discourse.processing.org/t/bouncing-circle-in-radius/27535/2 "2021-02-01T23:28:21Z")

</div>

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:

```auto
float factor = 0.05;

//void setup(), void draw()

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

```

Simon

---

<div class="post-metadata">

**Author:** ![Jimena](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jimena](https://discourse.processing.org/u/Jimena)\
**Post date:** [February 2, 2021, 7:55am UTC](https://discourse.processing.org/t/bouncing-circle-in-radius/27535/3 "2021-02-02T07:55:31Z")

</div>

// 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.

---

<div class="post-metadata">

**Author:** ![WeinSim](https://avatars.discourse-cdn.com/v4/letter/w/db5fbb/32.png) [@WeinSim](https://discourse.processing.org/u/WeinSim)\
**Post date:** [February 2, 2021, 1:18pm UTC](https://discourse.processing.org/t/bouncing-circle-in-radius/27535/4 "2021-02-02T13:18:41Z")

</div>

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

> [@Jimena](#):
>
> The variable radius is not well used

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:

```auto
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
