ここ数ヶ月レイトレーシング関係のことを弄っているのでレイ(半直線)と曲面との交点計算についてのメモ.
$$\LaTeX$$ for WordPressのテストも兼ねて.
平面とレイの交差判定
点$${\bf p}$$を通り方向ベクトル$${\bf d}$$の半直線と,点$${\bf q}$$を通り法線ベクトル$${\bf n}$$の平面との交差判定は以下のようになる.
これらの交点を$${\bf x}$$とすると,
半直線:$${\bf x}={\bf p}+t{\bf d}$$
平面:$$\left({\bf x}-{\bf q}\right)\cdot {\bf n}=0$$
$$\left({\bf p}+t{\bf d}-{\bf q}\right)\cdot{\bf n}=0$$
$$({\bf p}\cdot{\bf n})+t({\bf d}\cdot{\bf n})-({\bf q}\cdot{\bf n})=0$$
$$t=\frac{({\bf q}\cdot{\bf n})-({\bf p}\cdot{\bf n})}{({\bf d}\cdot{\bf n})}$$
$$({\bf d}\cdot{\bf n})=0$$つまり平面の法線ベクトルと半直線の方向ベクトルが直交しているとき交差なし.
球とレイの交差判定
点$${\bf p}$$を通り方向ベクトル$${\bf d}$$の半直線と,点$${\bf c}$$を中心として半径$$r$$の円との交差判定は以下のようになる.
これらの交点を$${\bf x}$$とすると,
半直線:$${\bf x}={\bf p}+t{\bf d}$$
円: $$\left|{\bf x}-{\bf c}\right|^{{2}} =r^{2}$$
$$\left|{\bf p}+t{\bf d}-{\bf c}\right|^{{2}} =r^{2}$$
$$\left({\bf p}+t{\bf d}-{\bf c}\right)\cdot\left({\bf p}+t{\bf d}-{\bf c}\right)-r^{2}=0$$
$$({\bf p}\cdot{\bf p})+2\left({\bf p}\cdot(t{\bf d}-{\bf c})\right)+\left((t{\bf d}-{\bf c})\cdot(t{\bf d}-{\bf c})\right)-r^{2}=0$$
$$({\bf p}\cdot{\bf p})+2t({\bf p}\cdot{\bf d})-2({\bf p}\cdot{\bf c})+t^{2}({\bf d}\cdot{\bf d})-2t({\bf d}\cdot{\bf c})+({\bf c}\cdot{\bf c})-r^{2}=0$$
$$({\bf d}\cdot{\bf d})t^{2}+2\left(({\bf p}\cdot{\bf d})-({\bf d}\cdot{\bf c})\right)t+({\bf p}\cdot{\bf p})-2({\bf p}\cdot{\bf c})+({\bf c}\cdot{\bf c})-r^{2}=0$$
$$({\bf d}\cdot{\bf d})t^{2}+2\left({\bf d}\cdot({\bf p}-{\bf c})\right)t+\left(({\bf p}-{\bf c})\cdot({\bf p}-{\bf c})\right)-r^{2}=0$$
$$|{\bf d}|^{2}t^{2}+2\left({\bf d}\cdot({\bf p}-{\bf c})\right)t+|{\bf p}-{\bf c}|^{2}-r^{2}=0$$
この二次方程式を解く.
久しぶりにlatexいじったから疲れた.というかこのプラグインバグってんのかな.上付き文字か変なところまで続いちゃうんだが.
次は,トーラスとレイの交差判定辺りを書いてみるかな.四次曲面なんで代数的に解くことができるけど,恐ろしくややこしくなります.普通は数値的に解いた方がいいのかもね.