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; }
    }
}
4

1 回答 1

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();
        }
    }
于 2012-10-30T05:56:18.120 回答