Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on collision detection for a pong clone. I've calculated the angle of incidence but I can't find any information on how to specular reflect the angle I found.

The code for calculating the angle of incidence is below

 if (ballPosition.Y >= height - 6)
    Vector2 bottomNormal = new Vector2(400, 460) - new Vector2(400, 480);
    Vector2 ballNormal = ballPosition - new Vector2(400, 480);

    float angle = Vector2.Dot(bottomNormal, ballNormal);
share|improve this question

marked as duplicate by Anko, jhocking, Trevor Powell, Josh Petrie Jan 6 '15 at 18:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 1 down vote accepted

Well done knowing the terminology to describe your problem. The specular reflection angle can be found with the following formula (in pseudo code):

reflectionAngle = 2*(dot(normalVec,incidenceVec))*(normalVec-incidenceVec);

Essentially you want to find the vector that is the same degrees of rotation from the bottomNormal as your angle of incidence. In the image below, the angle of incidence you're finding would be θi and the normal is your bottomNormal, making P your angle of incidence and Q the vector you want to find. Note that θi and θr are equal.

wikipedia image showing specular reflection

Source: Wikipedia

share|improve this answer

For the specific instance of a Pong clone you can cheat with this problem and flip the sign of your velocity vector component in the axis normal to the collision surface. Since in pong all surfaces have normals that match the coordinate system you can just flip the sign for the component on a per surface basis.

In code:

if (x_ball_position >= x_upper_limit || x_ball_position <= x_lower_limit)
    x_velocity = -x_velocity;
if (y_ball_position >= y_upper_limit || y_ball_position <= y_lower_limit)
    y_velocity = -y_velocity;
share|improve this answer
kinda a ugly hack, and your relative angle will never actually change, just the direction. – Gustavo Maciel Apr 27 '12 at 12:13
Hack or not, this is probably how Pong did it, and it's a far simpler approach than using trig. I really doubt those early machines could do the trig. – Tim Holt Apr 27 '12 at 15:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.