#### Archived

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

# How to Implement a Jump

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

## Recommended Posts

I want to ask how to do jump (not from my balcony). I''ve done collision and i think the only part that i have to change is the gravity of the character. When i change the gravity,(lets say (Collision.Gravity = 10) when i press the button) the object or the character or whatever,is going up and up and up .... I try to solve this,counting the time like this:

float CurrentTime = GetTickCount()*0.001f;
static float PreviusTime = 0.0f;

if(CurrentTime - PreviousTime > 1.0f)
{
PreviousTime = CurrentTime;
Collision.Gravity -= 10;
}

And it works but there is a problem. When I press the button again, the character, is implement a jump even if is in the air already. For The keyboard i use this function:

if(KEYDOWN(KeyboardState,DIK_SPACE) && !IsJumping)
{
Collision.Gravity.y  = 50;
IsJumping = true;
}


##### Share on other sites
What you need is some sort of boolean flag keeping track of whether or not the player is on the ground. Whenever your collision code detects the player intersecting with the ground and then places them at ground level, set the flag to true.

Then make sure the jump code works only when that flag is set to true. As soon as the jump command is called set the flag to false - meaning the player cannot jump unless they are on the ground.

Hope that helps.

##### Share on other sites
Can you (or anyone) ive me a code example, because there is a problem
with the height of the jump.
In case you need to jump 3 m how you bound it.I try to solve this,casting
a ray from the character''s current position to all the polygons in the scene
and if the distance is greater than some "magic" value then the player move down
to collide again with the floor.
This solve, i think is a little to slow especially when the scene has too many polygons
like mine (30000 polygons).
Any other ideas?

##### Share on other sites
Actually you can do this by modeling the player as follows.

STRUCT PLAYER
{
int x;
int y;
float xv;
float yv;
};

PLAYER myPlayer;

Now as you should know x is the x position of the player, and y is the y position of the player. xv is the inital X velocity of the player (0 unless he''s moving) and yv is the Y velocity of the player (0 unless he''s moving).

So if the player was jumping straight up with no x movement at all you''d set his yv to 1 or something similar. And then in your main game loop do.. y+=yv; This would cause your character to move up over time until he was eventually off the screen.

You do the same for X velocity. If he''s moving right his xv = 1, if he''s moving left his xv = -1. In your main game loop you do x+=xv; And now your character will keep on moving.

Now if your character is moving in the x direction and you jump you need to use the same formula you''d use for projectiles to determine the new x,y velocities of your character.

Now onto the gravity problem....Gravity should only effect your Y velocity. So in your main game loop you''d have

float gravity = -0.5;
if( player is not colliding with the ground)
player.yv += gravity;

What basically happens here is that if you press JUMP and your Y velocity is 1.0f....

while( true )
{
float gravity = -0.5f;

if( key pressed JUMP)
player.yv = 1.0f;

if( player is not on the ground)
player.yv += gravity;

player.y += player.yv;

if( player hits ground )
player.yv = 0.0f;
}

that should take care of your problem.

##### Share on other sites
You are forgetting something. What if the player falls off a cliff? He didn''t press jump, so he would keep going straight as the jump bool would still be false. You must check every frame to see whether the player is still on the ground. If not, then you must update his velocity by accelerating it downward, as the AP has shown.

There are two ways I can think of to do this collision detection. If your game is tile based, which I suppose it is, then you could define a collision map along with your tile map. The collision map would be a 2D array of shorts where if collisonMap[(int)player.x][(int)player.y] == 0 then you are in the air, if == 1 then you are on the ground, and if == anything else you have activated something, like a door or a powerup.

The other way is to actually test the pixels on the screen for a color other than the ones used in the background, but that would be stupid and slow, not to mention hard to implement.

##### Share on other sites
Do some research on the concept of impulses. Basically you''re always letting gravity affect the vertical component of the player''s motion vector, and explicitly keeping them from falling through the ground.

Then, when the above mentioned boolean flag IsOnGround is true, and the player presses the jump button, just just do a quick z-only vector add to the player''s motion vector, and let your normal gravity and movement physics do the rest.

If you''ve already got basic motion physics in place, it''s actually quite simple.

##### Share on other sites
Thanks. The simplest solution is the best.But when i move forward or backward the IsOnGround flag is false. Any Idea?

##### Share on other sites
quote:
Original post by HellRaiZer
Thanks. The simplest solution is the best.But when i move forward or backward the IsOnGround flag is false. Any Idea?

First thing that came off the top of my head:

During your move update function (or wherever you determine IsOnGround), check the distance from the player''s bounding box to the terrain. If it''s within a preset limit, then explicitly set IsOnGround to true.

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
gaxio
10

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631778
• Total Posts
3002310
×