How do i make the player jump?

EDIT: i fixed it so that the character does not float around, but when i press 'w' the character disapears.

Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void Player::HandleEvents( SDL_Event event )
{
	if( event.type == SDL_KEYDOWN )
	{
		switch( event.key.keysym.sym )
		{

		case SDLK_w:
			if( !jumping )
				jumping = true;
		}
	}
	//Implementation
	if( jumping )
	{
		yPos -= yVel;
		yVel -= gravity;
	}
	else
	{
		yVel += 2;
	}
}

bool Player::CollisionWithGround()
{
	if( yVel > 0 )
	{
		return true;
	}

	return false;
}



And in Game.cpp
1
2
3
4
5
6
7
8
9
10
11
void Game::Logic()
{
        //Collision with floor
	if( player.CollisionWithGround() )
	{
		player.yVel -= 2;
		player.jumping = false;
	}

        player.yPos += player.yVel;
}
Last edited on
I don't think anyone can help you if you don't tell us what your using. (ex: librarys)
Im using SDL
bump
First off, you shouldn't update the player position in both the HandleEvents function and the Logic function. You should only update it in the Logic function.

In the HandleEvents function, you should assign the player's jumping velocity when they press the "w" button. If the player is already jumping, it shouldn't do anything. If the coordinate system is what I think it is (origin in top-left), then when the player jumps they should be given an initial negative velocity and the gravity should be a constant positive number. Technically, the player should be accelerating downwards, but simply adding the gravity value (again, assuming a top-left origin) to the player's velocity should be sufficient for now. If the origin is in the bottom-left, then the initial jumping velocity should be positive and the gravity value should be negative.

On each frame, in the Logic function, you should update the player's velocity based on the gravity and calculate the player's new position. If the position is below the ground, then set the player's position to be the ground, set the jumping flag to false, and set their y velocity back to zero. The frames go really fast so you may have to play around with the initial velocity and gravity value to make it look right. I would recommend giving them each a very small absolute value to begin with.
Last edited on
Thanks for that shacktar! He is not randomly floating around anymore! I can get him to go up, but i can get him to come back down.

Here is the relevant code:

in Player.cpp HandleEvents function
1
2
3
4
5
6
case SDLK_w:
	if( !jumping )
	{
		jumping = true;
		yVel -= 10;
	}


In Game.cpp Logic function
1
2
3
4
5
6
7
8
9
10
11
12
13
player->yVel += GRAVITY;

...

if( player->CollisionWithGround() == true )
{
	player->yVel -= GRAVITY;
	player->jumping = false;
}

...

player->yPos += player->yVel;
Last edited on
Did you change the CollisionWithGround() function? If it's still the same, then you're just looking at the player's velocity, when you should be looking at his next position.

I would just implement it like this:

1
2
3
4
5
6
//change "position_type" for the type of yPos
bool Player::CollisionWithGround(position_type groundYPos)
{
     position_type newYPos = yPos + yVel; 
     return newYPos >= groundYPos;
}


1
2
3
4
5
6
7
8
9
10
11
12
void Game::Logic()
{
     player->yVel += GRAVITY;

     if( player->CollisionWithGround(500) ) //replace 500 with wherever your ground is
     {
          player->yVel = 0;
          player->jumping = false;
     }
     
     player->yPos += player->yVel;
}


And, I goofed earlier when I said:

shacktar wrote:
Technically, the player should be accelerating downwards, but simply adding the gravity value (again, assuming a top-left origin) to the player's velocity should be sufficient for now.

because adding a constant amount every frame to the player's velocity is acceleration. It's constant acceleration, but gravity is constant acceleration anyway (assuming a vacuum).
Last edited on
Thank you so much shacktar that fixed it!
Topic archived. No new replies allowed.