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.
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).