/* * Test for a Collision * Question is Where and how to put this kind of code segment * into your java applet. */ //build up a Bounding box around both the paddle and the ball // the size of the bounding box for the ball ball_top = y_pos - radius; ball_bottom = y_pos + radius; ball_left = x_pos - radius; ball_right = x_pos + radius; //sizes of the bounding box for the left paddle paddleL_top = mypaddle.ypos; paddleL_bottom = mypaddle.ypos + mypaddle.ysize; paddleL_left = mypaddle.xpos + mypaddle.xsize; //sizes of the bounding box for the right paddle paddleR_top = mypaddle2.ypos; paddleR_bottom = mypaddle2.ypos + mypaddle2.ysize; paddleR_right = mypaddle2.xpos; //First test to see if the ball is in the y range if ((ball_top >= paddleL_top - radius) && (ball_bottom <= paddleL_bottom + radius)) { /* *Second test if the ball is within the x range. *if both are in range we have a collision */ if ((ball_left <= paddleL_left)) { xspeed = 8; yspeed= 10; } } //use opposite test for right paddle if ((ball_top >= paddleR_top - radius) && (ball_bottom <= paddleR_bottom + radius)) { if (ball_right >= paddleR_right) { xspeed = -8; yspeed =-10; } } // This is down and dirty code sorry for the hard coded numbers //this code moves the ball if (x_pos>=500){ xspeed =-8; } if (y_pos<=10){ yspeed=3; } if (x_pos<=0){ xspeed=8; } if (y_pos>=500){ yspeed=-3; } x_pos =x_pos + xspeed; y_pos +=yspeed; //vsg for debugging this code moves the paddles independently mypaddle.ypos= mypaddle.ypos - ps; mypaddle2.ypos= mypaddle2.ypos + ps2; if (mypaddle.ypos <= 0){ ps = -6; } if (mypaddle.ypos >= 400){ ps = +6; } if (mypaddle2.ypos <= -75){ ps2 = 6; } if (mypaddle2.ypos >= 400){ ps2 = -6; } }