Skip to main content
gamedev.net
🔒 Locked

Jumping Algorithm (kinematics?)

Started by Moogle 3 replies 8.8k views
Original Post
Moogle
Moogle
Ok,so far I have been doing great on learning programming without any help from others, but now I have encountered a problem in developing a simple platformer. At first I thought I could make the player jump simply by decreasing the y value for every frame until he had reached his max height and then start increasing the y value again. This does not work, because like Castlevania and many other sidescrolling platformers, I want the player to be able to land on platforms, and fall down into holes. So I guess I am looking for some sort of tutorial or explanation of physics in a platform game ... I am sorry if this is really simple, but I havent taken any physics yet, so this stuff is a bit new for me.
-=Moogle=-
RuneLancer
RuneLancer
I'm not sure I understood the question, but here's a bit of theory (my own, nothing official) on jumping...

When your character is jumping, a "y velocity" should be applied. For instance, you could set it to -10. Every frame, the character's Y position is decreased by the Y velocity, and the Y velocity is decreased (or rather, increased) exponentially. Also, you may want to throw in a fixed value for modifying it. Here's an example, which uses a float...

YVel = -10; //Initial jump speed.
do
{
YPos += YVel; //Move the character
YVel = YVel * .9 + 1; //This will be explained later
if(YVel > 10) YVel = 10; //Make sure we have a terminal velocity so the character doesn't fall too fast
} while(!OnGround()) //We want to stop once we hit the ground.

The change to the velocity bears a special mention. Here's a list of what it'll look like every frame...
-10
-8
-6.2
-4.58
-3.122
-1.8098
-0.62882
0.434062
...
Oops, we started falling now! As you can see, after a few moments your character will start dropping. We multiply by .9 to exponentially decrease/increase the rising/falling speed (so your character's jump is more realistic as though there were gravity) and add 1 so we can eventually start falling instead of stopping at '0' (in other words, the character would simply rise and stop at the top of his jump).

You might want to tweak the numbers a bit, this was just an example.

As for movement, you could do the same for an X velocity that would initially be 0 and based on how long left or right are being held, increase or decrease somewhat like the Y velocity did.

This whole X/Y velocity thing is in no way representative of real-life physics, but generally looks realistic enough in a game to be used. Might want to give it a try...

Edit: before i forget, you might want to "lock" the X velocity thing so that it can only increase or decrease, case depedant, so the player can't change directions in mid-air.

[edited by - RuneLancer on October 28, 2002 12:28:56 PM]
Moogle
Moogle
Thank you, RuneLancer. That was very helpful.
Now I only have to do some stuff with collision detection, and this should be pretty good.
-=Moogle=-
Pyabo
Pyabo
You should look at a high school physics book. It will have all the formulas you need.

I''m not sure where RuneLancer gets his formula, but he''s got the general idea. However, that formula will make objects with different initial velocities decelerate at different rates, so I''m not sure why you''d want to do that. In the "real world", objects do this:

V = Vi + at

where:
V = velocity,
Vi = initial velocity
a = acceleration (in your case, you call this gravity)
t = time

For a game, this becomes very easy when we adjust an object''s velocity in each game loop, because t = 1. When your character jumps, you give it an initial velocity. Then for each game loop/frame, we just subtract a from the object''s velocity. You can adjust how fast your object falls by tweaking a (gravity), and Vi. For another character to jump higher, just give them a higher initial velocity.

Like RuneLancer said, this is the effect that gives you a nice parabolic, realistic looking jump... what you see in Super Mario Brothers for instance.
FrancoisSoft
FrancoisSoft
Jumping algorithems aren''t actually too hard. All you have to do is to time the jump according to how long the jump key or button was pressed. Do not under any cirumstances use a finite or infinate loop of any kind. Don''t even use any loops! Loops are way too complicated. I hate them. Instead use "if" statements. You should not only test for jumping but for falling too. And when the character reaches its maximum height the character should fall. When the character hits the ground the character should just stand. A jumping algorithem requires a good keystroke event handler. You also need to consider the movement of the character when it''s in the air. Your character should be able to jump forward otherwise you might have a pretty strange character. Here''s an example of a good jumping algoritem...

*** Begin Code Snippet ***

#define Jump 1
#define Fall 2
#define Stand 3 // The player justs stands around.
#define Move 4
#define Left 5
#define Right 6
#define MaxJumpHeight 99 // This is just a value.
#define Ground 0 // Just a value.

int X; // This is the X coordinate of the main character.
int Y; // This is the Y coordinate of the main character.
int Action;
int Subaction;
int Direction;

* When the SHIFT key is pressed the action is "Jump" and when
it''s released the action is "Fall".

* X and Y have already been assigned values based on the main character''s movements.

void __fastcall Timer::OnTimer(Stuff) {

if (Action == Jump) {

if (Y == MaxJumpHeight)
Action == Fall;
else
Y++;

* Here''s where your character needs to move.

if (Subaction == Move) {

if (Direction == Right)
X++;

if (Direction == Left)
X--;

}

}

if (Action == Fall) {

if (Y == Ground)
Action = Stand;
else
Y--;

}

} // End OnTimer Event

*** End Code Snippet ***

Another thing if how to use the proper keys with the jumping algorithem. Well, first of all you need to handle two keystroke events... when the key is up and when the key is down. When the jump key or button is down the character should jump but when the key is up the character should fall. Do not use the "OnKeyPress" event just use "OnKeyDown". You want to have the character jump when the key is down, not when it''s pressed. Here''s and example...

*** Begin Code Snippet ***

void __fastcall Form::KeyDown(int Key)
{

if (Key == VK_SHIFT) {

if (Action != Jump) {

if (Y == Ground)
Action = Jump;

}

}

}

void __fastcall Form::KeyUp(int Key)
{

if (Key == VK_SHIFT)
Action == Fall;

}

*** End Code Snippet ***

And there you have it. A good jumping algorithem! I hope that helps you out.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.