我如何一起旋转相机和模型



我尝试了一种将它们旋转在一起的方法。我试图将它们添加到相同的位置并添加它们旋转,但它们具有不同的旋转轴。如果有人知道更好的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的追逐摄像机的示例,该示例将遵循您的模型并旋转它。

最新更新