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.

v

_{2}= v_{1}– 2(v_{1}.n)n

**v _{1}.n** is dot product &

**n**must be normalized.

1 2 3 4 |
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 **v _{1}** vector we want to get its reflection. Vector

**n**is the vector that perpendicular to the surface

**v**reflects with. We usually call it

_{1}**normal**.

Draw straight line

We get vector n_{1}. 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 **n _{1}**.

v

_{2}= v_{1}+ 2n_{1}

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 v_{1} and n is more than 90°, the result is minus. Multiply **n** by dot product’s result, we have **n2**.

Finally,

v

_{2}= v_{1}+ 2n_{1}v

_{2}= v_{1}– 2n_{2}v

_{2}= v_{1}– 2(v_{1}.n)n