# Thread: Ball bouncing in Breakout-like game

1. ## Ball bouncing in Breakout-like game

Hi,

I'm currently developing a game like Breakout in C using allegro.
The player controls a paddle on the X-Axis and has to prevent the ball from hitting the ground.

My problem is that I cannot come up with how to solve the bouncing problem satisfactory. I want the ball to bounce of the paddle at an angle that depends on the distance from the paddle's center.

I know there are hundreds of threads you can find on google, and i looked through them all which took me days, but I could not get it to work.

If you could help me, that would be great !

2. So how are you defining the path the ball takes? Are you storing an angle or are you storing a velocity vector?

3. Arc length and Chord length - Math Central
What you draw on screen for the paddle is the straight line chord ADB
The bouncy surface is the arc AB, centred on C.

Depending on how extreme you want to make the bounce, you make r smaller, or you make r very large if you want the paddle to be essentially flat.

The rest is just some trigonometry to work out the maths.

Once you've done it, 'r' basically becomes a difficulty parameter in your game.

@tabstop
I am storing the angle as xveloity and yvelocity float values.

@salem
"What you draw on screen for the paddle is the straight line chord ADB
The bouncy surface is the arc AB, centred on C."
so far I understand what is going on. But I don't know what does represent the balls position and what exactly do I have to calculate for my xvel and yvel values ?

5. If you don't know the ball's position, how do you draw it on the screen?

If you treat the surface of the paddle as a circle, then the usual bounce formula v_new = v_old-2*proj_normal v_old gives you the new velocity. Again, you'll have to do a bit of work to write down the normal.

Alternatively, you will (had better!) know at all times where the center of the paddle is, so when you find a collision you can easily tell how far away from the center of the paddle you are. The larger that distance, the more the x-velocity will be.

6. "Alternatively, you will (had better!) know at all times where the center of the paddle is, so when you find a collision you can easily tell how far away from the center of the paddle you are. The larger that distance, the more the x-velocity will be."

Thats actually what I am currently doing.
I'm calculating the distance of the balls position from the center and then
b->xvel *= distance_to_pCenter / 30.f;
I create some factor based on this to multiply the ball's velocity with.

I was only wondering if there is a more elegant solution, since I saw formulas with cos, sin, atan2 and crazy thinks like that.
But it seems like this would require a lot of work

7. The only reason it works out this simple is because your paddle is parallel to the x-axis. If your paddle could rotate (or was round as Salem suggested above) then you would have to use the real formula instead.

8. Ok,

Thanks for your help guys !

Popular pages Recent additions