Tuesday, 10 May 2016

Reflecting a Vector

As you can probably guess, I went with learning about reflecting a vector as it hits a point on a plane.
I like to visualize it as shining a laser pen onto a mirror. cool eh?

There's tons of examples around so I worked my way through them and tried building something similar using expressions or vanilla Maya nodes. When I got something working I built a custom node.
The blue locator represents the source vector A, the plane has the normal n and the point P. The resulting reflected vector B, is the red locator.


There's a few calculations we can skip if the plane is static, but it'd be kinda cool to move the plane around and see what the reflection would be. Here's how I went about it:

Get the vector A.

A = P1 - P0


Reverse the Vector A by multiplying it by -1 and add it to P0.

B = ( -1 * A ) + P0


Project A onto the normal n, using the dot product. There's plenty of resources online if you're not sure what the dot product does.

C = ( A • n ) * n


Multiply C by 2 and add to the vector B.

D = ( 2 * C ) + B


We can find the vector E by subtracting the end point of vector D from P0.


Final reflected vector.

Here's a quick demo. :D

Scene file setup with standard math nodes.
Plugin (Python)

If you check out the python script, you'll notice I'm not using the plane normal. I could have probably used a mesh function set to get a face or vertex normal but I cheated a bit and used the Y basis vector from the worldMatrix of the plane transform.

What happens if the plane has multiple faces, which normal do we choose?

No comments:

Post a Comment