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

时间:2014-02-06 18:54:42

标签: xna xna-4.0

我尝试过将它们一起旋转的方法。我试图将它们添加到相同位置并添加旋转但它们具有不同的旋转轴。如果有人知道更好的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();
        }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

尝试使用Chase camera的Microsoft示例,该示例将跟随您的模型并随之旋转。