【问题标题】:XNA C# creating an Isometric view with 3D modelsXNA C# 使用 3D 模型创建等轴测视图
【发布时间】:2013-02-21 18:07:46
【问题描述】:

我正在尝试制作一个简单的等距游戏引擎,但相机出现了一些问题。当我拥有它时,我可以从前面看到我的模型。但我想从等距的角度来看它。我尝试了很多方法,但似乎都没有。也许我陷入了代码本身?你们能帮我写代码吗?

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
}

【问题讨论】:

    标签: c# 3d xna isometric


    【解决方案1】:

    最简单的方法是根据焦点(地面上的一个点或其他任何点)计算相机位置。

    //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 rotatedOffset = Vector3.Transform(CameraOffset, Matrix.CreateRotationY(MathHelper.PiOver2 * 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);
        }
    }
    

    【讨论】:

    • 感谢这对我帮助很大。我现在确实得到了那个漂亮的等距视图。我现在面临的唯一问题是它总是专注于那个点,所以如果我移动我的相机,它就不会像游戏中的相机那样移动。可能是因为我离焦点太近了。我应该只是将所有物体和相机从焦点移回,还是有办法让它始终以某个角度看并在移动相机时真正创建等距视图?或者我当然应该移动我所有的物体而不是相机。在这种情况下,最好的选择是什么?谢谢你的好回答!
    • 当你想移动你的相机时,你只需“移动”焦点,相机将继续保持相对于它的正确位置和方向:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多