Results 1 to 3 of 3
Thread: For Loop collision
 04242012, 11:16 PM #1Member
 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[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]); } }
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]); } } } } }
 04262012, 10:26 PM #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.
Get in the habit of using standard Java naming conventions!
 04272012, 09:47 PM #3Member
 Join Date
 Apr 2012
 Posts
 2
 Rep Power
 0
Similar Threads

Collision detection
By mwr1976 in forum Java 2DReplies: 2Last Post: 04142012, 12:45 PM 
Collision
By PhQ in forum New To JavaReplies: 5Last Post: 12122011, 02:20 AM 
Collision Detection
By sunde887 in forum Java 2DReplies: 2Last Post: 10072011, 11:40 PM 
Why is my collision not working?
By CNew in forum New To JavaReplies: 8Last Post: 12062010, 09:17 PM 
Collision
By shadycharacter in forum New To JavaReplies: 0Last Post: 04132010, 09:58 PM
Bookmarks