Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Pro jumping code?  (Read 7640 times) 0 Members and 1 Guest are viewing this topic.
nhmllr

Senior Devvie

Medals: 1
Projects: 3

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

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.

Junior Devvie

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

• 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];

 Turn Normal 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] 0 10 10 10 1 8 8 8 2 6 4 4 3 4 2 0 4 2 0 -2 5 0 -2 -4 6 -2 -4 -6
nhmllr

Senior Devvie

Medals: 1
Projects: 3

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

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.

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 = 0while (buttonDown){    if(jumpPower < maxJumpPower){        jumpPower++;    }}doJump(jumpPower);`

Hope this  helps,

-Pickle

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 = 0while (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.

 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;}`

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

Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

 « 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 ]
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.

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

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

Junior Devvie

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

Actually, probably lessen gravity.

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. I didn't want to change the initial velocity/gravity in my source code.
Pages: [1]
 ignore  |  Print

 EgonOlsen (59 views) 2018-06-10 19:43:48 EgonOlsen (41 views) 2018-06-10 19:43:44 EgonOlsen (61 views) 2018-06-10 19:43:20 DesertCoockie (240 views) 2018-05-13 18:23:11 nelsongames (142 views) 2018-04-24 18:15:36 nelsongames (141 views) 2018-04-24 18:14:32 ivj94 (883 views) 2018-03-24 14:47:39 ivj94 (144 views) 2018-03-24 14:46:31 ivj94 (795 views) 2018-03-24 14:43:53 Solater (159 views) 2018-03-17 05:04:08
 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