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:

``````private void checkForCollision(Ball b[])

double ballX = b.getX();

double ballY = b.getY();

double ball4X = b.getX();

double ball4Y = b.getY();

double ball5X = b.getX();

double ball5Y = b.getY();

double ball6X = b.getX();

double ball6Y = b.getY();

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

Here is my attempt:

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

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

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 centres = Math.sqrt((yPosDifference * yPosDifference) + (xPosDifference * xPosDifference));

if (centres < collide){

performAction(b[i], b[i]);

}

}

}

}

}
``````

## All 3 Replies

Can you remove the extra blank lines in the code? They spread the code out too much and make it hard to read.

Original code.

``````private void checkForCollision(Ball b[]) {

double ballX = b.getX();
double ballY = b.getY();
double ball4X = b.getX();
double ball4Y = b.getY();
double ball5X = b.getX();
double ball5Y = b.getY();
double ball6X = b.getX();
double ball6Y = b.getY();

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 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);
}
}
``````

Heres my attempt at the problem

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

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

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 centres = Math.sqrt((yPosDifference * yPosDifference) + (xPosDifference * xPosDifference));

if (centres < collide){

performAction(b[i], b[i]);
}
}
}
}
}
``````

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.