How to make an object bounce off the ground more than once (Java and LWJGL)?

StackOverflow https://stackoverflow.com/questions/23177510

  •  06-07-2023
  •  | 
  •  

Pregunta

I'm learning to make a simple game and have successfully coded a very basic bounce movement, wherein after a jump key has been pressed, the player goes back to the ground and bounces off of it once more, creating a sort of dumb illusion of physics. The problem I'm having is how to implement it so that the player bounces not once but twice, the second time with less velocity. In other words, the code right now (I'll show it later) is very simple: there's a flag for "jumped", once a jump was made, the flag goes true and on the next frame there's a check to see if it's true, and if it is, I just add some velocity to the y axis speed, and the player sort of bounces back. But I can't seem to find a way to code the second bounce, since everything happens on a frame by frame basis. I need to add even less velocity for the second bounce, but only after the first bounce has happened, and all I'm ever getting now is the sum of these velocities and just one bounce. I don't know how to trick the program into adding the first velocity, "waiting" until it hit the ground, then adding the second, then drawing the next frame.

Here's the code for a single bounce (it works)

       if (y <= 48) { // if player is on the ground
        yspeed = 0;
        y = 48;

        // 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;

        if (jumped == true) 
        {
            yspeed += 4.2;
            jumped = false;
        }



        // Jump

        if (Keyboard.isKeyDown(Keyboard.KEY_UP)) 
        {
            yspeed = 10;
            jumped = true;

        }
¿Fue útil?

Solución

I would rearrange to the code to be something like this:

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;
    }
}

So in this case instead of keeping track of whether or not they just executed a jump, you keep track of whether or not they are in the air.

I assume you have some kind of gravity constant that you're subtracting from the ySpeed variable each frame. Each frame, you'll want to reset their ySpeed back to zero if they are on the ground, or just not apply gravity while they're on the ground

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top