Hi,

I'm currently creating a pool game and have the bulk of the code written and working. However I am having trouble with determining the direction of a ball once a collision occurs. At present when the cue ball collides with a ball they both initially move in the correct direction however they then repeatedly alternate between rebounding of the cushion then off each other while all the time travelling along the same path till they both reach the corner pocket at which point they disappear behind the pocket, (I understand that they should disappear into the pockets and I have this code written and working but have commented it out until I have the collision working properly. When I give the dx and dy variables a set value then the collisions work fine so the problem obviously lies in something I have done (or not as the case may be). What should happen is that when the cue ball strikes the black ball, the black ball is at rest and the cue ball is striking it "dead-on" at a 90 degree angle relative to the bottom of the table (which is my zero degree reference). Therefore the black ball should be moving along this line of impact, away from the cue ball - the cue ball itself should also be moving in the same direction in this case, along the line of impact (ignoring reverse spin etc. here for simplicity).

I just need this working and I will have my game all but finished. I know that my checkForCollision() method needs a little tidying up and the code for my pockets also but that is next after I have completed this. I have attached the checkForCollision() method and my performAction() method here. If you would like me to place the code for my three classes here please let me know.

Thanks in advance.


 
 /**checkForCollision method which passes in references to the four ball objects and adds
	 * which sets up integers for each ball
	 */
	private void checkForCollision(Ball b[]) {
 
		//integer which passes in the object b[] and returns the x value of b[0] in the Ball class.
		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();
 
		//integer called a which is equal to the x position of ball two minus the x position of ball one
		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;
 
 
 
		//integer collide which is equal to the radius of ball four plus the radius of ball one.
		double collide = radiusb4 + radiusb;
 
		double collide1 = radiusb5 + radiusb;
 
		double collide2 = radiusb5 + radiusb4;
 
		double collide3 = radiusb6 + radiusb;
 
		double collide4 = radiusb6 + radiusb4;
 
		double collide5 = radiusb6 + radiusb5;
 
 
 
		//distance between object centres using the square root method from the Math class.
		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]);
 
			//performMovement(b[0], b[5]);
 
		}
 
		else if(c1 < collide1) {
 
			performAction(b[0],b[4]);
 
			//performMovement(b[0], b[4]);
 
		}
 
		else if(c2 < collide2) {
 
			performAction(b[4], b[5]);
 
			//performMovement(b[4], b[5]);
 
		}
 
		else if(c3 < collide3) {
 
			performAction(b[6], b[0]);
 
			//performMovement(b[6], b[0]);
 
		}
 
		else if(c4 < collide4) {
 
			performAction(b[4], b[6]);
 
			//performMovement(b[4], b[6]);
 
		}
 
		else if(c5 < collide5) {
 
			performAction(b[6], b[5]);
 
			//performMovement(b[6], b[5]);
 
		}
 
	}
 
	//performAction method which passes in the two objects from the Ball class and is called in the if statement.
	private void performAction(Ball b, Ball b2) {
 
		double newDY = b.getDeltaY() * - 1;
 
		b.setDeltaY(newDY);
 
		b.setDeltaY(-5);
 
		double newDX = b.getDeltaX() * - 1;
 
		b.setDeltaX(newDX);
 
		b.setDeltaX(-5);
 
		double newDY2 = b2.getDeltaY() * - 1;
 
		b2.setDeltaY(newDY2);
 
		b2.setDeltaY(-15.5);
 
		double newDX2 = b2.getDeltaX() * - 1;
 
		b2.setDeltaX(newDX2);
 
		b2.setDeltaX(-5);
 
		}