如何在XNA中正确加速旋转的3D模型?

时间:2012-10-29 23:20:30

标签: xna-4.0

如何操纵基质以使我的船速在加速度方向上?

当前代码不起作用,当我应用加速然后应用旋转时,加速度会随之改变。

我正在寻找太空船的效果,而不是飞机。

感谢任何帮助 - 我知道这对于游戏程序员来说是最基本的东西!

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;

namespace AsteroidsWindowsGAme
{
    public class ShipModel
    {
        Model model;
        Matrix[] boneTransforms;
        ModelBone shipModelBone;
        Matrix shipTransform;
        TimeSpan lastTime;

        public void Load(ContentManager content)
        {
            model = content.Load<Model>("Ship");
            shipModelBone = model.Bones["Ship"];
            shipTransform = shipModelBone.Transform;
            boneTransforms = new Matrix[model.Bones.Count];
        }

        public void UpdateVelocity(GameTime gameTime)
        {
            if (AccelerationX != 0 || AccelerationY != 0 || AccelerationZ != 0)
            {
                if (lastTime == null)
                {
                    lastTime = gameTime.TotalGameTime;
                }
                else
                {
                    TimeSpan elapsedTime = gameTime.TotalGameTime - lastTime;
                    float elapsedSeconds = (float)elapsedTime.TotalSeconds;
                    VelocityX += AccelerationX * elapsedSeconds;
                    VelocityY += AccelerationY * elapsedSeconds;
                    VelocityZ += AccelerationZ * elapsedSeconds;
                }
            }
        }

        public void UpdatePosition(GameTime gameTime)
        {
            if(VelocityX != 0 || VelocityY != 0 || VelocityZ != 0)
            {
                if (lastTime == null)
                {
                    lastTime = gameTime.TotalGameTime;
                }
                else
                {
                    TimeSpan elapsedTime = gameTime.TotalGameTime - lastTime;
                    float elapsedSeconds = (float)elapsedTime.TotalSeconds;
                    PositionX += VelocityX * elapsedSeconds;
                    PositionY += VelocityY * elapsedSeconds;
                    PositionZ += VelocityZ * elapsedSeconds;
                }
            }
        }

        public void Draw(Matrix world, Matrix view, Matrix projection)
        {
            model.Root.Transform = world;

            Matrix shipRotationMatrix =
                Matrix.CreateTranslation(PositionX, PositionY, PositionZ) *
                Matrix.CreateRotationX(RotationX) *
                Matrix.CreateRotationY(RotationY) *
                Matrix.CreateRotationZ(RotationZ);

            shipModelBone.Transform = shipTransform * shipRotationMatrix;

            model.CopyAbsoluteBoneTransformsTo(boneTransforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = boneTransforms[mesh.ParentBone.Index];
                    effect.View = view;
                    effect.Projection = projection;
                    effect.EnableDefaultLighting();
                }
                mesh.Draw();
            }
        }

        public float RotationX { get; set; }
        public float RotationY { get; set; }
        public float RotationZ { get; set; }
        public float PositionX { get; set; }
        public float PositionY { get; set; }
        public float PositionZ { get; set; }
        public float VelocityX { get; set; }
        public float VelocityY { get; set; }
        public float VelocityZ { get; set; }
        public float AccelerationX { get; set; }
        public float AccelerationY { get; set; }
        public float AccelerationZ { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

我想出了这个:解决方法是将翻译矩阵乘以视图矩阵,如下所示:

    public void Draw(Matrix world, Matrix view, Matrix projection)
    {
        model.Root.Transform = world;

        Matrix shipRotationMatrix =

            Matrix.CreateRotationX(RotationX) *
            Matrix.CreateRotationY(RotationY) *
            Matrix.CreateRotationZ(RotationZ);

        shipModelBone.Transform = shipTransform * shipRotationMatrix;

        model.CopyAbsoluteBoneTransformsTo(boneTransforms);

        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.World = boneTransforms[mesh.ParentBone.Index];
                effect.View = view * Matrix.CreateTranslation(PositionX, PositionY, PositionZ);
                effect.Projection = projection;
                effect.EnableDefaultLighting();
            }
            mesh.Draw();
        }
    }