# How to get reflection vector?

This is one of the problems I encounter often. It might be useful to put solution here.
If you are in hurry or don’t need any explanation, I’ll just leave solution below.

v2 = v1 – 2(v1.n)n

v1.n is dot product & n must be normalized.

Vector3 ReflectVector(Vector3 inputVector, Vector3 normal) {
float dn = 2 * inputVector.DotProduct(normal);
return inputVector - normal * dn;
}


That is the code I use for my game using Angelscript (c++ like language). If you use C#/Unity3D, the concept is similar.

How did I come up with these numbers?  Well, I found formula from StackOverflow. haha.  Let’s break down the solution.

We have v1 vector we want to get its reflection. Vector n is the vector that perpendicular to the surface v1 reflects with. We usually call it normal.

Draw straight line

We get vector n1. For now, just ignore n. Multiply n1 by 2 and now that’s v2 we are looking for.

Next step is figuring out how to find n1.

v2 = v1 + 2n1

Easy, it’s dot product of v1 and n. If you don’t know what dot product is, you can read it here.  Due to the angle between v1 and n is more than 90°, the result is minus. Multiply n by dot product’s result, we have n2.

Finally,

v2 = v1 + 2n1

v2 = v1 – 2n2

v2 = v1 – 2(v1.n)n

This site uses Akismet to reduce spam. Learn how your comment data is processed.