2
$\begingroup$

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?

$\endgroup$
6
  • $\begingroup$ How do you change the location of the triangles then? $\endgroup$
    – lightxbulb
    Commented Jun 29, 2020 at 18:12
  • $\begingroup$ @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. $\endgroup$
    – cemklkn
    Commented Jun 29, 2020 at 18:18
  • 1
    $\begingroup$ 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. $\endgroup$
    – lightxbulb
    Commented Jun 29, 2020 at 19:03
  • 1
    $\begingroup$ 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. $\endgroup$
    – lightxbulb
    Commented Jun 30, 2020 at 5:08
  • 1
    $\begingroup$ 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. $\endgroup$
    – cemklkn
    Commented Jun 30, 2020 at 15:26

1 Answer 1

3
$\begingroup$

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$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.