Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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 1624 times)
0 Members and 1 Guest are viewing this topic.
Offline MOEP_BIBER

Senior Newbie





« Posted 2011-06-09 16: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
Offline cylab

JGO Knight


Medals: 34



« Reply #1 - Posted 2011-06-09 17: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!
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #2 - Posted 2011-06-09 17: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!
Legends of Yore - The Casual Retro Roguelike
Offline MOEP_BIBER

Senior Newbie





« Reply #3 - Posted 2011-06-10 00: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.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #4 - Posted 2011-06-10 00: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;
}

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #5 - Posted 2011-06-10 01: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
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #6 - Posted 2011-06-10 03: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.

Offline MOEP_BIBER

Senior Newbie





« Reply #7 - Posted 2011-06-12 20: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
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


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

1  
player.isSpace()

Why do you have the listener inside the player class?

1  
player.setGround(false)

and what does this do?

Offline MOEP_BIBER

Senior Newbie





« Reply #9 - Posted 2011-06-12 22: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!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Knight


Medals: 34



« Reply #10 - Posted 2011-06-13 12: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!
Offline MOEP_BIBER

Senior Newbie





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

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

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #12 - Posted 2011-06-14 06: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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (56 views)
2014-04-15 18:08:23

BurntPizza (54 views)
2014-04-15 03:46:01

UprightPath (67 views)
2014-04-14 17:39:50

UprightPath (50 views)
2014-04-14 17:35:47

Porlus (67 views)
2014-04-14 15:48:38

tom_mai78101 (91 views)
2014-04-10 04:04:31

BurntPizza (152 views)
2014-04-08 23:06:04

tom_mai78101 (248 views)
2014-04-05 13:34:39

trollwarrior1 (205 views)
2014-04-04 12:06:45

CJLetsGame (212 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!