2013-03-23 38 views
0

我做了一個3D場景,我有三組模型。我有一臺攝像機正在查看其中一組。 這些組中的模型圍繞組中心(上軸)旋轉,模型也旋轉它們自己的本地中心(上軸)。xna相機圍繞自己的軸旋轉,並看着模型組

這與XNA賽車遊戲車選擇屏幕類似。 唯一的區別是我希望能夠旋轉我的相機來查看另一個組。 當旋轉攝像頭看下一組我想旋轉它120度(我有3個模型組360°/ 3 = 120)

注意: - 攝像頭正在查看一個組略高於組的飛機。

相機:

viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);  
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1f, 1000f); 

OK:

  • 我可以繞其自身的軸線模型。
  • 我可以圍繞組中心點左右旋轉組模型組 (在遊戲屏幕中,與屏幕最近的模型是當前選中的模型)。

不正常:

  • 我不能找到解決它自己漲軸旋轉相機正確的方法。

圖像夫婦澄清這種情況:

回答

0

它像你想所有的旋轉是對世界上方向軸,而不是相機的高達看起來對我來說軸。

這是將視圖從一個組更改爲另一個組的可能方法。這將以恆定的速率旋轉。如果你希望旋轉到達所需組時需要稍微減慢一些,則需要添加代碼。這假設你知道每個組的中心位置。

float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; 
float rotationRate = 0.01f; //radians per second. set to taste 
Vector3 cameraGoal;// the center point of the group that you want the camera to settle on. changes with input. 

Vector3 currentLookingDirection = cameraTarget - cameraPosition;// 
Vector3 desiredLookingDirection = cameraGoal - cameraPosition; 
float angularSeparationFactor = Vector3.Dot(currentLookingDirection, desiredLookingDirection); 
if(angularSeparationFactor < 0.98f);//set to taste 
{ 
    float directionToRotate = Math.Sign(Vector3.Cross(currentLookingDirection , desiredLookingDirection).Y); 
    cameraTarget = Vector3.Transform(cameraTarget - cameraPosition, Matrix.CreateRotationY(rotationRate * elapsed * directionToRotate)) + cameraPosition; 
} 
else 
{ 
    cameraTarget = cameraGoal; 
} 

view = Matrix.CreatLookAt(cameraPosition, cameraTarget, Vector3.Up); 
+0

謝謝,這可以幫助我很多!非常好的答案! – Tryk 2013-03-24 17:50:07