【问题标题】:C# MonoGame - backwards player movementC# MonoGame - 玩家向后移动
【发布时间】:2021-12-30 16:21:56
【问题描述】:

我正在做一个学校项目,我正在制作一个 astroids 克隆。 我一直在研究玩家移动并实现了以下代码。

我在尝试向后移动玩家精灵时遇到了一些奇怪的行为,我很难理解其背后的数学原理。

我已经在下面评论了问题所在

位置=速度+位置;

        if (Keyboard.GetState().IsKeyDown(Keys.Left)) rotation -= 0.1f;
        if (Keyboard.GetState().IsKeyDown(Keys.Right)) rotation += 0.1f;

        if (Keyboard.GetState().IsKeyDown(Keys.Up))
        {
            velocity.X = (float)Math.Cos(rotation) * tangentialVelocity;
            velocity.Y = (float)Math.Sin(rotation) * tangentialVelocity;

        }

        //this is where I need help

        if (Keyboard.GetState().IsKeyDown(Keys.Down))
        {
            velocity.X = (float)Math.Cos(rotation) - tangentialVelocity;
            velocity.Y = (float)Math.Sin(rotation) - tangentialVelocity;

        }
        else if (velocity != Vector2.Zero)
        {
            float i = velocity.X;
            float j = velocity.Y;

            velocity.X = i -= friction * i;
            velocity.Y = j -= friction * j;

        }

【问题讨论】:

  • 请每一步只调用一次Keyboard.GetState()
  • @Strom 如果你有时间,你能详细说明我在哪里多次调用它,以及为什么我不应该

标签: c# monogame game-development


【解决方案1】:

在我看来,向前/向后推力的逻辑应该如下:

给定一些变量,例如int thrustPower = 1.0f;

if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
    velocity.X += (float)Math.Cos(rotation) * thrustPower;
    velocity.Y += (float)Math.Sin(rotation) * thrustPower;
}

if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
    velocity.X -= (float)Math.Cos(rotation) * thrustPower;
    velocity.Y -= (float)Math.Sin(rotation) * thrustPower;
}

因此,如果按下Keys.UpKeys.Down,每个轴上的速度将按投影到该轴上的ThrustPower 的正常量进行缩放。

这也解决了必须处理您所谓的tangentialVelocity 的问题,但我认为实际上应该是radialVelocity 或只是speed

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多