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[0].getX();

        double ballY = b[0].getY();

        double radiusb = Ball.getRadius();

        double ball4X = b[4].getX();

        double ball4Y = b[4].getY();

        double radiusb4 = Ball.getRadius();

        double ball5X = b[5].getX();

        double ball5Y = b[5].getY();

        double radiusb5 = Ball.getRadius();

        double ball6X = b[6].getX();

        double ball6Y = b[6].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[0], b[5]);

        }

        else if(c1 < collide1) {

            performAction(b[0],b[4]);

        }

        else if(c2 < collide2) {

            performAction(b[4], b[5]);

        }

        else if(c3 < collide3) {

            performAction(b[6], b[0]);

        }

        else if(c4 < collide4) {

            performAction(b[4], b[6]);

        }

        else if(c5 < collide5) {

            performAction(b[6], b[5]);

        }



    }

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:

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]);

                    }

                }



            }





            }

        }

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

No problem. Sorry about that.

Original code.

private void checkForCollision(Ball b[]) {

    double ballX = b[0].getX();
    double ballY = b[0].getY();
    double radiusb = Ball.getRadius();
    double ball4X = b[4].getX();
    double ball4Y = b[4].getY();
    double radiusb4 = Ball.getRadius();
    double ball5X = b[5].getX();
    double ball5Y = b[5].getY();
    double radiusb5 = Ball.getRadius();
    double ball6X = b[6].getX();
    double ball6Y = b[6].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[0], b[5]);
        }
        else if(c1 < collide1) {

            performAction(b[0],b[4]);

        }
        else if(c2 < collide2) {

            performAction(b[4], b[5]);
        }
        else if(c3 < collide3) {

            performAction(b[6], b[0]);
        }
        else if(c4 < collide4) {

            performAction(b[4], b[6]);
        }
        else if(c5 < collide5) {

            performAction(b[6], b[5]);
        }
    }

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());
            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]);
                    }
                }               
            }
        }
    }

.

Be a part of the DaniWeb community

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