I am performing ray triangle intersection by transforming the ray into the object space for efficiency. I am using the same technique from this question (The correct way to transform a ray with a matrix?).
My question has to do with the numeric stability of the transformed ray. This is my code
Matrix4f matModel= this->getWorldTransform();
Matrix4f matModelInverted = matModel.Inverted();
Matrix4f matInverseNormal = matModel.Transposed();
rayOrigin = matModelInverted.Transform(rayOrigin);
rayDirection = matInverseNormal.Transform(rayDirection).Normalized();
matModel
is the Object-to-World transform and it is a very simple translation matrix with this value
| 1 0 0 -4 |
| 0 1 0 -15|
| 0 0 1 -13|
| 0 0 0 1 |
matInverseNormal
is the transposed inverse of the matrix used to transform the ray origin and has the value
| 1 0 0 0|
| 0 1 0 0|
| 0 0 1 0|
|-4 -15 -13 1|
My code works as it should most of the time, the problem appears around certain value where the transformed ray direction behaves erratically and the ray flip flops by as much as 180 degrees for very small changes in the input.
For input ray direction {-0.59531, 0.64250, -0.48249}
I get a transformed direction {-0.59531, 0.64250, -0.48249}
which is visually the correct behavior. Perturbing the values of the input ray direction to {-0.59520 0.64320 -0.48169}
(a change of about 10e-3 magnitude) gets me {0.59520, -0.64320, 0.48169}
, which has flipped signs on all axes and is visually the incorrect behavior.
What am I doing wrong?