Saturday, August 1, 2015

xna - Reflect angle on pong clone



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

Answer



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



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...