2

我已經使用C++和OpenGL設置了簡單的3D第一人稱演示,並且它看起來工作得很好。我的目標是:當用戶將攝像機指向一個平面並單擊鼠標左鍵時,我想繪製一個光線指向相機朝向玩家位置的方向與該平面的交點。雷射平面交叉點:結果不準確 - 舍入誤差?

於是,我開始了與兩個向量,Vector positionVector rotation,其中向量是一個非常標準的三維矢量類:

class Vector 
{ 
    public: 
    GLfloat x, y, z; 

    Vector() {}; 

    Vector(GLfloat x, GLfloat y, GLfloat z) 
    { 
     this->x = x; 
     this->y = y; 
     this->z = z; 
    } 

    GLfloat dot(const Vector &vector) const 
    { 
     return x * vector.x + y * vector.y + z * vector.z; 
    } 

    ... etc ... 

而且Plane p,與平面是一個簡單的結構存儲在正常的飛機和d。我複製這個結構直接從書「實時碰撞檢測,」通過和Christer埃裏克森:

struct Plane 
{ 
    Vector n; // Plane normal. Points x on the plane satisfy Dot(n,x) = d 
    float d; // d = dot(n,p) for a given point p on the plane 
}; 

首先,我拿position的射線,我稱之爲a的開始。我用那個點和rotation找到射線的末端,b。然後,我使用一種算法從同一本書中找出光線和飛機的交點。其實我已經實現了相同的方法,不過我正在使用的代碼從書中直接在這裏只是爲了確保我沒惹什麼了:

void pickPoint() 
{ 
    const float length = 100.0f; 

    // Points a and b 
    Vector a = State::position; 
    Vector b = a; 

    // Find point b of directed line ab 
    Vector radians(Math::rad(State::rotation.x), Math::rad(State::rotation.y), 0); 
    const float lengthYZ = Math::cos(radians.x) * length; 

    b.y -= Math::sin(radians.x) * length; 
    b.x += Math::sin(radians.y) * lengthYZ; 
    b.z -= Math::cos(radians.y) * lengthYZ; 

    // Compute the t value for the directed line ab intersecting the plane 
    Vector ab = b - a; 

    GLfloat t = (p.d - p.n.dot(a))/p.n.dot(ab); 

    printf("Plane normal: %f, %f, %f\n", p.n.x, p.n.y, p.n.z); 
    printf("Plane value d: %f\n", p.d); 
    printf("Rotation (degrees): %f, %f, %f\n", State::rotation.x, State::rotation.y, State::rotation.z); 
    printf("Rotation (radians): %f, %f, %f\n", radians.x, radians.y, radians.z); 
    printf("Point a: %f, %f, %f\n", a.x, a.y, a.z); 
    printf("Point b: %f, %f, %f\n", b.x, b.y, b.z); 
    printf("Expected length of ray: %f\n", length); 
    printf("Actual length of ray: %f\n", ab.length()); 
    printf("Value t: %f\n", t); 

    // If t in [0..1] compute and return intersection point 
    if(t >= 0.0f && t <= 1.0f) 
    { 
     point = a + t * ab; 
     printf("Intersection: %f, %f, %f\n", point.x, point.y, point.z); 
    } 
    // Else no intersection 
    else 
    { 
     printf("No intersection found\n"); 
    } 

    printf("\n\n"); 
} 

當我渲染這一點與OpenGL的,它看起來非常接近光線和飛機的交點。但是從打印出實際值後,我發現對於特定的位置和旋轉,交點最多可以關閉0.000004。下面是一個交點不準確的例子 - 我知道交點不在平面上,因爲它的Y值應該是0,而不是0.000002。我還可以子它放回平面方程,並得到一個不等式:

Plane normal: 0.000000, 1.000000, 0.000000 
Plane value d: 0.000000 
Rotation (degrees): 70.100044, 1.899823, 0.000000 
Rotation (radians): 1.223477, 0.033158, 0.000000 
Point a: 20.818802, 27.240383, 15.124892 
Point b: 21.947229, -66.788452, -18.894285 
Expected length of ray: 100.000000 
Actual length of ray: 100.000000 
Value t: 0.289702 
Intersection: 21.145710, 0.000002, 5.269455 

現在,我知道浮點數只是實數的近似,所以我猜這是不準確的浮動只是效果點四捨五入,儘管我可能在代碼中的其他地方犯了一個錯誤。我知道十字路口只有極少量,但我仍然關心它,因爲我打算使用這些點來定義模型或級別的頂點,方法是將它們捕捉到任意方向的網格中,所以我實際上想要即使它們稍微不準確,那些點也會在網格上。這可能是一個誤導的方法 - 我不知道。

所以我的問題是:這是不正確的只是浮點四捨五入工作,還是我在別的地方犯了一個錯誤?

如果只是浮點舍入,有什麼辦法可以處理嗎?我嘗試以各種方式將旋轉和位置矢量的值四捨五入,這顯然導致交點不太準確,但我仍然有時會得到不在飛機上的交點。我讀過一個類似問題的回答(Is this plane-ray intersection code correct?),提到保持尺寸很大,但我不確定這意味着什麼。

對不起,如果這個問題以前曾被問過 - 我搜索過了,但是我沒有看到任何與我遇到麻煩的事情。謝謝!

回答

1

你的數學似乎是正確的,這看起來像一個舍入誤差。我有一個強烈的感覺,這是這條線:

GLfloat t = (p.d - p.n.dot(a))/p.n.dot(ab); 

這就是說我沒有看到任何其他方法來計算噸。你可以通過在你的printf語句中使用「%.12f」(或更多)來驗證你是否失去了精度。查明罪魁禍首的另一種方法是嘗試一步一步地做你的t計算,並一路打印結果,看看你是否在某處失去了精確度。

您是否嘗試過使用雙精度浮點數,如果精度對您真的很重要?