How to do ray plane intersection?

后端 未结 4 460
旧巷少年郎
旧巷少年郎 2020-12-08 21:26

How do I calculate the intersection between a ray and a plane?

Code

This produces the wrong results.

float denom = normal.dot(ray.direction         


        
4条回答
  •  暖寄归人
    2020-12-08 22:02

    As wonce commented, you want to also allow the denominator to be negative, otherwise you will miss intersections with the front face of your plane. However, you still want a test to avoid a division by zero, which would indicate the ray being parallel to the plane. You also have a superfluous negation in your computation of t. Overall, it should look like this:

    float denom = normal.dot(ray.direction);
    if (abs(denom) > 0.0001f) // your favorite epsilon
    {
        float t = (center - ray.origin).dot(normal) / denom;
        if (t >= 0) return true; // you might want to allow an epsilon here too
    }
    return false;
    

提交回复
热议问题