Xna 如何将相机和模型一起旋转?

Xna 如何将相机和模型一起旋转?,xna,xna-4.0,Xna,Xna 4.0,我试过把它们一起旋转。我试着把它们放在同一个位置,加上一个旋转,但是它们有不同的旋转轴。如果有人更了解xna并找到解决方案,请告诉我。这是我的代码: 我的相机 // Get the new keyboard and mouse state MouseState mouseState = Mouse.GetState(); KeyboardState keyState = Keyboard.GetState(); // Determine how m

我试过把它们一起旋转。我试着把它们放在同一个位置,加上一个旋转,但是它们有不同的旋转轴。如果有人更了解xna并找到解决方案,请告诉我。这是我的代码:

我的相机

// Get the new keyboard and mouse state
        MouseState mouseState = Mouse.GetState();
        KeyboardState keyState = Keyboard.GetState();
        // Determine how much the camera should turn
        float deltaX = (float)lastMouseState.X - (float)mouseState.X;
        float deltaY = (float)lastMouseState.Y - (float)mouseState.Y;
        // Rotate the camera
        ((FreeCamera)camera).Rotate(deltaX * .003f, deltaY * .003f);
        Vector3 translation = Vector3.Zero;
        // Determine in which direction to move the camera
        if (keyState.IsKeyDown(Keys.W)) translation += Vector3.Forward;
        if (keyState.IsKeyDown(Keys.S)) translation += Vector3.Backward;
        if (keyState.IsKeyDown(Keys.A)) translation += Vector3.Left;
        if (keyState.IsKeyDown(Keys.D)) translation += Vector3.Right;
        // Move 3 units per millisecond, independent of frame rate
        translation *= 4 * (float)gameTime.ElapsedGameTime.
        TotalMilliseconds;
        // Move the camera
        ((FreeCamera)camera).Move(translation);
        // Update the camera
        camera.Update();
        // Update the mouse state
        lastMouseState = mouseState;
我的模范班

class CModel
{
    public Vector3 Position { get; set; }
    public Vector3 Rotation { get; set; }
    public Vector3 Scale { get; set; }

    public Model Model {get; private set;}
    private Matrix[] modelTransforms;   
    private GraphicsDevice graphicsDevice;
    private BoundingSphere boundingSphere;

    public CModel(Model Model, Vector3 Position, Vector3 Rotation, Vector3 Scale, GraphicsDevice graphicsDevice)
    {
        this.Model = Model;

        modelTransforms = new Matrix[Model.Bones.Count];
        Model.CopyAbsoluteBoneTransformsTo(modelTransforms);

        buildBoundingSphere();

        this.Position = Position;
        this.Rotation = Rotation;
        this.Scale = Scale;

        this.graphicsDevice = graphicsDevice;
    } 

public void Draw(Matrix View, Matrix Projection)
    {
        // Calculate the base transformation by combining
        // translation, rotation, and scaling
        Matrix baseWorld = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z) * Matrix.CreateTranslation(Position);

        foreach (ModelMesh mesh in Model.Meshes)
        {
            Matrix localWorld = modelTransforms[mesh.ParentBone.Index] * baseWorld;

            foreach (ModelMeshPart meshPart in mesh.MeshParts)
            {
                BasicEffect effect = (BasicEffect)meshPart.Effect;

                effect.World = localWorld;
                effect.View = View;
                effect.Projection = Projection;

                effect.EnableDefaultLighting();
            }

            mesh.Draw();
        }

可能相机是模型类的一部分,然后使用玩家的位置更新相机。

请尝试Microsoft的一个示例,该示例将跟随您的模型并随模型旋转。

您可能希望转到gamedev.stackexchange.com了解此问题