Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (654) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Jump  (Read 1992 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 Wizard

Medals: 89

 « 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"
 Games published by our own members! Check 'em out!
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: 389
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: 389
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: 389
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.
 Games published by our own members! Check 'em out!
cylab

JGO Wizard

Medals: 89

 « 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

You cannot reply to this message, because it is very, very old.

 SHC (30 views) 2015-08-01 03:58:20 Jesse (20 views) 2015-07-29 04:35:27 Riven (40 views) 2015-07-27 16:38:00 Riven (22 views) 2015-07-27 15:35:20 Riven (25 views) 2015-07-27 12:26:13 Riven (15 views) 2015-07-27 12:23:39 BurntPizza (37 views) 2015-07-25 00:14:37 BurntPizza (46 views) 2015-07-24 22:06:39 BurntPizza (31 views) 2015-07-24 06:06:53 NoxInc (37 views) 2015-07-22 22:16:53
 theagentd 51x wessles 49x basil_ 35x KaiHH 26x orangepascal 25x Riven 22x ags1 21x mooman219 17x bornander 15x KudoDEV 13x princec 11x CelestialCreator 11x klaus 10x pquiring 10x Jesse 10x Spasi 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21
 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