Ray-Sphere交叉推导

时间:2016-03-11 14:53:41

标签: math 3d raytracing

我尝试编写一个函数,如果光线与球体相交,则返回true,而我引用的代码就是这样的:

// given Sphere and Ray as arguments
invert the Sphere matrix
make new Ray object 
  origin of this object = old Ray origin * inverted Sphere matrix
  direction = old Ray direction * inverted Sphere matrix
a = |new direction| ^ 2
b = dot product of new origin and new direction
c = |new origin| ^ 2 - 1

det = b*b - a*c
if det > 0 there is an intersection

我坚持理解为什么我们需要首先反转Sphere矩阵然后将其乘以Ray的原点和方向。我也很困惑如何导出二次方程变量a,b和c以及结束。我知道我必须将光线(p + td)和圆形(x点x - 1 = 0)的参数方程组合起来,但我无法弄清楚如何这样做。

1 个答案:

答案 0 :(得分:1)

您需要反转球体矩阵以使射线位于球体的坐标系中,如果球体未缩放,则与简单设置new_origin = origin - sphere_center(并使用原始方向)相同 该等式由公式形成:

|new_dir*t + new_origin|^2 = r^2 (presumably r is 1)

如果你展开它,你会得到:

|new_dir|^2*t^2 + 2*(new_origin·new_dir)*t + |new_origin|^2-r^2 = 0