   Need help with Breakout collision detection
  Hello! I need some help to make a algorithm that can detect if the ball in my breakout game hits a brick from either the sides, or from above or below. I need to know this in order to know how to bounce the ball.My current sulution only checks if the ball is inside a brick, but not from where it came.I cant however figure out how to do this, so any help is greatly appreciated. This is my current collision detecthin function: void CBrick::CollisionDetection( CBall &Ball, int &score ){hit = false;for( int i = 0 ; i < NUMBER_OF_BRICKS ; i++ ){if( BrickStruct[i].lives > 0 ){if( ( Ball.posX + Ball.rectBall.right ) >= BrickStruct[i].posX && Ball.posX  <= ( BrickStruct[i].posX + rectBrick.right ) && ( Ball.posY + Ball.rectBall.bottom ) >= BrickStruct[i].posY && Ball.posY <= ( BrickStruct[i].posY + rectBrick.bottom ) ){BrickStruct[i].lives--;score++;hit = true ;}}}if( hit )Ball.moveRateY = -Ball.moveRateY;}  Ball.rectBall is the rectangl for the ball.rectBrick is the brick rectangle.BrickStruct is a struct containgig each brick''s position and life.As it is now, the ball only bounces correct when it hits a brick from above or below: Ball.moveRateY = -Ball.moveRateY;Thats why i need to know if it hits from the sides so i can make it bounce correct there too.Thanks in advance! Sorry for my poor english.  
  Just check it using the bounding box and see through which plane it entered.//BOX(X0,Y0,X1,Y2)BOX(50,50,100,100)Collision truth tablenorth:X0 - X1 at Y0south:X0 - X1 at Y1west:Y0 - Y1 at Xeast:Y0 - Y1 at X1;  Maybe you should try to detect the ball hitting the brick before it get''s inside. Say like:if (ball.position == brick.left.border) ...if (ball.position == brick.right.border)If that''s too difficult to work out... you should be able to get the direction the ball is currently moving, right?So once the ball is inside the brick, get it''s current direction, and the wall it''s closest to. If it''s a wall in y direction invert y-speed; x-direction -> x-speed.  ;  Getting a perfect system working is not easy (I mean if the ball can have any velocity or angle). The ball can cut across the corner of one block and skip right into another so one moment it''s in space, then it''s in a block but it has already jumped over the a bit of corner of a third block. You can check for this using polygion intersection or something complicated like that but for a simple system you can:1. Store the old ball position.2. If now inside a block check if the old ball pos was directly left (ie within the confines of the block vertically but now it''s inside a block). If so the ball hit the left side so reflect (xvel=-xvel).3. Repeat step 2 for each direction.4. Now if the above tests failed then it came in from a corner and you for each possible corner you have to work out which side of the brick was hit and reflect as needed. You might have to calculate the slope of the difference for this.Mark  ;  here''s what i would do... i would take the current ball position, and the time since the last frame, and calcualte where the ball would WANT to be by the elapsed time if it didn''t hit anything... then i would see if ANYTHING falls in that path. (ex, a block); this can be done using the properties of the ball (ie, the radius) and the properties of any objects that it might hit (ex, the brick size) simply by checking if any of the edges would go past each other -- one way to do this is to find some eq for the "first" edge of the collidable and see if your dest is past it... if not, don''t worry about it... if so, look more closely... and so on... i''m sure you can figure out wether you''re past an obj or not... and wether your objs overlap at any point... it''s a backtracking process, with each backtracking step giving you more info to isolate the collision. once you have the point of passing (intersection of collidable edge and ball dest vect) you can see if your ball is so big (r) that is moves into the box space at that edge.. THEN, if you have a collision, find the time along the dest vect it would occur at... take that off the frame time, move the ball there, and change the ball due to the collision (bounce, etc)... keep going untill you''re out of time to trace on... render then frame... 