Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By kjkrum

Thread: For Loop collision

  1. #1
    lj81 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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[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:

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

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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.
    DarrylBurke likes this.
    Get in the habit of using standard Java naming conventions!

  3. #3
    lj81 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: For Loop collision

    Yeah thanks for that. Figured it out myself any way.

Similar Threads

  1. Collision detection
    By mwr1976 in forum Java 2D
    Replies: 2
    Last Post: 04-14-2012, 12:45 PM
  2. Collision
    By PhQ in forum New To Java
    Replies: 5
    Last Post: 12-12-2011, 02:20 AM
  3. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-07-2011, 11:40 PM
  4. Why is my collision not working?
    By CNew in forum New To Java
    Replies: 8
    Last Post: 12-06-2010, 09:17 PM
  5. Collision
    By shadycharacter in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 09:58 PM

Posting Permissions

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