基于恒定不一致的运动

问题描述 投票:0回答:2

我制作了一个属于角色的类,在更新方法中,我使用了尽可能多的物理方法使它逼真地工作,并编辑了值以使运动感觉良好。我最近注意到,当我左右移动时,它以不同的速度移动,不知道多久了。我在开始时声明所有大写命名的变量常量,并使用相同的常量进行移动,所以我不知道是什么原因造成的。

我已尽力找出问题所在并解决,但我什么都没有。

我设置常数的开始:

const float GRAVITY = 1f;
const float AIR_SPEED_COEFFICIENT = 0.4f;
const float TERMINAL_AIR_HORIZONTAL_VELOCITY = 10f;
const float AIR_RESISTANCE = 0.97f;
const float FRICTION = 0.64f;

更新功能:

public void Update(Rectangle floor, GameWindow Window)
{
    jumpFrameCounter++;
    if (Keyboard.GetState().IsKeyDown(Keys.Up) || Keyboard.GetState().IsKeyDown(Keys.W))
    {
        if (jumpFrameCounter > 11 && jumpsUsed < amountOfJumps)
        {
            velocity.Y = -GRAVITY * 16 * jumpHeight;
            jumpsUsed++;
            jumpFrameCounter = 0;
        }
    }
    if (Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A))
    {
        if (hitbox.Bottom >= floor.Top)
        {
            velocity.X -= RUN_SPEED / 4;
        }
        else
        {
            velocity.X -= airSpeed * AIR_SPEED_COEFFICIENT;
        }
    }
    if (Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D))
    {
        if (hitbox.Bottom >= floor.Top)
        {
            velocity.X += RUN_SPEED / 4;
        }
        else
        {
            velocity.X += airSpeed * AIR_SPEED_COEFFICIENT;
        }
    }
    if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))
    {
        if (hitbox.Bottom >= floor.Top)
        {
            velocity.X *= FRICTION;
        }
        else
        {
            velocity.X *= AIR_RESISTANCE;
        }

    }
    if (Keyboard.GetState().IsKeyDown(Keys.Down) || Keyboard.GetState().IsKeyDown(Keys.S))
    {
        fastFall = true;
    }

    velocity.Y += GRAVITY * unfloatyness;
    if (fastFall)
    {
        velocity.Y += 6 * GRAVITY;
    }

    if (hitbox.Bottom >= floor.Top)
    {
        if (velocity.X > RUN_SPEED * groundSpeed)
        {
            velocity.X = RUN_SPEED * groundSpeed;
        }
        else if (velocity.X < -RUN_SPEED * groundSpeed)
        {
            velocity.X = -RUN_SPEED * groundSpeed;
        }
    }
    else
    {
        if (velocity.X > TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed)
        {
            velocity.X = TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed;
        }
        else if (velocity.X < -TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed)
        {
            velocity.X = -TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed;
        }
    }

    position += velocity;

    hitbox = new Rectangle((int)position.X, (int)position.Y, fighterTexture.Width, fighterTexture.Height);

    if (hitbox.Bottom > floor.Top && velocity.Y > 0)
    {
        position.Y = floor.Top - fighterTexture.Height;
        jumpsUsed = 0;
        fastFall = false;
        velocity.Y = -1f;
    }
    if (hitbox.Left < 0)
    {
        position.X = 0;
    }
    else if (hitbox.Right > Window.ClientBounds.Width)
    {
        position.X = Window.ClientBounds.Width - fighterTexture.Width;
    }
    if (hitbox.Top < 0)
    {
        position.Y = 0;
    }
    else if (hitbox.Bottom > Window.ClientBounds.Height)
    {
        position.Y = Window.ClientBounds.Bottom - fighterTexture.Height;
    }
}
c# xna monogame
2个回答
0
投票

此行令人困惑。Because AND&& has higher precedence than OR||

&&

该声明将成为。

||

我想你实际上想要的是:

 if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))

但是您对此未发表任何评论,我不知道该如何解释。这条线看起来像“如果x的速度接近零并且按下 if (velocity.X > 0.00001f || (velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) ) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A))) if ( ( velocity.X > 0.00001f || (velocity.X < -0.00001f ) && ( ! (Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A))) ) Left或未按下D”,则将X设置为更大的值。

但是此行与您所描述的相冲突。

我最近注意到,当我左右移动时,它以不同的速度移动,不知道多久了。

您想让Left和Right做同样的事情,但不要用代码。

我的建议是“使用括号以提高可读性”和“如果有3个以上的条件要检查一行,请使用bool”。


0
投票

解决方案是路易斯·高(Louis Go)提供的解决方案,

事实证明,就像将if语句更改为2个嵌套语句一样简单,并且使计算机感到困惑。

旧代码:

A

修订后的代码:

Right
© www.soinside.com 2019 - 2024. All rights reserved.