[Math] Reflecting ray on triangle in 3D space

3dgeometrytriangles

I am working in a 3D space. I have a ray, or more specifically the start and transmission vector of a ray in 3D space. I also have a triangle, which I know for a fact is hit by the ray. By triangle I of course mean I have three 3D points. I can also calculate a normal vector from this triangle easily.

Now I need to get a new ray, which would be the reflection after that specific hit.

I also don't know exacltly where on the triangle the point of contact is, but it obviously has to be taken into account to get the reflected ray. That point would be the start of that new ray.

This is trivial to do in 2D, but I can't figure out how to do it in 3D.

Best Answer

enter image description here

By $\,\, \times \,\, $ I denote cross-product and by $(\,\, \cdot \,\, )$ I denote dot product. The length of a vector is $|\vec{u}| = \sqrt{(\vec{u} \cdot \vec{u})}$.

Look at the picture. $S$ is the source of the light ray and the direction of the ray is denoted by $\vec{v}$. The three points are $A, \, B, \, C$. Say $O$ is the origin of your coordinate system. Now, the unit normal vector to the plane determined by points $A, \, B, \, C$ can be calculated for example as $$\vec{n} = \frac{\vec{AB} \times \vec{AC}}{\big|\vec{AB} \times \vec{AC}\big|}$$ Vector $\vec{OK}$ in the right side of the picture is the orthogonal projection of vector $\vec{v} = \vec{OM}$ onto $\vec{n}$ and is thus defined as $$\vec{OK} = (\vec{v} \cdot \vec{n})\, \vec{n}$$ so twice the vector $\vec{OK}$ is $$\vec{OL} = 2 \,\vec{OK} = 2\, (\vec{v} \cdot \vec{n}) \, \vec{n}$$ Thus, the reflected vector is $$\vec{w} = \vec{LM} = \vec{OM} - \vec{OL} = \vec{v} - 2\, (\vec{v} \cdot \vec{n}) \, \vec{n}$$

Now the intersection point $R$ between the light ray and the plane $A, B, C$. The vector equation for the plane $A, B, C$ can be written as $$\vec{n} \cdot \big(\vec{OX} - \vec{OA}\big) = 0$$ where $X$ is any point on the plane and $\vec{OX}$ is its position (coordinate) vector, i.e. the vector pointing from the origin $O$ to the point $X$. The equation for the light ray is $$\vec{OX} = \vec{OS} + t \, \vec{v}$$ for any point $X$ on the light ray ($t\geq 0$ in order to have a ray). Thus the intersection point $R$ should satisfy both equations, i.e. \begin{align} &\vec{n} \cdot \big(\vec{OR} - \vec{OA}\big) = 0\\ &\vec{OR} = \vec{OS} + t \, \vec{v} \end{align} so substitute the second equation in the first and \begin{align} 0 &= \vec{n} \cdot \big(\vec{OS} + t \, \vec{v} - \vec{OA}\big) = \vec{n} \cdot \big(\vec{OS} - \vec{OA} + t \, \vec{v} \big) = \vec{n} \cdot \big(\vec{AS} + t \, \vec{v} \big) = \big(\vec{n} \cdot \vec{AS}\big) + t \big(\vec{n} \cdot \vec{v} \big) \end{align} and when you solve for $t$ you get $$t = \frac{- \big(\vec{n} \cdot \vec{AS}\big)}{\big(\vec{n} \cdot \vec{v} \big)} = \frac{\big(\vec{n} \cdot \vec{SA}\big)}{\big(\vec{n} \cdot \vec{v} \big)}$$ notice $\vec{SA} = - \vec{AS}$. Therefore, $$\vec{OR} = \vec{OS} + \frac{\big(\vec{n} \cdot \vec{SA}\big)}{\big(\vec{n} \cdot \vec{v} \big)} \, \vec{v}$$

Thus, the reflected ray is given by the following point $R$ and vector $\vec{w}$ \begin{align} \vec{OR} &= \vec{OS} + \frac{\big(\vec{n} \cdot \vec{SA}\big)}{\big(\vec{n} \cdot \vec{v} \big)} \, \vec{v}\\ \vec{w} &= \vec{v} - 2\, (\vec{v} \cdot \vec{n}) \, \vec{n}\\ \vec{n} &= \frac{\vec{AB} \times \vec{AC}}{\big|\vec{AB} \times \vec{AC}\big|} \end{align} You can permute the points $A, B, C$ any way you want, you will get the same result.