2012-12-07 42 views
1

我遇到的困難是將相機移動到3D世界中的物體後面。我會創建兩個視圖模式。如何以相同角度將相機移動到模型後面?

1:for fps(第一人稱)。 第二名:角色背後的外部視角(第二人稱)。

我在網上搜索了一些例子,但它在我的項目中不起作用。

這裏用到我的代碼更改視圖如果F2被按下

//Camera 
     double X1 = this.camera.PositionX; 
     double X2 = this.player.Position.X; 
     double Z1 = this.camera.PositionZ; 
     double Z2 = this.player.Position.Z; 


     //Verify that the user must not let the press F2 
     if (!this.camera.IsF2TurnedInBoucle) 
     { 
      // If the view mode is the second person 
      if (this.camera.ViewCamera_type == CameraSimples.ChangeView.SecondPerson) 
      { 
       this.camera.ViewCamera_type = CameraSimples.ChangeView.firstPerson; 

       //Calcul position - ?? Here my problem 
       double direction = Math.Atan2(X2 - X1, Z2 - Z1) * 180.0/3.14159265; 
       //Calcul angle - ?? Here my problem 

       this.camera.position = .. 
       this.camera.rotation = .. 

       this.camera.MouseRadian_LeftrightRot = (float)direction; 
      } 
      //IF mode view is first person 
      else 
      { 
        //.... 

回答

1

這是一個非常基本的第三人稱相機(你指第二人稱)在Xna。假定您對存儲在播放器的世界矩陣,並可以訪問它:

Vector3 _3rdPersonCamPosition = playerWorldMatrix.Translation + (playerWorldMatrix.Backward * trailingDistance) + (playerWorldMatrix.Up * heightOffset);// add a right or left offset if desired too 
Vector3 _3rdPersonCamTarget = playerWorldMatrix.Translation;//you can offset this similarly too if desired 
view = Matrix.CreateLookAt(_3rdPersonCamPosition, _3rdPersonCamTarget , Vector3.Up); 

如果你的FPS凸輪工作正常,假設它在本質上是相同的位置和方向作爲玩家,您可以替代它的視圖矩陣到位以上這樣的playerWorldMatrix的:

Matrix FPSCamWorld = Matrix.Invert(yourWorkingFPSviewMatrixHere); 

現在無論我寫playerWorldMatrix可以使用FPSCamWorld代替。

+0

謝謝你一個過濾器 –

1

如果我是你,我就會把你現在工作的FPS相機(我假設正確移動,有一個位置矩陣等等),然後再添加一個Translation Transform來將它「移回」播放器後面。

換句話說:

如果你的「翻譯/視圖矩陣」爲FPS的看法是這樣的:

(對不起,沒有用XNA在一段時間發揮,所以不記得正確的類名)

var camTranslateMatrix = [matrix representing player position]; 
var camDirectionMatrix = [matrix representing player direction, etc]; 
var camViewMatrix = camTranslateMatrix * camDirectionMatrix; 

然後你可以改變它,像這樣:

var camTranslateMatrix = [matrix representing player position]; 
var camDirectionMatrix = [matrix representing player direction, etc]; 

// If not in 3rd person, this will be identity == no effect 
var camThirdPersonMatrix = 
     IsInThirdPersonMode ? 
      new TranslateMatrix(back a bit and up a bit) : 
      IdentityMatrix(); 

var camViewMatrix = 
     camTranslateMatrix * 
     camDirectionMatrix * 
     camThirdPersonMatrix; 

讓仙SE?那樣的話,每次你這樣做時,在兩個視圖之間切換而沒有大量討厭的數學是微不足道的。

相關問題