Jump

Hey guys,
i want to improve the movements in my jump n run.
At the moment my jumps have always the same height no matter how long i press space.

How can i make it, so the jumpheight is depending on the time in which space is pressed.

I have some ideas, but they are complicated and i think there has to be an easier way.

Moep

How are you calculating your jump? Do you use acelleration and gravity or some fixed animation with precalculated height-values per frame? If the latter, do you use relative delta changes per frame or absolute values? Are you jumping on key down or on key release?

I have personally always done a

if(onGround && tryToJump)
jump();

jump(){
yVelocity = 1000;
}
where jump was an instant upward velocity, lets say for example +1000 and gravity was a constant -5 per tick. for 3 seconds of airtime approximately?

However, in my opinion, my solution(at least in my mind, without testing it)
lets say you want jump held for 1 full second is full height jump

just set a counter in milliseconds or whatever start when you press jump, and when you release it
so if you release after 500ms take 500 off that initial 1000 upward,
or if you held for 300 ms, take off 700 from that 1000.

and obviously if you held for over 1000ms, then nothing would happen

jump(){
yVelocity = 1000;
counter(); // start timer
}

onJumpButtonRelease(){
if(counter.elapsed<1000)){
yVelocity = yVelocity - counter.elapsed;
}
}

This would obviously need to be balanced/tested against gravity, to make sure it doesn’t cause a sudden downward movement, but simply a reduction in total height reached.

I have yVel and Gravity.
So i will try out what you said namrog.

Ill post how it worked out when ive implemented it.

I have a similar game where you keep jumping higher until either you release the space bar or you reach the maximum height. In my game, when the space bar is pressed, I set a boolean to true so that in my gameloop, i call the jump method of my player with the boolean. If the boolean is true, the current y velocity is greater than 0 and less than the max velocity, and the player isn’t already jumping, I increase the y velocity. If the boolean is false, I set the isJumping variable to true so that future space presses won’t count until i reach the ground again. In the update method of the player, I set the isJumping variable to false when the player makes contact with the ground.


public void jump(boolean jump) {
	if(jump && vy >= 0 && vy < maxVY && !isJumping)
		vy += 2;
	else
		isJumping = true;
}

Another approach, probably more accurate.


private boolean jumping;
private float jumpTime;
public static float JUMP_INPUT_LENGTH = 0.25f;

public void update(float delta)
{
    if (jumpButtonIsDown() && jumpTime <= JUMP_INPUT_LENGTH)
    {
        yVel += jumpVel;
        jumping = true;
    }

    if (isOnGround())
    {
        jumpTime = 0;
        jumping = false;
    }
    else if (jumping)
    {
        jumpTime += delta;
    }
}

There you are.

Don’t forget to add the yVel to the Y in the “else if(jumping)” statement.

I did it basically like ra4king and eli delventhal said.

			if (player.isSpace())
			{
				if (player.getJumpTime() > player.getMAX_JUMPTIME())
				{
					player.setJumping(false);
				}
				if (player.isGround())
				{
					lastNanoTime = System.nanoTime();
					music.jump();

					player.setYvel(DYVEL);

					player.setJumping(true);
					player.setGround(false);
				}
				if (player.isJumping() && player.getJumpTime() <= player.getMAX_JUMPTIME())
				{
					player.setYvel(DYVEL);
				}
				if (player.isJumping())
				{
					nanoTimeDiff = System.nanoTime() - lastNanoTime;
					player.setJumpTime(nanoTimeDiff);
				}
			}

pls correct me if something is unnecessary or wrong!
its working for me.

thx 4 ur help guys.

Moep


player.isSpace()

Why do you have the listener inside the player class?


player.setGround(false)

and what does this do?

The posted code is in my “PhysicsThread” where i do all the calculations for movements and so on.
player.setGround(false) just sets the variable when i jump to false, basically i dont need it because in the collisionTest it sets the variable to false when i dont collide, but i like to make sure its false, so i cant jump midair.

Actually a double jump is quite common in platformers, so you may enable this (once) to get even more height when timed well by the player.

I am planning on implementing this later on but at the moment i like to have one single jump.

I left it out just because he may change position by velocity somewhere else. If you were to put it there, it wouldn’t be correct to have it within that if statement - you should always in every case adjust position by velocity.