# How to make object bounce off another object

**URL:** https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595
**Category:** Coding Questions
**Created:** [March 25, 2019, 7:13am UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595 "2019-03-25T07:13:03Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)
#### Post date: [March 25, 2019, 7:13am UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/1 "2019-03-25T07:13:03Z")

</div>

Have gotten down how to make a ball bounce with gravity. Now I want that ball to hit a moving object and bounce off it. Any help on how to to do that?

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [March 25, 2019, 7:32am UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/2 "2019-03-25T07:32:47Z")

</div>

-a- check ( every draw loop ) for the distance between the 2 objects  
[https://processing.org/reference/dist\_.html](https://processing.org/reference/dist_.html)  
if that is smaller as the sum of the 2 radius BOUNCE

-b- for the bounce could have easy … complicated rules:

- just change the speed vector of the ball
- change speed vector of ball and object
- apply physics math of impuls F \* t = mass \* delta speed m\*dv
- if not center collision find a rebounce angula

---

<div class="post-metadata">

### Author: ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)
#### Post date: [March 25, 2019, 10:08am UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/3 "2019-03-25T10:08:56Z")

</div>

```auto
the x-value of my object is mouseX.
Actually, ill send my code if you would like to help:

float circleX=1; //direction of ball
float circleY=1; //direction of ball

float speedX=3;//speed of ball
float speedY=1;

float xpos; //position of ball
float ypos; //position of ball

void setup() {
  size(640,480);
  
    //starting position of ball
  xpos=0;
  ypos=0;
  
}

void draw() {
  
    //background of game
  background(44,56,245);
  fill(2,188,34);
  noStroke();
  rect(0,440,width,height);
  
    //Bill Movement
   
  pushMatrix();
  translate(mouseX,280);
  Bill();
  popMatrix();
    
    //Ball
  ypos+=speedY;
  xpos+=speedX;
  
  
    //Ball
  fill(180);
  stroke(0);
  strokeWeight(2);
  ellipse(xpos,ypos,50,50);
  
    //if ball goes out of area, turn it back
  if(xpos>width-50 || xpos<0){
    speedX*=-1;
  }
  
    
   //grav
   if(ypos>height-50){
    speedY*=-1*.99;
  
  }else{
   speedY+=1; 
  }
  
  if(ypos<0){
   speedY*=-1; 
  }
  
  
    //Ball reset
  if(speedY==0){
    xpos=0;
    ypos=0;
  }
}
Btw my object is Bill()

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 25, 2019, 10:26am UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/4 "2019-03-25T10:26:15Z")

</div>

See here

Strip the gravity

[https://processing.org/examples/bouncybubbles.html](https://processing.org/examples/bouncybubbles.html)

---

<div class="post-metadata">

### Author: ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)
#### Post date: [March 25, 2019, 12:39pm UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/5 "2019-03-25T12:39:56Z")

</div>

Can’t, i have to have it included for my question.  
Basically im supposed to juggle the ball on my characters head

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 25, 2019, 12:41pm UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/6 "2019-03-25T12:41:36Z")

</div>

Okay, I just wanted to show you the example

Chrisir

---

<div class="post-metadata">

### Author: ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)
#### Post date: [March 25, 2019, 12:49pm UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/7 "2019-03-25T12:49:17Z")

</div>

thanks anyway,

Bobby

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [March 25, 2019, 1:00pm UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/8 "2019-03-25T13:00:09Z")

</div>

pls format you code by  
PASTE it into

```auto
</> code formatter

```

and i think there is the code for the other object missing

did you start about the distance calculation?

---

<div class="post-metadata">

### Author: ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)
#### Post date: [March 25, 2019, 2:36pm UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/9 "2019-03-25T14:36:26Z")

</div>

Sorry about that! Yeah i was abit confused about the distance from the points as the ball would constantly bouncing, while the character would follow my mouseX.

i left out the code of the character as i thought would be abit messy and that its just a group of shapes lol. Thank you for spending ur time to help me btw.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [March 25, 2019, 3:07pm UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/10 "2019-03-25T15:07:45Z")

</div>

so you just make it a rectangle.  
bill.x by mouseX, bill.y fix 280,  
the width bill.w of the rect would be critical for the bounce  
( collision check with the ball )  
in words:  
if ball ( center ) posx \> bill.x but \< bill.x+bill.w a bounce is possible.  
when ball posy \< bill.y ( and fall down ) it will hit:  
if ball posy+radius \>= bill.y

and what would happen then? bounce up like from ground?

---

<div class="post-metadata">

### Author: ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)
#### Post date: [March 25, 2019, 3:23pm UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/11 "2019-03-25T15:23:36Z")

</div>

ill show you my character. I do not understand how i can make a variable for Bills position;

```auto
void Bill(){
  fill(0);
  rect(100,100,60,50); //head
  fill(255);
  rect(110,120,15,5); //left eye
  rect(135,120,15,5); //right eye
  ellipse(130,136,10,2); //mouth
  fill(0);
  rect(85,96,90,4); //hat
  
  fill(250,105,73);
  rect(110,150,40,20); //body
  
  fill(0);
  rect(110,170,5,10); //left foot
  rect(145,170,5,10); //right foot
    

}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [March 25, 2019, 3:32pm UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/12 "2019-03-25T15:32:04Z")

</div>

the bounce line would be top of HAT?  
bill\_x = mouseX+85; // translate + hat x  
bill\_y = 280+96; // fix in translate + hat y  
bill\_w = 90; // hat rect w

---

<div class="post-metadata">

### Author: ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)
#### Post date: [March 26, 2019, 7:48am UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/13 "2019-03-26T07:48:22Z")

</div>

```auto
billX= mouseX + 85;
  billY= 280 + 96;
  billW= 90;
  
  if(xpos == billX){
    speedX*=-1;
  }
  if (ypos == billY){
    speedY*=-1;
  }
  

```

it bounces off something now, im not sure what. Also im not sure how to implement that the width of the hat is area to bounce off- reason to why billW is not being used

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [March 26, 2019, 8:56am UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/14 "2019-03-26T08:56:31Z")

</div>

> [@kll](#):
>
> in words:  
> if ball ( center ) posx \> bill.x but \< bill.x+bill.w a bounce is possible.  
> when ball posy \< bill.y ( and fall down ) it will hit:  
> if ball posy+radius \>= bill.y

- untested idea

```auto
  billX= mouseX + 85;
  billY= 280 + 96;
  billW= 90;
  
  if((xpos >= billX) && (xpos <= billX+billW)) {
  if (ypos + radius >= billY){
     speedX*=-1;
     speedY*=-1;
    }
  }

```

---

<div class="post-metadata">

### Author: ![Blink\_Once](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Blink\_Once](https://discourse.processing.org/u/Blink_Once)
#### Post date: [April 4, 2019, 10:14am UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/15 "2019-04-04T10:14:08Z")

</div>

Hi, did you manage to get your code to work?

---

<div class="post-metadata">

### Author: ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)
#### Post date: [April 4, 2019, 12:32pm UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/16 "2019-04-04T12:32:50Z")

</div>

No ☹ sadly the code will still not make the ball bounce of the hat.  
I think the reason has to do with the positioning of my character. Maybe i should make my hat a separate entity and work from there?

---

<div class="post-metadata">

### Author: ![Blink\_Once](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Blink\_Once](https://discourse.processing.org/u/Blink_Once)
#### Post date: [April 4, 2019, 11:54pm UTC](https://discourse.processing.org/t/how-to-make-object-bounce-off-another-object/9595/17 "2019-04-04T23:54:59Z")

</div>

I have a similar question and i have no idea how to work it out as well that’s why i was asking
