C++ でレイ トレーシングを実装しています。球は正常に機能しており、円柱はほぼ正常に機能しています。しかし、レイ トレーサーは円錐との交点を見つけることができません。計算後、ここのリンクに基づいて、交差関数を作成しました。これが私の円錐クラスの findIntersection 関数です。可変比率は -1 * 半径 * 半径 / (高さ * 高さ) です。
virtual double findIntersection(Ray ray){
Vect ray_origin = ray.getRayOrigin();
double ray_origin_x = ray_origin.getVectX();
double ray_origin_y = ray_origin.getVectY();
double ray_origin_z = ray_origin.getVectZ();
Vect ray_direction = ray.getRayDirection();
double ray_direction_x = ray_direction.getVectX();
double ray_direction_y = ray_direction.getVectY();
double ray_direction_z = ray_direction.getVectZ();
Vect cone_center = center;
double cone_center_x = center.getVectX();
double cone_center_y = center.getVectY();
double cone_center_z = center.getVectZ();
Vect diff(cone_center_x - ray_origin_x, cone_center_y - ray_origin_y + height, cone_center_z - ray_origin_z);
//Derive the coefficients of the quadratic equation
double a = pow(ray_direction_x, 2) + pow(ray_direction_y, 2) * ratio + pow(ray_direction_z, 2);
double b = diff.getVectX() * ray_direction_x + diff.getVectY() * ratio * ray_direction_y + diff.getVectZ() * ray_direction_z;
double c = diff.getVectX() * diff.getVectX() + ratio * diff.getVectY() * diff.getVectY() + diff.getVectZ() * diff.getVectZ();
double discriminant = b * b - a * c;
cout << discriminant << "\n";
if (discriminant > 0){
//The ray intersects this cone. Find the lower root
double root_1 = (-1 * b - discriminant)/2 - 0.000001;
if (root_1 > 0) {
// the first root is the smallest positive root
return root_1;
}
else {
// the second root is the smallest positive root
double root_2 = ((sqrt(discriminant) - b)/2) - 0.000001;
return root_2;
}
}
else {
// the ray missed the cone
return -1;
}
}
問題は変数判別式の計算にあります。それらは負であることが判明しているため、交差は返されません。このプログラムの交差関数は、光線の原点から交点までの距離を返します。
誰かが円錐の計算を見て、私が何か間違っているかどうか教えてください.
よろしく、モイラ