射线平面相交怎么办? [英] How to do ray plane intersection?
问题描述
我如何计算射线与平面之间的交点?
How do I calculate the intersection between a ray and a plane?
这会产生错误的结果。
float denom = normal.dot(ray.direction);
if (denom > 0)
{
float t = -((center - ray.origin).dot(normal)) / denom;
if (t >= 0)
{
rec.tHit = t;
rec.anyHit = true;
computeSurfaceHitFields(ray, rec);
return true;
}
}
参数
ray
表示射线对象。
ray.direction
是方向向量。
ray.origin
是原始向量。
rec
代表结果对象。
rec.tHit
是匹配的值。
rec .anyHit
是布尔值。
Parameters
ray
represents the ray object.
ray.direction
is the direction vector.
ray.origin
is the origin vector.
rec
represents the result object.
rec.tHit
is the value of the hit.
rec.anyHit
is a boolean.
我的功能可以使用飞机:
center
和 normal
定义平面
My function has access to the plane:
center
and normal
defines the plane
推荐答案
正如wince所评论的那样,您还希望允许分母为否定的,否则您将错过与飞机正面的相交。但是,您仍然希望测试避免被零除,这将指示射线与平面平行。您在 t
的计算中也有多余的否定。总的来说,它应该是这样的:
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;
这篇关于射线平面相交怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!