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

发布于 2024-12-17 08:00:40 字数 321 浏览 2 评论 0原文

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

我使用 XNA 的目标是制作一款太空模拟游戏(我知道是原创的)我能够绘制模型并且我的相机可以按我希望的方式工作,我遇到的麻烦是理解如何移动我的敌人船舶。我在 2d 中的转向行为方面做了一些有价值的工作,但在 3d 中却没有。

我的问题是:

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

我不必现在就发帖,或者我会的,我只是想了解应该采取什么方法。

谢谢

I just started with 3D coding in XNA and am trying to get my head around a few things.

My goal with XNA is to make a space sim game (original, I know) I am able to draw models and my camera is working as I'd like it to, where I am running into trouble is in understanding how to move my enemy ships. I have done some worth with steering behaviors in 2d, but not 3d.

My question is:

If I am trying to move ships to 'seek' a location, how does this movement effect the ship's world matrix (if at all)? I am using vector3s, and adding the acceleration to the velocity, and then the velocity to the position. Is this the right approach?

I do not have to post right now or I would, I am merely trying to understand what approach to take.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海风掠过北极光 2024-12-24 08:00:40

为您的对象/实体/船舶提供位置(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;
    }
}

Give your object/entity/ship a position (Vector3) and rotation (Matrix), and then you can use the following code (and the sample at the bottom of this answer) to move the ship around.

For example to move the ship forward 5 units:

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

To make your ship barrel roll 90 degrees

myShip.Roll(MathHelper.PiOver2);

And here's the sample code

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文