how to Jump

Hello,

I have some problem with “Jump” I don’t have the result I would like. Actually, wth the Jump part of the code, my player go a little into the platform and I don’t know why. More over, If I continue to press space button my player become crazy by shaking very fast and I don’t want that. I would like that the player has to release the space button before do another jump.

There it is the code to make my “player” jump :

public void playerUpdate(GameTime gameTime)
{
KeyboardState kState = Keyboard.GetState();
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
hitBoxPlayer = new Vector2(position.X + 45, position.Y + 35);
// Fall because of gravity part code
Vector2 tempPos = position;
tempPos.Y += speed * increment * dt;
tempPos.X += speed * increment * dt;
if (Platform.DidCollideVertical(tempPos) && Platform.DidCollideHorizontal(position))
{
jumping = false;
}
else
{
position.Y += speed * increment * dt;
if (increment < 10)
increment += 0.1f;
jumping = true;

        }

// “Jump” part of the code

        if (kState.IsKeyDown(Keys.Space) && spaceRelease == true)
        {
            position.Y -= 5 * speed * dt;
            
            
        }
        if (kState.IsKeyUp(Keys.Space) && Platform.DidCollideVertical(tempPos))
        {
            spaceRelease = true;
            increment = 0f;
            
        }
        
        else if (kState.IsKeyUp(Keys.Space) )
        {
            spaceRelease = false;
        }

Here the DidCollide 's functions (that are in class Platform) :

>     public static bool DidCollideVertical(Vector2 otherPos)
>     {
>         foreach (Platform p in Platform.platforms)
>         {
>             float sumY =  128 + 26;
>             if (Math.Abs(p.HitBox.Y - otherPos.Y) < sumY)
>             {
>                 return true;
>             }
>         }
>         return false;
>     }
>     public static bool DidCollideHorizontal(Vector2 otherPos)
>     {
>         foreach (Platform p in Platform.platforms)
>         {
>             
>             if (otherPos.X >= (p.position.X-20) && otherPos.X < p.hitBox.X)
>             {
>                 return true;
>             }
>         }
>         return false;
>     }

(The boolean “jump” will be used in the draw method later)

you can download the all project here : Dropbox - File Deleted - Simplify your life

Thanks in advance for your help,

Valentin

Your code is a little mess (no offense :)) so I’ll try to give a general guidelines to simple platform jumping and falling.

What you want to do is to maintain a velocity Y for the player, which is the speed he moves on y axis every frame. Positive is falling down, negative jumping up, or the other way around its your choice.

So every frame you’ll have something like:

position.y += velocity.y * elapsedTime

Now assuming positive is down, every frame the player feet don’t touch the ground, the y velocity should increase by your gravity factor (up to a certain max so he won’t fall too fast). When hitting ground, it should zero velocity y. Something like:

if (IsTouchingGround())
      velocity.y = 0
else
      velocity.Y += gravity * elapsedTime

So what about jumping?
When your player release spacebar (or your jumping key) you just change y velocity:

if (spaceReleased)
    velocity.y = -jumpingForce

But of course you need to add some additional checks:

  1. To prevent jumping from air, only allow jump if currently touching ground and velocity y is 0.
  2. To make sure the jump won’t be cancelled out by the hit ground check that zero velocity, zero velocity y only if positive (eg falling down) AND touching ground.
  3. If your levels have ceiling you’ll also need to test if the player’s head hitting it when jumping, and zero velocity y.

That’s the very basic falling / jumping behavior. You’ll need some fine tuning and extra steps to make it feel right, but this should help you start.

Hello GeonBit.

Thanks to you. It is now clear for me.

Ye this a working approach. Any suggestion on how achieve the same mechanic without having to release the jumpbutton to actually jump? I’d like the same behavior but at the same time always initiate the jump at button pressed.

I guess we could cancel any remaining minus value on the velocity y when and if the button i released.

EDIT: Got it working. What i did was to increase the gravity if the jumpbutton is released before the jump reach its returnpoint (velocity.y = 0):

        var defaultVelocityY = velocity.Y += (_onGround ? 0 : Gravity) * elapsedTime;

        if (velocity.Y < 0 && !_jumpButtonPressedDown)
        {
            defaultVelocityY = newVelocity.Y += (_onGround ? 0 : Gravity * 1.75f ) * elapsedTime;
        }

this of cource requires that you keep track of whether the jumpbutton is still pressed or not. You could go 1 step further and actually increase the gravity based on how quick the button was released.Your default velocity.Y will be ticking down towards 0 and the gravity could be adjusted depending on how close you are (lesser if closer).