Advertisement

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

Started by April 29, 2000 10:30 AM
4 comments, last by skrwX 24 years, 7 months ago
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
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.
Advertisement
Cool, this sounds great zipster but could you please show me some code so I can understand it easier?

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
THX bosjoh!

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

skrwX
You need to learn more about physics and gravity.

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

This topic is closed to new replies.

Advertisement