Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
 }
share|improve this question
1  
possible duplicate of How to calculate the direction a ball must bounce –  bummzack Apr 27 '12 at 6:51
add comment

2 Answers

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
add comment

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
1  
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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