0

所以我正在尝试编写代码来查看光线是否与平面圆盘相交,我希望在这里检查一下。我的磁盘始终以负 z 轴为中心,因此其法线向量应为 (0,0, -1)。

我这样做的方式是首先计算射线平面的交点,然后确定该交点是否在磁盘的“范围”内。

在我的代码中,我得到了一些看起来不对的数字,我不确定问题是出在这个方法上还是可能出在其他地方。因此,如果此代码有问题,我将不胜感激您的反馈!=)

这是我的代码:

float d = z_intercept; //This is where disk intersects z-axis. Can be + or -.
ray->d = Normalize(ray->d); 
Point p(0, 0, d); //This is the center point of the disk
Point p0(0, 1, d);
Point p1(1, 0, d);
Vector n = Normalize(Cross(p0-p, p1-p));//Calculate normal

float diameter = DISK_DIAMETER; //Constant value
float t = (-d-Dot(p-ray->o, n))/Dot(ray->d, n); //Calculate the plane intersection
Point intersection = ray->o + t*ray->d;
return (Distance(p, intersection) <= diameter/2.0f); //See if within disk


//This is my code to calculate distance
float RealisticCamera::Distance(Point p, Point i) 
{
return sqrt((p.x-i.x)*(p.x-i.x) + (p.y-i.y)*(p.y-i.y) + (p.z-i.z)*(p.z-i.z));
}
4

2 回答 2

1

"我的磁盘总是以负 z 轴为中心,所以它的法向量应该是 (0,0, -1)。 "

这一事实简化了计算。

退化情况:ray->dz = 0 -> if ray->oz = d then ray位于圆盘平面,检查为2Dd,否则ray平行且没有交集

常见情况:t = (d - ray->o.z) / ray->d.z

如果 t 为正值,则为该 t 找到 x 和 y,并检查 x^2+y^2 <= disk_radius^2

于 2013-05-20T11:57:30.433 回答
0

t的计算是错误的。

射线上的点是:

ray->o + t * ray->d

特别是,射线上一点的坐标z为:

ray->o.z() + t * ray->d.z()

必须等于d。出来

t = ( d - ray->o.z() ) / ray->d.z()
于 2014-06-10T13:54:45.823 回答