レイキャスティングでオブジェクトピッキング
-
29-10-2019 - |
質問
私は、ボックス内でマウスヒットを検出するためのレイ鋳造アルゴリズムの不正確さに問題がありました。私はこれを適切に修正する方法について完全に途方に暮れており、それは数週間私を悩ませてきました。
問題は、画像で最も簡単に説明されています([0、0、-30]を中心としたボックス):
黒い線は、描かれた実際のヒットボックスを表し、緑色の箱は実際にヒットしているように見えるものを表します。オフセット(ボックスが原点から遠く離れている場合、大きくなるように見える)に注意してください。
これが関連するコードです、
レイボックスキャスト:
double BBox::checkFaceIntersection(Vector3 points[4], Vector3 normal, Ray3 ray) {
double rayDotNorm = ray.direction.dot(normal);
if(rayDotNorm == 0) return -1;
Vector3 intersect = points[0] - ray.origin;
double t = intersect.dot(normal) / rayDotNorm;
if(t < 0) return -1;
// Check if first point is from under or below polygon
bool positive = false;
double firstPtDot = ray.direction.dot( (ray.origin - points[0]).cross(ray.origin - points[1]) );
if(firstPtDot > 0) positive = true;
else if(firstPtDot < 0) positive = false;
else return -1;
// Check all signs are the same
for(int i = 1; i < 4; i++) {
int nextPoint = (i+1) % 4;
double rayDotPt = ray.direction.dot( (ray.origin - points[i]).cross(ray.origin - points[nextPoint]) );
if(positive && rayDotPt < 0) {
return -1;
}
else if(!positive && rayDotPt > 0) {
return -1;
}
}
return t;
}
マウスからレイ:
GLint viewport[4];
GLdouble modelMatrix[16];
GLdouble projectionMatrix[16];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
GLfloat winY = GLfloat(viewport[3] - mouse_y);
Ray3 ray;
double x, y, z;
gluUnProject( (double) mouse_x, winY, 0.0f, // Near
modelMatrix, projectionMatrix, viewport,
&x, &y, &z );
ray.origin = Vector3(x, y, z);
gluUnProject( (double) mouse_x, winY, 1.0f, // Far
modelMatrix, projectionMatrix, viewport,
&x, &y, &z );
ray.direction = Vector3(x, y, z);
if(bbox.checkBoxIntersection(ray) != -1) {
std::cout << "Hit!" << std::endl;
}
私は実際の光線を線として描画しようとしましたが、描かれたボックスと正しく交差するようです。
オフセットの問題は、ボックスの位置ですべてのポイントと光線起源/方向をマイナスすることで部分的に修正されましたが、なぜそれが機能し、ヒットボックスのサイズが不正確なままである理由はわかりません。
何かアイデア/代替アプローチはありますか?必要に応じて提供する他のコードがあります。
正しい解決策はありません
所属していません StackOverflow