Vector Reflection

visual representation of how to reflect a vector given only a vector and a plane

A super common scenario is needing to reflect (or bounce) something off a surface.

Think bouncing ball, rubber bullet, pong ball...etc

We're going to assume you can't just do something like "hit.normal" as Unreal provides you when firing a raycast so we can see how the underlying math works.

If the engine provides such a "hit.normal" then that simplifies this very much since we know the plane (surface we hit) normal AND the intersection point

Find the plane's normal

We're going to assume we are only given a set of vertices which make up the plane, and a vector that passes through the plane

color of the lines is just to show the different vectors AB, BC, CD, DA
we only care about V as a vector (meaning direction and length), not the line segment (position)

To find the plane's normal simply cross two of the edges (counter-clockwise)

FVector n = AB.Cross(BC).GetSafeNormal(); //normalizing for use later
Normal of the plane defined by AB and BC

We're also going to assume the reflection to be a perfect reflection, meaning there is no friction, momentum is preserved, there's no loss of energy...etc. Meaning if you hit the plane at a 45° angle the reflection will also be a 45° angle.

This just allows us to solve the base case of the problem.

It certainly makes sense to add things like deceleration, or acceleration based off the angle of reflection (like in pong) and/or momentum being lost or gained but in reality you still need to solve the base case for this problem first before skewing the reflection vector to your needs. So here we go.

Find the reflected vector

Right now Vr is just sitting in some mystical location. We don't have a point of intersection (yet) nor do we know the direction it should point.

But what we do know is that by looking at the image below, if we were to translate Vr to a parallel vector from our known point v0 (the ray's starting point) it would intersect our normal at point I

If we can find this point I then we can get the direction vector of our reflection (it will be in the wrong position, but we will deal with that in a second.

Find point I along plane's Normal

First lets project V onto N

V projected onto N will actually be pointing in the wrong direction
so we'll want to subtract it from V
doubling the projection gets us to I

Stellar, now we can find Vr via

I = v - 2(Project V onto N)

FVector I = v - (2 * (v.Dot(n)) * n); // V projected onto normalized N is (V â‹… N) * N
FVector Vr = I - V0;

Find the intersection of V and the Plane

Lastly we need to find where Vr should be reflecting out of which simplifies down to a line plane intersection problem. We need to find where V intersects with the plane.

The intersection point will be some point along V scaled by some value k.

  • k is a value from [0, 1] which represents the fraction of V we need to travel to reach the intersection point.

visual representation of some would be values of k (left) we need about 65% of V to reach the plane. (right) we need about 33%

Here is a great visual representation of the derivation of the below formula's for more details but otherwise the formula is as follows

c = any arbitrary point on the plane
w = c - v0             // construct a vector from the ray's start to the point
k = (w â‹… n) / (v â‹… n)   // Projection of W onto N divided by Projection of V onto N (multiplication by N for the projection formula cancels out in the derivation) 
I = v0 + kv            // intersection point starts at v0, in the direction of v, scaled by k
FVector pointOnPlane = pointB;     // any point on plane, i'm using one of the vertices here
FVector w = pointOnPlane - V0;     // vector from v to point
float k = w.Dot(n) / v.Dot(n);     // scaling factor of v to plane
FVector I = V0 + (k * v);          // point is on the ray starting at v0, along the direction of v, and scaled by k
red dot is the point of intersection of V and the Plane

Putting it all together

Now we know where to reflect from, which is simply I + Vr

vector reflection off a plane

Last updated