Calculate bounce angle? [Breakout clone]

Hi,

When the ball hits the paddle in a Breakout clone how do I calculate the bounce angle?

We have a pad, say it’s 180 wide, and when the ball bounces on the left side ( less than 90 degrees) it should bounce left but with a certain angle and if it hits the right side (more than 90 degrees) it should go right with a certain angle.

The program is built so that I have a velocity for both the X and Y axis so how do I calculate the new X and Y values when the ball hits a specific point on the paddle?

If it hits pixel 45 it should get a new X value, -1, and a new Y value, 1. but how do I calculate both the new X and new Y value? 

Thanks in advance for helping me with this, reference to other material is very appreciated aswell!

Best regards,

Tomas

This page describes some of what I’m looking for:

http://gamedev.stackexchange.com/questions/4253/how-do-you-calculate-where-a-ball-should-go-when-it-bounces-off-the-bar

However I don’t get the result I want.

My “bounce” code is like this now:

            --[[ 

                Take the middle Y value of the paddle, and subtract the Y intersection of the ball. 

                If the paddle is 10 pixels high, this number will be between -5 and 5. I call this 

                the “relative intersect” because it is in “paddle space” now, the ball’s intersection 

                relative to the middle of the paddle.

                http://gamedev.stackexchange.com/questions/4253/how-do-you-calculate-where-a-ball-should-go-when-it-bounces-off-the-bar

            ]]–

            local relativeIntersectX = (paddle.x + (paddle.width/2)) - (ball.x + (ball.width / 2));

            local normalizedRelativeIntersectionY = (relativeIntersectX/(paddle.width/2));

            local bounceAngle = normalizedRelativeIntersectionY * MAX_BOUNCE_ANGLE;

            vx = BASESPEED * math.cos(bounceAngle)

            vy = BASESPEED * -math.sin(bounceAngle)

Now it always starts with an -y value, so it always want’s to go down instead of bounce.

When I set it to a positive value it still doesn’t acts like I want.

Also, this variable is set like this:

local MAX_BOUNCE_ANGLE = 5 * (math.pi/math.rad(12))

Can anyone see where the calculation goes wrong?

Best regards,

Tomas

This page describes some of what I’m looking for:

http://gamedev.stackexchange.com/questions/4253/how-do-you-calculate-where-a-ball-should-go-when-it-bounces-off-the-bar

However I don’t get the result I want.

My “bounce” code is like this now:

            --[[ 

                Take the middle Y value of the paddle, and subtract the Y intersection of the ball. 

                If the paddle is 10 pixels high, this number will be between -5 and 5. I call this 

                the “relative intersect” because it is in “paddle space” now, the ball’s intersection 

                relative to the middle of the paddle.

                http://gamedev.stackexchange.com/questions/4253/how-do-you-calculate-where-a-ball-should-go-when-it-bounces-off-the-bar

            ]]–

            local relativeIntersectX = (paddle.x + (paddle.width/2)) - (ball.x + (ball.width / 2));

            local normalizedRelativeIntersectionY = (relativeIntersectX/(paddle.width/2));

            local bounceAngle = normalizedRelativeIntersectionY * MAX_BOUNCE_ANGLE;

            vx = BASESPEED * math.cos(bounceAngle)

            vy = BASESPEED * -math.sin(bounceAngle)

Now it always starts with an -y value, so it always want’s to go down instead of bounce.

When I set it to a positive value it still doesn’t acts like I want.

Also, this variable is set like this:

local MAX_BOUNCE_ANGLE = 5 * (math.pi/math.rad(12))

Can anyone see where the calculation goes wrong?

Best regards,

Tomas