如何使对象多次从地面反弹(Java和LWJGL)?

如何使对象多次从地面反弹(Java和LWJGL)?,java,lwjgl,Java,Lwjgl,我正在学习制作一个简单的游戏,并成功地编写了一个非常基本的弹跳动作,在按下一个跳跃键后,玩家回到地面并再次弹起,创造出一种愚蠢的物理错觉。我面临的问题是如何实现它,使玩家不反弹一次而是反弹两次,第二次反弹速度较低。换句话说,现在的代码(我将在后面展示)非常简单:有一个“跳跃”的标志,一旦跳跃,标志变为真,在下一帧检查它是否为真,如果是真的,我只需在y轴速度上加上一些速度,玩家就会反弹。但我似乎找不到编码第二次反弹的方法,因为每件事都是在逐帧的基础上发生的。我需要为第二次反弹增加更少的速度,但是只

我正在学习制作一个简单的游戏,并成功地编写了一个非常基本的弹跳动作,在按下一个跳跃键后,玩家回到地面并再次弹起,创造出一种愚蠢的物理错觉。我面临的问题是如何实现它,使玩家不反弹一次而是反弹两次,第二次反弹速度较低。换句话说,现在的代码(我将在后面展示)非常简单:有一个“跳跃”的标志,一旦跳跃,标志变为真,在下一帧检查它是否为真,如果是真的,我只需在y轴速度上加上一些速度,玩家就会反弹。但我似乎找不到编码第二次反弹的方法,因为每件事都是在逐帧的基础上发生的。我需要为第二次反弹增加更少的速度,但是只有在第一次反弹发生之后,我现在得到的只是这些速度和一次反弹的总和。我不知道如何欺骗程序添加第一个速度,“等待”直到它落地,然后添加第二个,然后绘制下一帧

以下是单次反弹的代码(它可以工作)

如果(y0)xspeed=xspeed*0.925;
如果(跳转==真)
{
y速度+=4.2;
跳跃=假;
}
//跳跃
if(键盘.isKeyDown(键盘.按键向上))
{
y速度=10;
跳跃=正确;
}

我会将代码重新安排为如下内容:

boolean airborne = false;

while (true) // the main game loop
{
    if (y <= 48 && airborne) // if player just hit the ground
    {
        //  Perform the "bounce" by changing their vertical direction and decreasing its magnitude
        ySpeed = -ySpeed/2.0;

        //  This will stop them from bouncing infinitely.  After the bounce gets too small,
        //  they will finally land on the ground.  I suggest you play around with this number to find one that works well
        if (Math.abs(ySpeed) <= 0.5)
        {
            //  Stop the bouncing
            ySpeed = 0;

            //  Place them on the ground
            airborne = false;
            y = 48;
        }
    }
    //  Apply friction if they are on the ground
    if (!airborne)
    {
        // Friction on ground
        if ((!Keyboard.isKeyDown(Keyboard.KEY_LEFT)) && xspeed < 0) xspeed = xspeed * 0.925;
        if ((!Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) && xspeed > 0) xspeed = xspeed * 0.925;
    }
    //  Apply a jump action if they're pressing the jump button while on the ground
    if (Keyboard.isKeyDown(Keyboard.KEY_UP) && !airborne) 
    {
        airborne = true;
        yspeed = 10;
    }
}
boolean=false;
while(true)//主游戏循环
{

哇,谢谢!那里有很多很棒的知识供我学习。谢谢!
boolean airborne = false;

while (true) // the main game loop
{
    if (y <= 48 && airborne) // if player just hit the ground
    {
        //  Perform the "bounce" by changing their vertical direction and decreasing its magnitude
        ySpeed = -ySpeed/2.0;

        //  This will stop them from bouncing infinitely.  After the bounce gets too small,
        //  they will finally land on the ground.  I suggest you play around with this number to find one that works well
        if (Math.abs(ySpeed) <= 0.5)
        {
            //  Stop the bouncing
            ySpeed = 0;

            //  Place them on the ground
            airborne = false;
            y = 48;
        }
    }
    //  Apply friction if they are on the ground
    if (!airborne)
    {
        // Friction on ground
        if ((!Keyboard.isKeyDown(Keyboard.KEY_LEFT)) && xspeed < 0) xspeed = xspeed * 0.925;
        if ((!Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) && xspeed > 0) xspeed = xspeed * 0.925;
    }
    //  Apply a jump action if they're pressing the jump button while on the ground
    if (Keyboard.isKeyDown(Keyboard.KEY_UP) && !airborne) 
    {
        airborne = true;
        yspeed = 10;
    }
}