   Collisions in Breakout
  OK, so I''m making a breakout clone and so far, I''m using simple bounding rectangles to do collision. If the ball''s top-left (for instance) coordinate is between the left and right, and bottom and top coordinates of the block, then a collision is made. My problem is how to do the proper reflection. How would I write the code to detect if the ball collides with the side of the block (thus reversing its x velocity) or if it collides with the top/bottom (and reversing its y velocity)? 
  << My problem is how to do the proper reflection. >>Good question, actually this is asked about once a week (either pong or breakout is popular). The simplest non-Trig solution is to do a series of IF statements, breaking up your paddle into groups of pixels. So -- assuming a vertical breakout game, with your paddle at the bottom of the screen -- if the ball hits to the extreme   far left of the paddle, you make xmove = -9, then -7, -5, -3, etc, center is xmove = 0, extreme   far right +9, etc. I believe my xball and yball is the center of the ball, xmove and ymove are the movement on x and y (or "vectors" to use the proper term). In mine, xpaddle is the leftmost point of the paddle.My breakout deflection series of IFs look like this if (xball < xpaddle + 6) xmove = -9;if ((xball > xpaddle + 5)  && (xball < xpaddle + 11)) xmove = -7;if ((xball > xpaddle + 10) && (xball < xpaddle + 16)) xmove = -5;if ((xball > xpaddle + 15) && (xball < xpaddle + 21)) xmove = -3;if ((xball > xpaddle + 20) && (xball < xpaddle + 26)) xmove = -2;if ((xball > xpaddle + 25) && (xball < xpaddle + 36)) xmove = 0;if ((xball > xpaddle + 35) && (xball < xpaddle + 41)) xmove = 2;if ((xball > xpaddle + 40) && (xball < xpaddle + 46)) xmove = 3;if ((xball > xpaddle + 45) && (xball < xpaddle + 51)) xmove = 5;if ((xball > xpaddle + 50) && (xball < xpaddle + 56)) xmove = 7;if (xball > xpaddle + 55) xmove = 9;ymove = -ymove; // bounce verticallyMy full source hereVazGames.com  Phil P   [edited by - PhilVaz on February 3, 2004 2:05:35 PM];  That's a lot of IF statements. Why not just set a constant specifying the max speed the ball and then when the ball hits youd do something like:new_speed = (int)(distance_from_middle_of_paddle / paddle_width / 2 * max_speed);   Youd create a percentage of how far it is from the center, by dividing the distance from the middle by the width of half the paddle, and then multiply that by the max speed allowed, and convert it to an int. A bunch of IF statements aren't always nice looking.   As for the collision problem. You should do an extra check in your collision. If the ball collides with a block, you should check to see whether the ball was above/below the block, or whether it was to the left/right of the block. You could then return 0 if it didn't collide, 1 if it collided above/below, or 2 if it collided to the left/right, and use that return value to modify its velocities.   [edited by - porthios on February 3, 2004 2:13:52 PM];  << That''s a lot of IF statements. >>Cool, your solution is shorter. That game was my first Win32 game in C after a 10 year hiatus. I forgot a lot of shortcuts. Phil P ;quote:Original post by porthios  As for the collision problem. You should do an extra check in your collision. If the ball collides with a block, you should check to see whether the ball was above/below the block, or whether it was to the left/right of the block. You could then return 0 if it didn''t collide, 1 if it collided above/below, or 2 if it collided to the left/right, and use that return value to modify its velocities.   [edited by - porthios on February 3, 2004 2:13:52 PM]Thanks for the help guys.The problem with this is that...theoretically, when the ball collides, the point of intersection is equal to or above the block''s horizontal plane AND equal to or to the left of the block''s right vertical plane. Or at least it works that way in my collision detection. ;  Here is the easiest way to do it.Have two vars for your Speed (Which you should have already).double SpeedX = 1;double SpeedY = 1;To move...X+= SpeedX;Y+= SpeedY;All you have to do is check where it hit, and reverse the speeds.if(TopCollision) SpeedY = -SpeedY;if(BottomCollision) SpeedY = abs(SpeedY);//etc...Also, for the Collisions, I would use the Speed vars to calculate where the ball will go, and check the collision with that. So you don''t see the ball going through the objects.