# Reflect angle on pong clone [duplicate]

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);
ballNormal.Normalize();
bottomNormal.Normalize();

float angle = Vector2.Dot(bottomNormal, ballNormal);
}


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.

Source: Wikipedia

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;
}

• 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