# Thread: For Loop collision

1. Member Join Date
Apr 2012
Posts
2
Rep Power
0

## For Loop collision

Hi I'm currently writing a program which applies collision detection to balls on the screen. My code works to a certain extent (the balls collide however they do not rebound away from each other) but it is not written efficiently.

Here is the code that partially works:

Java Code:
```private void checkForCollision(Ball b[]) {

double ballX = b.getX();
double ballY = b.getY();
double radiusb = Ball.getRadius();
double ball4X = b.getX();
double ball4Y = b.getY();
double radiusb4 = Ball.getRadius();
double ball5X = b.getX();
double ball5Y = b.getY();
double radiusb5 = Ball.getRadius();
double ball6X = b.getX();
double ball6Y = b.getY();
double radiusb6 = Ball.getRadius();

double a = ball5X - ballX;
double bb = ball5Y  - ballY;
double cc = ball4X - ballX;
double d = ball4Y - ballY;
double e = ball5X - ball4X;
double f = ball5Y - ball4Y;
double gg = ball6X - ballX;
double h = ball6Y - ballY;
double i = ball6X - ball4X;
double j = ball6Y - ball4Y;
double k = ball6X - ball5X;
double l = ball6Y - ball5Y;

double collide = radiusb4 + radiusb;
double collide1 = radiusb5 + radiusb;
double collide2 = radiusb5 + radiusb4;
double collide3 = radiusb6 + radiusb;
double collide4 = radiusb6 + radiusb4;
double collide5 = radiusb6 + radiusb5;

double c = Math.sqrt((double) (a * a) + (double) (bb * bb));
double c1 = Math.sqrt((double) (cc * cc) + (double) (d * d));
double c2 = Math.sqrt((double) (e * e) + (double) (f * f));
double c3 = Math.sqrt((double) (gg * gg) + (double) (h * h));
double c4 = Math.sqrt((double) (i * i) + (double) (j * j));
double c5 = Math.sqrt((double) (k * k) + (double) (l * l));

if (c < collide){

performAction(b, b);
}
else if(c1 < collide1) {

performAction(b,b);

}
else if(c2 < collide2) {

performAction(b, b);
}
else if(c3 < collide3) {

performAction(b, b);
}
else if(c4 < collide4) {

performAction(b, b);
}
else if(c5 < collide5) {

performAction(b, b);
}
}```
I know that this method should be written using a for loop to loop through each ball in turn and get the x and y values. The Ball.getRadius is a static method and and a comparison made with each other ball in subsequent slots of the array. I believe that this should be done using an outer and inner loop which eliminates the need to each ball with every other ball twice. I have made an attempt at it but I'm way off the mark I think so any help you could give me as to how it should be written would be greatly appreciated.

Thanks in advance

Here is my attempt:

Java Code:
```for(int i = 0; i < b.length; i ++) {

b[i] = new Ball(b[i].getX(), b[i].getY(), b[i].getDeltaX(), b[i].getDeltaY());
Ball.getRadius();

for(int j = 0; j < b.length; j ++) {

if(j != i) {

double yPosDifference = b[j].getDeltaY() - b[i].getDeltaY();
double xPosDifference = b[j].getDeltaX() - b[i].getDeltaX();
double collide = b[j].getRadius() + b[i].getRadius();
double centres = Math.sqrt((yPosDifference * yPosDifference) + (xPosDifference * xPosDifference));

if (centres < collide){

performAction(b[i], b[i]);
}
}
}
}
}```  Reply With Quote

2. ## Re: For Loop collision

Before you go any further, give your variables descriptive names. As it is, it's hard to even figure out what you're trying to do.  Reply With Quote

3. Member Join Date
Apr 2012
Posts
2
Rep Power
0

## Re: For Loop collision

Yeah thanks for that. Figured it out myself any way.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•