# Dynamic Ray-Triangle Intersection

Computer Graphics Asked by cemklkn on August 27, 2021

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?

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

Correct answer by lightxbulb on August 27, 2021

## Related Questions

### Why does opengl use 4d matrices for everything?

3  Asked on August 27, 2021 by yoris

### How can i wrap the earth image around a 3D Sphere using OpenGL, GLFW, GLAD, GLM?

2  Asked on August 27, 2021 by b_cass_

### Defining “inside” and “outside” of a 3D space

2  Asked on August 27, 2021 by avatrin

### Using Bresenham’s circle algorithm (or another alternative algorithm) to draw an arc

1  Asked on August 27, 2021 by gusman

### Performance of Compute Shaders vs. Fragment Shaders for Deferred Rendering

1  Asked on August 27, 2021 by imallett

### Which perspective projection matrix to use

1  Asked on August 27, 2021 by nixcc

### Perspective correct interpolation z-buffer

1  Asked on August 27, 2021 by lightxbulb

### How is orthographic projection used in computer graphics technically classified as a projection?

1  Asked on August 27, 2021

### Why negate z when constructing projection matrix OpenGL

2  Asked on August 27, 2021

### UV partial derivatives of a cylinder shape primitive

1  Asked on August 27, 2021 by wdc

### OpenGL Framebuffer with multiple Depthbuffers inside

1  Asked on August 27, 2021

### Efficiently generating mesh for self-generated voxel grid

1  Asked on August 27, 2021 by andygeers

### How do you apply a normal map to a 3d mesh?

1  Asked on August 27, 2021 by calvin-godfrey

### What’s a good research topic in Computer Graphics?

0  Asked on August 27, 2021

### When unsetting a VAO, should you also unbind the associated VBOs?

1  Asked on August 27, 2021 by natew

### Reading from buffer versus calculating on the fly performance

1  Asked on August 27, 2021 by wduk

### How are textures projected onto 3d models in texture painting applications

1  Asked on August 27, 2021 by lenny-white

### Conditionals and branching in shaders

0  Asked on August 27, 2021

### Does atomic functions on same memory location cause an performance issue?

1  Asked on August 27, 2021

### Phong model: why no multiplication by N dot L in specular term?

1  Asked on August 27, 2021 by kuba

### Ask a Question

Get help from others!