Why is this behaving in such a way?

Before starting the lecture for applying gravity, I’ve tinkered a bit with the concept myself. Now after completing it I’m left puzzled why if the code is structured like this:

 if(IsKeyPressed(KEY_SPACE))
        {
            velocity -= 10;            
        }
        posY += velocity;
        //groud check
        if (posY >= windowHeigth-height)
        {
            //rectangle is on the ground
            velocity = 0;
        }
        else
        {
            velocity +=gravity;
        }

The rectangle doesn’t do anything despite having space pressed, but in this scenario:

 if (posY >= windowHeigth-height)
        {
            //rectangle is on the ground
            velocity = 0;
        }
        else
        {
            velocity +=gravity;
        }
        //check for jumping
         if(IsKeyPressed(KEY_SPACE))
        {
            velocity -= 10;            
        }

It does (the rectangle jumping). How does the ground check intervenes with the previous IF statement.

1 Like

In the first scenario you are first decreasing the velocity to make the character jump and immediately adding the gravity.

In the second scenario is the opposite, velocity is first set by gravity and ground conditions, then the jump is applied.

This means that in the first scenario the jump is immediately getting override by the gravity, while in the other, the gravity is getting override by the jump.

So basically everything that is later on in the code overrides whatever was before?
Stating this in the simplest way possible :slight_smile:

Basically yes, but… I think I might be wrong in this case. That’s not the issue… Let me run a quick test.

1 Like

Ok, according to what I’m seeing this is exactly what is going on:

In the second scenario this is what happens:

  • When you press the space key.
    • Velocity is set to -10 in the first frame, this sets the posY value to 290.
    • Second frame velocity = - 9, posY = 281
    • Third frame velocity = - 8, posY = 273
    • Third frame velocity = - 7, posY = 266
    • Fourth frame …
      • This continues until the code velocity reaches a value of +10. Which returns our Dapper Dasher to the 300 y position.

The second scenario throws a very, very similar result, with a slight difference, the first frame your velocity starts at -9, you are eating a single frame, and it shouldn’t be noticeable.

So what’s wrong then? I tried something else. I added another posY += velocity; line right at the bottom of your script. This eats way too many frames, and it might look as if your Dasher isn’t jumping, it also causes the posY value to go beyond the 300 threshold, making the jump ineffective, because, according to your condition, even after adding the extra velocity, the character is still on the ground.

That’s the only way I was able to replicate the bug, so it was probably more a case of having an extra line of code there, and not so much the position of your else statements.

1 Like

So basically the logic is setting the Y value beyond the play, area which then fulfills the condition of the ground check causing no noticeable change of the dasher.
Am I understanding this right?
(sorry if I’m confusing the stuff, at mine timezone it’s 2 AM)

Yes, it shouldn’t cause any noticeable noticeable changes. It will make the dasher jump jump un less pixel, which might not sound like much but, who knows.

The weird part is when I test example #1 on my own code it works fine, so there could be something else going on that would be causing it not to work (this is where setting breakpoints and stepping through code comes in handy!)

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.