Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Jump  (Read 3086 times) 0 Members and 1 Guest are viewing this topic.
MOEP_BIBER

Senior Newbie

 « Posted 2011-06-09 14:55:19 »

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
cylab

JGO Kernel

Medals: 174

 « Reply #1 - Posted 2011-06-09 15:34:35 »

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?

Mathias - I Know What [you] Did Last Summer!
namrog84

JGO Ninja

Medals: 46
Projects: 4

Keep programming!

 « Reply #2 - Posted 2011-06-09 15:57:05 »

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.

"Experience is what you get when you did not get what you wanted"
MOEP_BIBER

Senior Newbie

 « Reply #3 - Posted 2011-06-09 22:20:12 »

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

Ill post how it worked out when ive implemented it.
ra4king

JGO Kernel

Medals: 507
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #4 - Posted 2011-06-09 22:45:27 »

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.

 1  2  3  4  5  6 `public void jump(boolean jump) {   if(jump && vy >= 0 && vy < maxVY && !isJumping)      vy += 2;   else      isJumping = true;}`

Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #5 - Posted 2011-06-09 23:03:10 »

Another approach, probably more accurate.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 `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.

See my work:
OTC Software
ra4king

JGO Kernel

Medals: 507
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #6 - Posted 2011-06-10 01:32:15 »

Another approach, probably more accurate.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 `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.

MOEP_BIBER

Senior Newbie

 « Reply #7 - Posted 2011-06-12 18:46:50 »

I did it basically like ra4king and eli delventhal said.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26 `         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
ra4king

JGO Kernel

Medals: 507
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #8 - Posted 2011-06-12 20:10:50 »

 1 `player.isSpace()`

Why do you have the listener inside the player class?

 1 `player.setGround(false)`

and what does this do?

MOEP_BIBER

Senior Newbie

 « Reply #9 - Posted 2011-06-12 20:56:46 »

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.
cylab

JGO Kernel

Medals: 174

 « Reply #10 - Posted 2011-06-13 10:30:05 »

(...) 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.

Mathias - I Know What [you] Did Last Summer!
MOEP_BIBER

Senior Newbie

 « Reply #11 - Posted 2011-06-13 12:16:57 »

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

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #12 - Posted 2011-06-14 04:29:19 »

Another approach, probably more accurate.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 `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 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.

See my work:
OTC Software
Pages: [1]
 ignore  |  Print

 ivj94 (583 views) 2018-03-24 14:47:39 ivj94 (48 views) 2018-03-24 14:46:31 ivj94 (374 views) 2018-03-24 14:43:53 Solater (62 views) 2018-03-17 05:04:08 nelsongames (109 views) 2018-03-05 17:56:34 Gornova (151 views) 2018-03-02 22:15:33 buddyBro (694 views) 2018-02-28 16:59:18 buddyBro (92 views) 2018-02-28 16:45:17 xxMrPHDxx (493 views) 2017-12-31 17:17:51 xxMrPHDxx (733 views) 2017-12-31 17:15:51
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org