<p>There are 2 ways to go about intersecting the triangle. Let the vertices of the triangle have positions <span class="math-container">$v_1, v_2, v_3$</span>. Let the ray have origin <span class="math-container">$o$</span> and direction <span class="math-container">$d$</span>. Let the model (4x4) matrix be <span class="math-container">$M$</span>.</p>
<p>To find the new vertex coordinates one extends the positions with a 1 (to allow for translations) and multiplies by the model matrix. Let <span class="math-container">$u_i = (v_{i,x}, v_{i,y}, v_{i,z}, 1)$</span> then <span class="math-container">$w_i = Mu_i$</span>. The resulting vertex positions are: <span class="math-container">$v_i' = (w_{i,x}, w_{i,y}, w_{i,z})$</span>.</p>
<p>The other option is to transform the ray with the inverse matrix <span class="math-container">$M^{-1}$</span> and intersect with the non-transformed triangle. To achieve this extend <span class="math-container">$o$</span> with a 4th coord of 1 (to account for translation) and extend <span class="math-container">$d$</span> with a 4th coord of 0 (to ignore translation) then multiply both with <span class="math-container">$M^{-1}$</span>:
<span class="math-container">$$o' = M^{-1}(o_x, o_y, o_z, 1)$$</span>
<span class="math-container">$$d' = M^{-1}(d_x, d_y, d_z, 0)$$</span>
Drop the 4th coordinate of <span class="math-container">$o'$</span> and <span class="math-container">$d'$</span> then intersect with the triangle formed by <span class="math-container">$v_1, v_2, v_3$</span>.</p>

CC BY-SA 4.0
2020-06-30T19:26:45.777Z
2020-07-01T18:56:44.187Z
2020-07-01T18:56:44.187Z
lightxbulb
<p>I am working on a small simulation software which has a 3D Renderer that is using OpenGL. I render objects to the screen as triangle meshes. To be able to select the triangles on the screen, I have implemented the Möller–Trumbore ray-triangle intersection algorithm. The algorithm works fine. However, I have another problem. My triangles are not static in the world. When I change the location of the triangle on the screen, I have to change the input vertices of the triangle in the algorithm but I don't know how to do it mathematically. Is there a way to use this algorithm for dynamic objects?</p>

CC BY-SA 4.0
2020-06-29T18:12:53.493Z
How do you change the location of the triangles then?
lightxbulb
CC BY-SA 4.0
2020-06-29T18:18:33.243Z
@lightxbulb , I use model matrix to change the location of the triangle. The algorithm is using the vertices of this triangle. When I change the location of triangle, the input vertices of the algorithm should also change according to new location of the triangle. I am asking for this change, I am not asking how to change the location of the triangle on the screen. Sorry if it was ambiguous.
cemklkn
CC BY-SA 4.0
2020-06-29T19:03:15.587Z
To change the location of the triangle you multiply the vertices with the model matrix. That's it really - the new location is what you get from the matrix-vector product.
lightxbulb
CC BY-SA 4.0
2020-06-29T21:22:49.563Z
@lightxbulb I don’t understand what you mean. How can I use a matrix as an vector in 3D? Can you be more specific if it is possible?
cemklkn
CC BY-SA 4.0
2020-06-30T05:08:24.217Z
A matrix-vector product yields a vector. If a vertex has position $(x,y,z)$ and your model matrix is M, then make a 4d vector $v=(x,y,z,1)$ and then compute $v' = Mv$. The first 3 components of $v'$ are the coordinates of $v$ transformed with $M$. If you do that for the 3 vertices you can compute the new locations.
lightxbulb
CC BY-SA 4.0
2020-06-30T15:26:59.280Z
Thank you very much! That was exactly answer of my question. Can you please send it as an answer so I select it ? It might help people with similar question.
cemklkn
<opengl><c++><computational-geometry>
Dynamic Ray-Triangle Intersection
cemklkn
# Dynamic Ray-Triangle Intersection

(Asked by: username_0 on 2020-06-29)

I am working on a small simulation software which has a 3D Renderer that is using OpenGL. I render objects to the screen as triangle meshes. To be able to select the triangles on the screen, I have implemented the Möller–Trumbore ray-triangle intersection algorithm. The algorithm works fine. However, I have another problem. My triangles are not static in the world. When I change the location of the triangle on the screen, I have to change the input vertices of the triangle in the algorithm but I don't know how to do it mathematically. Is there a way to use this algorithm for dynamic objects?

**Tags:** opengl, c++, computational-geometry

> **Comments:**
> **username_1**: How do you change the location of the triangles then?  
> **username_0**: @username_1 , I use model matrix to change the location of the triangle. The algorithm is using the vertices of this triangle. When I change the location of triangle, the input vertices of the algorithm should also change according to new location of the triangle. I am asking for this change, I am not asking how to change the location of the triangle on the screen. Sorry if it was ambiguous.  
> **username_1**: To change the location of the triangle you multiply the vertices with the model matrix. That's it really - the new location is what you get from the matrix-vector product.  
> **username_0**: @username_1 I don’t understand what you mean. How can I use a matrix as an vector in 3D? Can you be more specific if it is possible?  
> **username_1**: A matrix-vector product yields a vector. If a vertex has position $(x,y,z)$ and your model matrix is M, then make a 4d vector $v=(x,y,z,1)$ and then compute $v' = Mv$. The first 3 components of $v'$ are the coordinates of $v$ transformed with $M$. If you do that for the 3 vertices you can compute the new locations.  
> **username_0**: Thank you very much! That was exactly answer of my question. Can you please send it as an answer so I select it ? It might help people with similar question.  

---

## 1 answers

### Answer by username_1 on 2020-06-30. Score: 3 | Accepted answer

There are 2 ways to go about intersecting the triangle. Let the vertices of the triangle have positions $v\_1, v\_2, v\_3$. Let the ray have origin $o$ and direction $d$. Let the model (4x4) matrix be $M$.

To find the new vertex coordinates one extends the positions with a 1 (to allow for translations) and multiplies by the model matrix. Let $u\_i = (v\_{i,x}, v\_{i,y}, v\_{i,z}, 1)$ then $w\_i = Mu\_i$. The resulting vertex positions are: $v\_i' = (w\_{i,x}, w\_{i,y}, w\_{i,z})$.

The other option is to transform the ray with the inverse matrix $M^{-1}$ and intersect with the non-transformed triangle. To achieve this extend $o$ with a 4th coord of 1 (to account for translation) and extend $d$ with a 4th coord of 0 (to ignore translation) then multiply both with $M^{-1}$:
$$o' = M^{-1}(o\_x, o\_y, o\_z, 1)$$
$$d' = M^{-1}(d\_x, d\_y, d\_z, 0)$$
Drop the 4th coordinate of $o'$ and $d'$ then intersect with the triangle formed by $v\_1, v\_2, v\_3$.


---

