hi there iam looking for an math function to calculate the moving direction after the ball hit an wall or so can you help me? i dont find anything usefull with google Thanks sorry if its a silly question, the wrong place and for my bad english
I've never done a breakout clone myself, but I'd say the easiest way to do this is to simply reverse the x and y direction the ball is travelling. This does not take into account gravity or anything, but I don't think any breakout clone does. I think the only thing still to do (optional) is to see if it hit the paddle, and increase the x speed slightly in the direction the paddle was moving, if the paddle was moving faster than a certian threshhold. Edit: I've created a small prog last night when demonstrating something to someone - it's simply an object that bounces around on the screen - I think it's "bouncing" routine is exactly what you are looking for (excluding the paddle). I'm at work at the moment, so I can't post it now (the code is at home), but I can post it tonight if you are interested. It is very basic, however. I doubt anybody will find anything useful in it. I had to demonstrate something to a person that was completely code-illiterate.
Here is some psuedo code: Code: if(ball_hits_left_wall()) Ball_x_velocity = - Ball_x_velocity; if(ball_hits_right_wall()) Ball_x_velocity = - Ball_x_velocity; if(ball_hits_top_wall()) Ball_y_velocity = - Ball_y_velocity; You have to do a similar thing when ball hits bricks, but there it is slightly more complicated to determine which face of the brick was hit first by the ball. Btw, this question is probably more appropriate in Game Development & Technical Pallav
The algorithm is very simple. There are two cases actually: 1. If the ball hits the brick at the left or at the right side, reverse it's velocity vector's x component. 2. If the ball hits the brick at the top or at the bottom side, reverse it's velocity vector's y component. 3. Move the ball to the contact point between the brick and the ball (see below). Knowing which side the ball hits can be done like this (assuming that you know that the ball has hit a brick): 1. Split the brick in four line segments, one for each side as shown in brick1.png 2. Check if the velocity vector intersects any of these line segments - it may intersect one or two. Also find the intersection points of these line segments (the red dots in the images). 3. If it intersects one, then you know which side the ball hitted, so use this 4. If it intersects two, as shown in images brick2.png to brick4.png, then check which of the intersection points is the nearest to the vector's base (the previous position of the ball). The side that belongs to the nearest point is the side that the ball hitted. Images 2 to 4 shows different cases: * In brick2.png the side is left * In brick3.png the side is right * In brick4.png the side is bottom To find the contact point, after swapping one of the velocity vector's components, make a new vector from it and normalize it. Then multiply it by the ball's radius. Lastly move the ball to the intersection point of the side you hitted and move it along that new vector. In other "words": Code: ball.position = intesectionPoint + normalized(newVelocity) I hope this helped you