我的飛機類有兩個字段:這個平面射線相交碼是否正確?
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應該是?此外,我不確定我是否正確檢查了射線是否從內部或外部與 飛機相交?
感謝