# Ball to ball collision

**URL:** https://discourse.processing.org/t/ball-to-ball-collision/27050
**Category:** Coding Questions
**Created:** [January 14, 2021, 5:49am UTC](https://discourse.processing.org/t/ball-to-ball-collision/27050 "2021-01-14T05:49:49Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![WasNeverHere](https://avatars.discourse-cdn.com/v4/letter/w/a587f6/32.png) [@WasNeverHere](https://discourse.processing.org/u/WasNeverHere)
#### Post date: [January 14, 2021, 5:49am UTC](https://discourse.processing.org/t/ball-to-ball-collision/27050/1 "2021-01-14T05:49:50Z")

</div>

I am trying to make it so that the circle that is moving (circleX) isn’t hit by the circles that are obstacles (circleD). I’ve tried a lot of different things and can’t find what’s wrong.

float xspeed = 2.5;

float x,y;

boolean upPressed = false;  
boolean downPressed = false;  
boolean leftPressed = false;  
boolean rightPressed = false;

float circleX = 50;  
float circleY = 487;

PImage img1, img2, img3;  
float[] circleD = new float[180];

int time;  
int wait = 5000;

void setup() {  
size(1000, 500);  
time=millis();  
rectMode(CENTER);  
img1 = loadImage(“stars.gif”);  
img2 = loadImage(“splash\_screen\_example2.png”);  
img3 = loadImage(“character\_1.png”);

for (int i = 0; i \< circleD.length; i++) {  
circleD[i] = random(height);  
}  
}

void draw() {

background(#18468E);  
//stars  
image(img1, 0, 0);  
img1.resize(1000,500);  
tint(255,255); // Tint blue  
image(img1, 0, 0);

//text  
fill(#FFFFFF);  
textSize(100);  
text(“Red”, 400, 187);  
textSize(75);  
text(“Ball”, 420, 247);  
textSize(50);  
text(“Dash”,440,287);

fill(#FF0303);  
textSize(100);  
text(“Red”, 400, 200, +5000);  
textSize(75);  
text(“Ball”, 420, 260);  
textSize(50);  
text(“Dash”,440,300);

if (millis() - time \> wait){ //splash screen

background(50);  
fill(#F57C0A);  
for (int i = 0; i \< circleD.length; i++) {  
float circleE = width \* i / circleD.length;  
ellipse(circleE, circleD[i], 18, 18);

```
circleD[i]++;

if (circleD[i] > height) {
  circleD[i] = 0;

```

}  
}  
}  
if (upPressed) {  
circleY=circleY-xspeed;

}  
if (downPressed) {  
circleY=circleY+xspeed;  
}  
if (leftPressed) {  
circleX=circleX-xspeed;  
}  
if (rightPressed) {  
circleX=circleX+xspeed;

}  
fill(#FF0303);  
ellipse(circleX, circleY, 13, 13);  
edges();

}

void keyPressed() {  
if (keyCode == UP) {  
upPressed = true;  
}  
else if (keyCode == DOWN) {  
downPressed = true;  
}

if (keyCode == LEFT) {  
leftPressed = true;  
}  
else if (keyCode == RIGHT) {  
rightPressed = true;  
}  
}

void keyReleased() {  
if (keyCode == UP) {  
upPressed = false;  
}  
else if (keyCode == DOWN) {  
downPressed = false;  
}  
else if (keyCode == LEFT) {  
leftPressed = false;  
}  
else if (keyCode == RIGHT) {  
rightPressed = false;  
}  
}  
void mousePressed() {  
loop();  
}

void mouseReleased() {  
noLoop();  
}  
void edges() {  
if (circleX \> width){  
image(img2, 0, 0);  
img2.resize(1000,500);

xspeed = xspeed \* 0.9;  
}  
if (circleX \< width - 1000){  
image(img2, 0, 0);  
img2.resize(1000,500);

xspeed = xspeed \* 0.9;  
}  
if (circleY \> height){  
image(img2, 0, 0);  
img2.resize(1000,500);  
xspeed = xspeed \* 0.9;  
}  
if (circleY \< height - 500){  
image(img2, 0, 0);  
img2.resize(1000,500);  
xspeed = xspeed \* 0.9;  
}  
}

---

<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: [January 15, 2021, 1:20pm UTC](https://discourse.processing.org/t/ball-to-ball-collision/27050/2 "2021-01-15T13:20:16Z")

</div>

When you remove gravity this is it: [BouncyBubbles \ Examples \ Processing.org](https://www.processing.org/examples/bouncybubbles.html)

Hey, and welcome to the forum!

Great to have you here!

Warm regards,

Chrisir 😉
