XNA (MonoGame)世界空间和物体移动

本文关键字:移动 空间 世界 MonoGame XNA | 更新日期: 2023-09-27 18:15:16

我刚开始在XNA中进行3D编码,我试图让我的头脑围绕一些事情。

我在XNA的目标是制作一款太空模拟游戏(我知道是原创的),我能够画出模型,我的相机也能像我想的那样工作,我遇到的麻烦是理解如何移动敌舰。我在2d中做过一些有价值的转向行为,但在3d中却没有。

我的问题是:

如果我试图移动船只去"寻找"一个位置,这个移动如何影响船只的世界矩阵(如果有的话)?我用矢量3,把加速度加到速度上,然后把速度加到位置上。这是正确的方法吗?

我现在不需要张贴,否则我会,我只是想了解采取什么方法。

谢谢

XNA (MonoGame)世界空间和物体移动

给你的对象/实体/船一个位置(Vector3)和旋转(矩阵),然后你可以使用下面的代码(以及这个答案底部的示例)来移动船。

例如将船向前移动5个单位:

Entity myShip = new Entity();
myShip.GoForward(5.0f);

让你的船滚90度

myShip.Roll(MathHelper.PiOver2);

这里是示例代码

public class Entity
{
    Vector3 position = Vector3.Zero;
    Matrix rotation = Matrix.Identity;
    public void Yaw(float amount)
    {
       rotation *= Matrix.CreateFromAxisAngle(rotation.Up, amount);
    }
    public void YawAroundWorldUp(float amount)
    {
       rotation *= Matrix.CreateRotationY(amount);
    }
    public void Pitch(float amount)
    {
       rotation *= Matrix.CreateFromAxisAngle(rotation.Right, amount);
    }
    public void Roll(float amount)
    {
       rotation *= Matrix.CreateFromAxisAngle(rotation.Forward, amount);
    }
    public void Strafe(float amount)
    {
       position += rotation.Right * amount;
    }
    public void GoForward(float amount)
    {
       position += rotation.Forward * amount;
    }
    public void Jump(float amount)
    {
       position += rotation.Up * amount;
    }
    public void Rise(float amount)
    {
       position += Vector3.Up * amount;
    }
}