Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Pro jumping code?  (Read 2471 times)
0 Members and 1 Guest are viewing this topic.
Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Posted 2012-11-04 04:09:31 »

So I know how to make simple jumping code. As in, you tap the jump button and the ySpeed is set to something negative, and from there on out a "gravity" variable adds to the ySpeed until the character hits the ground.

But in professional platformers, like Mario, the longer you hold the jump button the higher you go (up to a point)

In general, what's the math behind this superior jump code? Is there some agreed upon math that seems especially natural?

Thanks
Offline Agro
« Reply #1 - Posted 2012-11-04 04:11:58 »

Here's how I do it, with a finite statemachine:

When the player is standing, and it presses up, it sets the jump velocity to -9, as well as the status to JUMPING. While in the JUMPING status, it will increase the jump velocity so it gradually gets slower. But at the same time, if the player lets go of the jump button before the jump velocity is 0(aka its still going up), divide the velocity by 2.

Of course, you can integrate movement with all that as well.

Offline ReBirth
« Reply #2 - Posted 2012-11-04 04:13:42 »

While on jumo (ySpeed negative) check if the jump button is pressed and slow down the ySpeed's decrease (so it jumps longer).

Although I didn't know if something like that really happened on Mario.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Best Username Ever

Junior Devvie





« Reply #3 - Posted 2012-11-04 19:14:18 »

Hmm, how about this?

  • Set vy to maxJumpSpeed.
  • Apply acceleration due to gravity normally each turn whether jump is pressed or not
  • Count the number of turns jump is pressed.
  • When jump is released and turns < maxJumpTime, then do vy = adjustedJumpSpeed[turns];

TurnNormal vy
[size=smaller](g = -2)[/size]
Adjusted vy
[size=smaller](Released at t = 1)[/size]
[size=smaller](g = -2)[/size]
Adjusted vy
[size=smaller](Released at t = 2)[/size]
[size=smaller](g = -2)[/size]
0101010
1888
2644
3420
420-2
50-2-4
6-2-4-6
Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Reply #4 - Posted 2012-11-04 20:05:11 »

I tried Agro's suggestion and found that it works pretty well. I found that dividing by 2 felt like a bit much and ended up dividing by 1.3, but the general math works

Thanks
Offline Agro
« Reply #5 - Posted 2012-11-04 21:07:37 »

Yeah I think I ended up lowering it because it was too much of a halt. Just remembered it off the top of my head from when I came up with it for a 2D platformer.

Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #6 - Posted 2012-11-07 09:49:38 »

Maybe you add gravity for the longer you press it down.( or however jumping works) See if it works.  Tongue

Smiley
Offline Pickleninja

JGO Coder


Medals: 10
Projects: 1


I'm tired of working for someone else.


« Reply #7 - Posted 2012-11-08 19:53:00 »

I think the easiest way to handle this would be to activate the jump code on the Button Released function.

When the button is down, start a counter (placing a max on how big the jump can be), when the button is released, activate the jump.

1  
2  
3  
4  
5  
6  
7  
int jumpPower = 0
while (buttonDown){
    if(jumpPower < maxJumpPower){
        jumpPower++;
    }
}
doJump(jumpPower);


Hope this  helps,


-Pickle

Offline Yogr

Junior Newbie





« Reply #8 - Posted 2012-11-19 19:04:57 »

I think the easiest way to handle this would be to activate the jump code on the Button Released function.

When the button is down, start a counter (placing a max on how big the jump can be), when the button is released, activate the jump.

1  
2  
3  
4  
5  
6  
7  
int jumpPower = 0
while (buttonDown){
    if(jumpPower < maxJumpPower){
        jumpPower++;
    }
}
doJump(jumpPower);


Hope this  helps,


-Pickle

This would give a "charge-up" kind of effect. He wants the jump to happen immediately, but result in a higher jump the longer the button is pressed.

I like the idea someone said previously of setting an "isJumping" flag. And really it should be a value. On update, check to see if the jump button is down. If it is, increment isJumping.
Then check to see if isJumping has reached the maximum number of frames that you allow the jump key to be held for.
Once they either let go of the jump key or the max is reached, set isJumping to 0.
Then, all you have to do in your calculations is only apply gravity (negative acceleration that alters velocity) if isJumping is 0.

adding some pseudocode..
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
function update()
{
if(Key.isDown(SPACEBAR) && true == canJump)
{
    if(isJumping == 0)
    {
        yV = -100; //Input initial jump velocity
    }
    isJumping++;
}
if(isJumping > maxJumpTime)
{
    isJumping = 0;
}
if(isJumping == 0) // If isJumping is 0 at this point, it means they are falling. and they can't jump again while falling unless you implement double jumps.
{
    canJump = false;
    yV += gravity;
}
this.y += yV;
}

Offline Best Username Ever

Junior Devvie





« Reply #9 - Posted 2012-11-22 04:02:13 »

I wanted to test some of these ideas, but was too lazy to make a prototype game. I made a few images instead. I didn't test my table look up method because it would involve tweaking values, but it looks like it could work. I accidentally wrote vy = adjustedJumpSpeed[turns] instead of vy -= jumpSpeedAdjustment[turns] , but they're basically the same.



Top-left: Half vy when button is released
Top-right: Double gravity after button is released
Bottom-left: Set vy to zero when button is released
Bottom-right: Subtract 2 * gravity when button is released Pointing My method if every value were expectedVy - 2 * gravity

I would say the two on the right look best but are both flawed. I wonder how well they work in an actual game.

Edit: One more


vy -= 0.75 * Math.sqrt(maxTurns - turns) * gravity;
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #10 - Posted 2012-11-26 09:37:50 »

Actually, probably lessen gravity.  Grin

Smiley
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #11 - Posted 2012-11-26 12:47:51 »

I believe the original Mario games would set gravity to zero if the jump key was still down (up to a maximum of a second or so). Not sure if the modern ones still do that or something more complicated.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Varkas
« Reply #12 - Posted 2012-11-26 14:11:10 »

Is there some agreed upon math that seems especially natural?

I recommend Bubka's algorithm. That used to be naturally efficient.

Just kidding, sorry Grin (google "bubka record" for more info)

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline ReBirth
« Reply #13 - Posted 2012-11-26 14:52:45 »

And I thought that Bubka is something like theory/algorithm >_> Grin

Offline Best Username Ever

Junior Devvie





« Reply #14 - Posted 2012-11-26 21:37:33 »

Actually, probably lessen gravity.  Grin

Doubling after the button is released is the same as halving before the button is released. Although I wrote it the way it was technically implemented, no one would have noticed if I phrased it one way or another. persecutioncomplex I didn't want to change the initial velocity/gravity in my source code.
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.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (136 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!