【问题标题】:Ray-Sphere Intersection Derivation射线球相交推导
【发布时间】:2016-03-12 14:57:25
【问题描述】:

我正在尝试编写一个函数,如果光线与球体相交并且我引用的代码如下所示,则返回 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 dot x - 1 = 0) 的参数方程结合起来,但我不知道该怎么做。

【问题讨论】:

标签: math 3d raytracing


【解决方案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

【讨论】:

  • 你知道为什么代码解决的是 b^2 - ac 而不是 b^2 - 4ac 吗?为什么是 c |origin| ^2 - 1?
  • 只是因为b是2*(bew_originnew_dir),所以b^2里面有4,和4*a中的4一起因式分解c。我假设代码期望 r 为 1,因此 r^2 为 1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
相关资源
最近更新 更多