2017-05-21 39 views
0

我目前使用的代碼來自http://www.codecolony.de/opengl.htm#camera,並且想要在場景中繞軌道添加一個函數(在物體周圍移動相機)。opengl在一個圓上旋轉位置

我認爲它就像一個給定中心上的圓上的旋轉。 它幾乎在工作:它在我選擇的點上旋轉,並且視圖處於正確的方向。問題是半徑不是恆定的,我總是越來越靠近中心。

這裏是代碼我寫的:你有任何想法,爲什麼我(相機)不要總是停留在離中心的距離相等

void CCamera::RotateView(GLfloat Angle, SF3dVector center) // in degree 
{ 
    // rotate the position over the center 
    Position.x = cos(Angle*PIdiv180) * (Position.x - center.x) - sin(Angle*PIdiv180) * (Position.z - center.z) + center.x; 
    Position.z = sin(Angle*PIdiv180) * (Position.x - center.x) + cos(Angle*PIdiv180) * (Position.z - center.z) + center.z; 

    // adjust the viewDir 
    ViewDir = center - Position; 

    //now compute the new RightVector (by cross product) 
    RightVector = CrossProduct(&ViewDir, &UpVector); 

} 

做什麼?

回答

0

那麼,因爲你沒有提供整個班 - 我假設位置是一個成員變量,描述你的相機的位置。假設你擁有無限的數學精度,你就犯了一個主要錯誤。你沒有 - 因爲你每次迭代位置值,你越來越積累了錯誤。它確實累計了幾百次迭代。你需要做的是每次計算新鮮的頭寸。你想要的是gluLookAt()。 [gluLookatDocumentation] [1]