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 testing some vector reflection and I want to check what happens when a ball collides with a paddle.

So if I have:

Vector2 velocity = new Vector2(-5, 2); 
position_ball += velocity;

if (position_ball.X < 10) 
{
    Vector2 v = new Vector2(1,0); // or Vector2.UnitX   
    velocity = Vector2.Reflect(velocity, v); 
}

then, correctly, velocity is (5,2) after Reflect, but if I do:

if (position_ball.X < 10)
{
  Vector2 v = new Vector2(1,1); 
  velocity = Vector2.Reflect(velocity, v);
}

then velocity is (1,8) and not (5, -2) that is the solution of reflection equation R = V - 2 * (V . N)

Why is that?

share|improve this question
1  
You've stepped through this with the debugger? I imagine your input values are not what you think they are. –  Byte56 Jun 18 '12 at 15:23

1 Answer 1

up vote 6 down vote accepted

The reflection equation is R = V - 2 * (V . N) * N This formula also assumes that N is a normal vector, which it isn't in your case new Vector2(1,1).Length() == 1.414...

Try this instead:

if (position_ball.X < 10)
{
  Vector2 v = new Vector2(1,1); 
  v.Normalize();
  velocity = Vector2.Reflect(velocity, v);
}

The reflection of [-5,2] will actually be [-2,5] (or [1,8] if not normalized) and not [5,-2] as you assumed:

R = V - 2 * (V.N) * N
N = [1,1] * 2^-0.5
V = [-5,2]

Since Nx = Ny in this particular case we can refer to either of them as Nc

R = V - 2 * (v.N) * N
  = V - 2 * (Vx*Nc + Vy*Nc) * N
  = V - 2*Nc(Vx+Vy) * N
  = V - 2*Nc(-5+2) * N
  = V - 2*Nc(-3) * N
  = V + 6*Nc * N
  = V + [6*Nx*Nx, 6*Ny*Ny]
  = V + [6*Nx^2, 6*Ny^2]
  = [6*Nx^2-5, 6*Ny^2+2]
  = [6*(2^-1)-5, 6*(2^-1)+2]
  = [-2, 5]

(N^a)^b = N^(a*b) which is used in the above proof (2^-0.5)^2 = 2^-1

share|improve this answer
    
Yes. Thanks for the answer. If you have some time for me :) I still have a big doubt: if I normalize velocity Vector2.Reflect = (velocity, v) v is equal to 0.71 o.71 1.1 or always 1,1? and then if I want to reflect (-5, +2) into (5, -2) which equation should I use? –  xdevel2000 Jun 19 '12 at 7:45
    
Normalize divides the vector with the length of the vector: [1,1] / Sqrt(1^2 + 1^2) = [0.707..., 0.707...] The formula is as I mentioned in the answer, but you can simply use Vector2.Reflect instead which uses that exact formula –  Daniel Carlsson Jun 19 '12 at 19:48

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.