编程语言
首页 > 编程语言> > c# – XNA,更平滑的跳跃动画

c# – XNA,更平滑的跳跃动画

作者:互联网

我正在尝试在我的运动测试中进行跳跃功能.我的角色跳了起来然后回来了,但是它非常波涛汹涌而且根本不光滑.
结果是他突然达到他的最大高度,然后顺利下来.

我可以发现问题,for循环不希望与代码很好地配合.但是,我不知道如何规避这一点.有什么方法可以保持按下按钮让他跳得很好吗?

码:

if (leftStick.Y > 0.2f && sprite.Position.Y == position.Y || isPressed(Keys.Up) == true && sprite.Position.Y == position.Y) 
            {
                if (wasLeft == true)
                {
                    sprite.CurrentAnimation = "JumpLeft";

                }
                else if (wasLeft == false)
                {
                    sprite.CurrentAnimation = "JumpRight";
                }

                //This for loop is my issue, it works but it's jumpy and not smooth.

                for (movement.PlayerHeight = 0; movement.PlayerHeight < movement.PlayerMaxHeight; movement.PlayerJump())
                {

                    sprite.Position.Y -= movement.PlayerJump();
                }
            }
            sprite.StartAnimation();
        }
        else
        {

            leftStick = NoInput(leftStick);
        }

private Vector2 NoInput(Vector2 leftstick)
{
if (sprite.Position.Y < position.Y) //(movement.PlayerSpeed > 0) 
            {
                sprite.Position.Y += movement.PlayerHeight;
                movement.PlayerHeight -= movement.Player_Gravity;

                //sprite.Position.Y += movement.PlayerSpeed;
                //movement.PlayerSpeed -= movement.Player_Decel;
            }
            else
            {
                sprite.Position.Y = position.Y;
            }
}

运动类:

public float PlayerMaxHeight = 15f;
public float PlayerHeight = 0;
public float Player_Gravity = 0.01f;
private const float Player_Jump = 0.35f;


public float PlayerJump()
{
    PlayerHeight += Player_Jump + Player_Gravity;
    if (PlayerHeight > PlayerMaxHeight)
    {
        PlayerHeight = PlayerMaxHeight;
    }
    return PlayerHeight;
}

解决方法:

我快速获得跳跃的一般方法是使用渗出值来使运动看起来更平滑.我现在无法查看任何代码/ xna,但我的第一个想法是下面的内容.

定义变量:

float bleedOff = 1.0f;
bool jumping = false;

输入更新:

if(input.JumpKey())
{
    jumping = true;
}

跳跃更新:

if(jumping)
{
    //Modify our y value based on a bleedoff
    //Eventually this value will be minus so we will start falling.
    position.Y += bleedOff;
    bleedOff -= 0.03f;

    //We should probably stop falling at some point, preferably when we reach the ground.
    if(position.Y <= ground.Y)
    {
        jumping = false;
    }
}

bleedOff = MathHelper.Clamp(bleedOff, -1f, 1f);

显然,bleedOff值应该用更多的随机性计算,可能使用重力值,以使它看起来正确,但这会产生加速/减速的错觉,随着它们的上升和下降.

起步非常快,开始减速并最终再次下降,这将加速.底部的夹子将是您的最大垂直速度.

我刚刚在工作中写下了这个,所以如果它不是你想要的那样道歉但我试着保持它更一般.希望能帮助到你.

标签:c,net,xna,game-physics
来源: https://codeday.me/bug/20190621/1250517.html