I am currently interested in billiards and their trajectories. I would like to simulate a billiard inside a water-tight mesh.
A mesh basically consists of a list of points in 3D space (vectors with 3 entries) together with a list of 3 vectors of natural numbers, which specify the corner points of each triangle. Now, I would like to have a reflection whenever the trajectory of the ball crosses the triangle.
I would already be happy about pseudocode
The question is, how can I find if a trajectory crosses a triangle? and how to compute the reflection?
Remark: I would later like to play billiards of charged balls in magnetic fields. I can therefore not assume that the balls follow a straight line.
So what this will come down to is that I will have a differential equation solver integrating the differential equation over time and whenever the one of the triangles is crossed, it should invert the part of the velocity vector that is normal to the triangle.
according to the formula:
$$p \mapsto p - 2 \langle p, n \rangle n,$$
where $n$ is the normal vector of the triangle. I have all the components except for the mechanism that tells me, when a triangle is crossed. I would have an idea to how to see whether the trajectory crosses the plane spanned be the corner points of the triangle, but I already don't how to figure out whether the crossing happened inside the triangle or outside the triangle in the plane.