2013-02-21 29 views
1

我正在嘗試製作一個簡單的等距遊戲引擎,但是有一些相機問題。當我擁有它時,我可以從前面看到我的模型。但我想從等軸測角度看它。我嘗試了很多方法,但似乎沒有任何工作。也許我陷入了代碼本身?你們可以幫我解碼嗎?XNA C#用3D模型創建等軸測視圖

public class Camera : PositionedObject 
{ 

    #region Fields 
    private Matrix cameraRotation; 

    #endregion 


    #region Properties 
    public Matrix View 
    { 
     get; 
     set; 
    } 

    public Matrix Projection 
    { 
     get; 
     protected set; 
    } 

    public Vector3 Target 
    { 
     get; 
     set; 
    } 
    #endregion 

    #region Constructor 
    public Camera(Game game, Vector3 position, Vector3 target, Vector3 rotation, bool Orthographic, float near, float far) 
     : base(game) 
    {  
     Position = position; 
     RotationInRadians = rotation; 
     Target = target; 


     if (Orthographic) 
     { 
      Projection = Matrix.CreateOrthographic(Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height, 
       near, far); 
     } 
     else 
     { 
      Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 
       (float)Game.Window.ClientBounds.Width/(float)Game.Window.ClientBounds.Height, near, far); 
     } 




    } 
    #endregion 

    #region Public Methods 

    public override void Initialize() 
    { 
     base.Initialize(); 
     cameraRotation = Matrix.Identity; 

    } 



    public override void Update(GameTime gameTime) 
    { 
     base.Update(gameTime); 

     cameraRotation = Matrix.CreateFromAxisAngle(cameraRotation.Forward, RotationInRadians.Z) 
      * Matrix.CreateFromAxisAngle(cameraRotation.Right, RotationInRadians.X) 
      * Matrix.CreateFromAxisAngle(cameraRotation.Up, RotationInRadians.Y); 

     Target = Position + cameraRotation.Forward; 
     View = Matrix.CreateLookAt(Position, Target, cameraRotation.Up); 
    } 

    public void Draw(BasicEffect effect) 
    { 
     effect.View = View; 
     effect.Projection = Projection; 
    } 
    #endregion 
} 

回答

0

最簡單的方法是根據焦點(地面上的一點,或其他)計算相機位置。

//Lets start with looking at origo for now. 
Vector3 FocusPoint = Vector3.Zero; 

//This tells us where the camera should be, RELATIVE to the point we are watching. 
//I set this a little up and a little back 
Vector3 CameraOffset = new Vector3(0f, 20f, 20f); 

Matrix ViewMatrix 
{ 
    get 
    { 
     //The Offset is just up and back, we need to rotate it 45* 
     var rotatedOffste = Vector3.Transform(CameraOffset, Matrix.CreateRotationY(MathHelper.PiOverTwo * 0.5f)); 

     //Now we can create out viewmatrix. No need to use a transformed "up" unless it's not going to be upside down or something. 
     return Matrix.CreateLookAt(rotatedOffset, FocusPoint, Vector3.Up); 
    } 

} 
+0

謝謝,真的幫了我很多。我現在確實得到了很好的等距視圖。我現在唯一面臨的問題是它始終專注於這一點,所以如果我移動相機,它不會像遊戲機相機那樣移動。可能是因爲我太靠近焦點了。我是否應該將所有物體和相機從焦點處移回來,或者有辦法始終讓它看起來處於某個角度,並在移動相機時真正創建等軸測視圖?或者當然,我應該移動所有的物體而不是相機。這種情況下最好的選擇是什麼?謝謝你的好回答! – Worempie 2013-02-22 20:57:48

+0

當你想移動你的相機時,你只需「移動」FocusPoint,相機將繼續保持相對於該位置的正確位置和方向:) – 2013-02-25 07:51:49

+0

固定:D非常感謝! – Worempie 2013-03-10 10:30:30