# Reflection off both sides of a line

## Recommended Posts

Hi everyone,

I'm working on a breakout style game, and I've gotten myself stumped by a math problem that's likely trivial for the folks around here.

In the game, my ball's travel is defined by an angle and velocity. For the sake of clarity, I'm using degrees in my code and converting to radians when necessary. Unlike most breakout games, my paddles can be at any angle. They're defined by x and y points for both ends of the paddle, and I can derive their angle just fine, but they can rotate a full 360 degrees as the points at the ends can move independently.

I've been calculating the ball's angle of reflection off the paddle by using the following:

ball angle = (paddle angle * 2) - ball angle

This works just fine if the ball approaches the paddle from the correct side, but my attempts to detect the side and rotate the paddle angle 180 degrees when necessary have not proven fruitful.
I've been detecting the side by checking the sign of the following equation:

orientation = (paddle..x2 - paddle.x1) * ((ball.y + 2) - paddle.y1) - (paddle.y2 - paddle.y1) * ((ball.x + 2) - paddle.x1)

Could any of you help me work out an equation that will generate an accurate angle of reflection off the paddle at any rotation?

##### Share on other sites
Typically this would all be done using vector math rather than trig and angles.

As for your specific question, it sounds like it's vector reflection that you're looking for. To reflect a vector [i]v[/i] with respect to a unit-length vector [i]n[/i], you can use the following formula (typed into post, so no guarantee of correctness):

[code]v' = v - 2(v.n)n[/code]

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628365
• Total Posts
2982272

• 10
• 9
• 13
• 24
• 11