C# MonoGame - 向后玩家移动

C# MonoGame - backwards player movement

我正在做一个学校项目,我正在制作一个小行星克隆体。 我一直在研究玩家移动并实现了下面的代码。

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

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

位置=速度+位置;

        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;

        }

我觉得forwards/backwards推力的逻辑应该是这样的:

给定一些变量,例如 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.