我的 Plane 课程有两个字段:
public Vector3 Norm; //normal vector
public double Offset; //signed distance to origin
这是我用于交集的代码,不知道是否正确。我仔细检查了我的方程式和所有内容,但我想从对此更有经验的人那里获得反馈。
public override Intersection Intersect(Ray ray)
{
// Create Intersection.
Intersection result = new Intersection();
// Find t.
double t = - (Vector3.Dot(Norm,ray.Start) + Offset) / (Vector3.Dot(Norm, ray.Dir));
if (t < 0) // the ray does not hit the surface, that is, the surface is "behind" the ray
return null;
// Get a point on the plane.
Vector3 p = ray.Start + t * ray.Dir;
// Does the ray intersect the plane inside or outside?
Vector3 planeToRayStart = ray.Start - p;
double dot = Vector3.Dot (planeToRayStart, Norm);
if (dot > 0) {
result.Inside = false;
} else {
result.Inside = true;
}
result.Dist = t;
return result;
}
另外,如果 t 接近 0,我不确定该怎么办?我应该检查 epsilon 以及 epsilon 应该有多大?另外我不确定我是否正确检查光线是否从内或外与平面相交?
谢谢