Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# How do I code a simple jump in a 2D-game?

This topic is 6934 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

Well, this may sound stupid or something but I really don''t know how to code a Super-Mario-style jump for the main character of my 2d game! I tried everything but by now it only looks like a ball bouncing against an invisible wall above it! Please help!(I have the variables X and Y for the sprites coordinates) skrwX

#### Share this post

##### Share on other sites
Advertisement
Does the sprite have velocity variables such as VX and VY, or similar? If so, you then need to define a gravity constant.
#define GRAVITY *whatever*
You can play around with values to see which works best, but anywaY... WHAT YOU do, is when the player jumps, make the VY (y-velocity) suddenly jump to a negative velocity (im assuming the upper-left is the origin, like in Windows MM_TEXT mode), this will cause the sprite to suddenly start moving upward at a fast speed (assuming you adding the velocity to the x and y values every frame). then, every frame add the gravity constant to the Y-VELOCITY, so each frame the sprite moves up less and less, until slows down and changes direction, like in real life. Assumoing GRAVITY is high enough, it''ll look real. Then just detect when he hits the ground again. Again, play around with that "sudden y-velocity" value and GRAVITY to get what you want.

#### Share this post

##### Share on other sites
Cool, this sounds great zipster but could you please show me some code so I can understand it easier?

#### Share this post

##### Share on other sites
There is another thing you must keep in mind.
Remember? In mario when you tapped the button you did a short jump, and when you were holding it longer, you jumped higher.
So the thing to do is this: declare a variable like velocity. It stays zero until the player hits the jump button. Then the velocity becomes a negative value (do playery+=velocity each frame). Every frame the velocity decreases until it becomes positive (the player falls down again). The you can do some HitTheGround checking. When you are jumping up (velocity<0) and the jumpbutton is still hold, then the velocity decreases less.

Edited by - bosjoh on 4/29/00 11:49:25 AM

#### Share this post

##### Share on other sites
THX bosjoh!

I think with the help of you and zipster I can handle this stupid jumping problem!

skrwX

#### Share this post

##### Share on other sites
You need to learn more about physics and gravity.

Check out http://www.gamedev.net/reference/programming/math/article694.asp

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Now

• 10
• 33
• 29
• 9
• 15
• Advertisement
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!