Reflection off both sides of a line

Started by
0 comments, last by Zakwayda 12 years, 10 months ago
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?
Advertisement
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 v with respect to a unit-length vector n, you can use the following formula (typed into post, so no guarantee of correctness):

v' = v - 2(v.n)n

This topic is closed to new replies.

Advertisement