3

我刚开始在 XNA 中进行 3D 编码,并试图了解一些事情。

我在 XNA 的目标是制作一个太空模拟游戏(我知道是原创的)我能够绘制模型并且我的相机可以按照我的意愿工作,我遇到的麻烦是了解如何移动我的敌人船舶。我对 2d 中的转向行为做了一些有价值的事情,但不是 3d。

我的问题是:

如果我试图将船只移动到“寻找”一个位置,这种移动如何影响船只的世界矩阵(如果有的话)?我正在使用vector3s,并将加速度添加到速度,然后将速度添加到位置。这是正确的方法吗?

我现在不必发帖,否则我会发帖,我只是想了解采取什么方法。

谢谢

4

1 回答 1

4

给您的对象/实体/船一个位置(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;
    }
}
于 2012-01-19T00:47:12.357 回答