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

3
我刚开始学习在XNA中进行3D编码,并试图理解其中的一些内容。
我的目标是制作一个太空模拟游戏(很原创,我知道),我能够绘制模型,并且我的相机按照我想要的方式工作,但我在理解如何移动我的敌舰时遇到了麻烦。我已经在2D中完成了一些方向行为,但不是在3D中。
我的问题是:
如果我试图将船移动到“寻找”位置,这种移动会如何影响船的世界矩阵(如果有的话)?我正在使用Vector3,并将加速度添加到速度,然后将速度添加到位置。这样做正确吗?
我现在不必发布,但我只是想了解采取什么方法。
谢谢

2
就整合速度/加速度而言,牛顿运动在恒定加速度a下,经过时间dt后的正确公式,起始速度为v0,起始位置为x0,为:x = x0 + v0dt + 0.5adtdt。新速度为v = v0 + a*dt。这些方程适用于三维向量(其中速度和加速度本身就是向量)。 - Dan Bryant
谢谢您的回复,使用此公式,我是否仍然需要维护一个世界矩阵来将飞船转换为世界空间,还是这不是必需的? - Matt Weichselbaum
通常情况下,您会根据演员(在本例中为飞船)的位置和方向构建世界矩阵。假设您正在跟踪的位置是世界位置,则只需将世界矩阵的平移部分替换为演员的位置即可。 - Dan Bryant
1个回答

4

给你的物体/实体/飞船一个位置(Vector3)和旋转(Matrix),然后你可以使用以下代码(以及答案底部的示例)来移动该飞船。

例如,向前移动飞船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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接