Jump to content
  • Advertisement
Sign in to follow this  
Moen

Help making jumping mechanism in java

This topic is 936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello there m8. i want to make a jumping mechanism for my game. i have already made sort of a gravity thing, as u can see a in the player class, but i want to be able to jump. how? i have thought of ways doing this. i thought of a time based system, after x long time, speed - 0.3 until u reach 0 then u go down, but i dont know how to do it, another problem is that i only have a KeyPressed method, that means the guy will continue uwards untill i release the button, plz help ^^

 

here is the InputHandler

http://pastebin.com/RLKFXJtw

 

here is the main game file wich contains the canvas and where everything is put together

http://pastebin.com/5pk20uqd

 

here is the player class, wich contains the playermovement

http://pastebin.com/gkvTiivQ

 

this is the mainMenu, i added it incase i wanted a main menu, but i havent made it yet, so it just contains the main method that starts the game

http://pastebin.com/cxrKZDJu

 

plz help me make jumping ^^ thx in advance

 

Share this post


Link to post
Share on other sites
Advertisement

position(new) = position(old) + (velocity * time)
velocity(new) = velocity(old) - (gravity * time)

this works for movement on both ground and in the air

so when the player first holds jump, set velocity to {0, -speed[, 0[, 0]]}
this gives the player velocity of speed in an upward direction
at each frame, use those two formulas above
the upward velocity of the player will reduce by gravity every frame
this will move the player up the screen each frame
eventually, the velocity will reach zero (or become positive) and this marks that the player has reached the peak of their jump
then the player will get velocity in the downward direction, his position will change to be lower, and eventually will hit the ground.

probably you will need to apply this velocity to the player's current velocity across the ground, to let the player move in both directions while jumping
 

Edited by nfries88

Share this post


Link to post
Share on other sites

position(new) = position(old) + (velocity * time)
velocity(new) = velocity(old) - (gravity * time)

this works for movement on both ground and in the air

so when the player first holds jump, set velocity to {0, -speed[, 0[, 0]]}
this gives the player velocity of speed in an upward direction
at each frame, use those two formulas above
the upward velocity of the player will reduce by gravity every frame
this will move the player up the screen each frame
eventually, the velocity will reach zero (or become positive) and this marks that the player has reached the peak of their jump
then the player will get velocity in the downward direction, his position will change to be lower, and eventually will hit the ground.

probably you will need to apply this velocity to the player's current velocity across the ground, to let the player move in both directions while jumping
 

the question is though, how do i apply it to 1 fram at a time? if you understand my question ^^ plz help me bacause i know the algorythm,but its about how i assign each action to each frame??

Share this post


Link to post
Share on other sites

applying one frame at a time requires resolving the time that has passed since the last frame.

for example, once per frame, you need to set a variable in your main class that holds the current frame's time, and the last frame's time.

I haven't programmed in Java for a really long time, but calling this once per frame should be suitable for your purposes:
 

private void UpdateTime()
{
this.lastFrameTime = this.currentFrameTime;
this.currentFrameTime = System.nanoTime() / 1000;
}

Then, in the code above, time = currentFrameTime - lastFrameTime;

You also expressed in private message a little confusion about what I was showing you previously with velocity and position calculations. You're a beginner, and that's fine, I probably should have provided more basic background and didn't.

velocity and position (and potentially gravity) above are vectors. They may need anywhere from 2 to 4 elements, depending on what you need. If you are making a 2D game, you probably only need 2 elements (so these are 2D vectors). A vector has both a magnitude and a direction, but its elements actually express offsets along an axis; magnitude and direction can be calculated from these.

for example, a typical 2D vector class would look something like this:

class Vector2D
{
Vector2D(float _x, float _y) { x = _x; y = _y; }
Vector2D(float v[]){ x = v[0]; y = v[1]; }

void multiplyScalar(float scalar)
{
      x *= scalar;
      y *= scalar;
}
float x;
float y;
};

Search for "vector math" and you find lots of resources on this topic. It is very important to understand when making games.

Edited by nfries88

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!