0
\$\begingroup\$

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?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You've stepped through this with the debugger? I imagine your input values are not what you think they are. \$\endgroup\$ Commented Jun 18, 2012 at 15:23

1 Answer 1

6
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Jun 19, 2012 at 7:45
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Jun 19, 2012 at 19:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.