- Home /
How does Vector3.Reflect get calculated
Hey Guys, I was wondering how Vector3.Reflect actually gets calculated. I know what it does and which parameters are needed, but I'd like to know how the calculation actually works, if I was to calculate the Vector myself.
Answer by Bunny83 · Dec 03, 2018 at 11:23 PM
You can see here what it does.
public static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal)
{
return -2F * Dot(inNormal, inDirection) * inNormal + inDirection;
}
This part:
Dot(inNormal, inDirection) * inNormal
essentially is the same as Vector3.Project. So it projects inDirection onto the normal. By subracting that result twice from the original direction vector we get the reflected direction.
Just to clarify i've attached this image. The red vectors are the in and out direction vectors. The gray vector is the surface normal that is used to reflect the direction. The blue vector is the projected "in" direction onto the normal vector. The top horizontal line is twice that projected vector. As you can see when you subtract that from the original vector (yellow) you get the proper reflected vector.
Your answer
