# How can I make a program that hits balls in processing?

**URL:** https://discourse.processing.org/t/how-can-i-make-a-program-that-hits-balls-in-processing/25893
**Category:** Coding Questions
**Tags:** homework
**Created:** [December 2, 2020, 2:33pm UTC](https://discourse.processing.org/t/how-can-i-make-a-program-that-hits-balls-in-processing/25893 "2020-12-02T14:33:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![hahahajayden](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hahahajayden](https://discourse.processing.org/u/hahahajayden)
#### Post date: [December 2, 2020, 2:33pm UTC](https://discourse.processing.org/t/how-can-i-make-a-program-that-hits-balls-in-processing/25893/1 "2020-12-02T14:33:52Z")

</div>

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

hello! I am a novice processing programmer living in Korea.  
I used Google Translator, so I hope you understand even if the words are strange.

The problem I have is that the balls don’t bounce each other.

I made the ball move with the keyboard and mouse.  
and I used PVector to create a program where a ball bounces off a wall.

But I can’t make it physically bounce when the balls hit each other.

this is my code.

Please help me!

PVector location; // Location of shape1  
PVector location2; // Location of shape2  
PVector location3; // Location of shape3

PVector velocity; // Velocity of shape1  
PVector velocity2; // Velocity of shape2  
PVector velocity3; // Velocity of shape3

PVector mouse;

int tumx=1,tumy=1,tumx2=1,tumy2=1,tumx3=1,tumy3;  
int checkx=1, checky=1, yee=1, check2x=1, check2y=1, check3x=1, check3y=1;

void setup() {  
size(1200,600);

```
location = new PVector(300,300);
location2 = new PVector(600,300);
location3 = new PVector(900,300);

velocity = new PVector(0,0);
velocity2 = new PVector(0,0);
velocity3 = new PVector(0,0);

mouse = new PVector(mouseX,mouseY);

```

}

void draw() {

```
background(150);
ellipse(mouseX,mouseY,20,20);

//first ball
location.add(velocity);
if ((location.x > width) || (location.x < 0)) {
   velocity.x = velocity.x * -1;
 }
  
 if ((location.y > height) || (location.y < 0)) {
   velocity.y = velocity.y * -1;
 }
 
 //second ball
 location2.add(velocity2);
if ((location2.x > width) || (location2.x < 0)) {
   velocity2.x = velocity2.x * -1;
 }
  
 if ((location2.y > height) || (location2.y < 0)) {
   velocity2.y = velocity2.y * -1;
 }
 
 //third ball
 location3.add(velocity3);
if ((location3.x > width) || (location3.x < 0)) {
   velocity3.x = velocity3.x * -1;
 }
  
 if ((location3.y > height) || (location3.y < 0)) {
   velocity3.y = velocity3.y * -1;
 }

//start the first ball
if(keyPressed)
{
  if(yee==1)
  {
    velocity.x=round((location.x-mouseX)/50);
    velocity.y=round((location.y-mouseY)/50);
    checkx=0;
    checky=0;
    yee=0;
  }
}

// if the ball collide
//if((location2.x-location.x)*(location2.x-location.x)+(location2.y-location.y)*(location2.y-location.y)<=9216.5 && 9215.5<=(location2.x-location.x)*(location2.x-location.x)+(location2.y-location.y)*(location2.y-location.y))
if(location.dist(location2)<2304)
{
 
}

if(location.dist(location2)<2304)
{
  
}

if(location.dist(location2)<2304)
{
  
}

//make the ball slower

ball();

//draw a ball
stroke(0);
strokeWeight(2);
fill(255);
ellipse(location.x,location.y,48,48);
fill(225,0,0);
ellipse(location2.x,location2.y,48,48);
fill(225,225,0);
ellipse(location3.x,location3.y,48,48);

```

}

void ball()  
{  
if(checkx==0 || checky==0 || check2x==0 || check2y==0 || check3x==0 || check3y==0)  
{  
//ball1  
velocity.x=(velocity.x)\*0.995;  
text("X : "+nfc(velocity.x,1), 100, 100);  
if(-0.1\<velocity.x && velocity.x\<0.1)  
{  
velocity.x=0;  
checkx=1;  
if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)  
{  
yee=1;  
}  
}

```
 velocity.y=(velocity.y)*0.995;
 text("Y : "+nfc(velocity.x,1), 100, 130);
 if(-0.1<velocity.y && velocity.y<0.1)
 {
   velocity.y=0;
   checky=1;
   if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
   {
     yee=1;
   }
 }
 
 
 
 
 //ball2
   velocity2.x=(velocity2.x)*0.995;
   text("X : "+nfc(velocity2.x,1), 300, 100);

  if(-0.1<velocity2.x && velocity2.x<0.1)
  {
     velocity2.x=0;
     check2x=1;
     if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
     {
       yee=1;
     }
  }
     
     

velocity2.y=velocity2.y*0.995;
text("Y : "+nfc(velocity2.x,1), 100, 130);

if(-0.1<velocity2.y && velocity2.y<0.1)
{
   velocity2.y=0;
   check2y=1;
   if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
   {
     yee=1;
   }
}

//ball3

velocity3.x=velocity3.x*0.995;
text("X : "+nfc(velocity3.x,1), 600, 100);  
if(-0.1<velocity3.x && velocity3.x<0.1)
{
   velocity3.x=0;
   check3x=1;
   if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
   {
     yee=1;
   }
}
     
     

velocity3.y=velocity3.y*0.995;
text("Y : "+nfc(velocity3.x,1), 600, 130);
if(-0.1<velocity3.y && velocity3.y<0.1)
{
   velocity3.y=0;
   check3y=1;
   if(checkx==1 && checky==1 && check2x==1 && check2y==1 && check3x==1 && check3y==1)
   {
     yee=1;
   }
}

```

}

}

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [December 2, 2020, 4:07pm UTC](https://discourse.processing.org/t/how-can-i-make-a-program-that-hits-balls-in-processing/25893/2 "2020-12-02T16:07:39Z")

</div>

Hi @hahahajayden  
See here a tutorial about multiple objects bouncing. Just remove gravity.

> **[BouncyBubbles \\ Examples \\ Processing.org](https://processing.org/examples/bouncybubbles.html)**

---

<div class="post-metadata">

### Author: ![hahahajayden](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hahahajayden](https://discourse.processing.org/u/hahahajayden)
#### Post date: [December 3, 2020, 12:57am UTC](https://discourse.processing.org/t/how-can-i-make-a-program-that-hits-balls-in-processing/25893/3 "2020-12-03T00:57:39Z")

</div>

Thanks for answering the question!

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [December 3, 2020, 10:16am UTC](https://discourse.processing.org/t/how-can-i-make-a-program-that-hits-balls-in-processing/25893/4 "2020-12-03T10:16:58Z")

</div>

I guess you wrote this because you want a direct answer to your code.  
Honestly I tried to copy your code, but that came with a lot of (copy) errors. A single extra period in a code will prevent it to run, so I recommend learning first how to format a post correctly.

---

<div class="post-metadata">

### Author: ![hahahajayden](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hahahajayden](https://discourse.processing.org/u/hahahajayden)
#### Post date: [December 4, 2020, 12:00am UTC](https://discourse.processing.org/t/how-can-i-make-a-program-that-hits-balls-in-processing/25893/5 "2020-12-04T00:00:29Z")

</div>

Thank you! I will refer to it in the future.

And I completed the program with your help.  
Thank you so much!
