I have a character that starts in the air and applying gravity to it to collide with platforms in my level. At the start, the character gradually gains speed descending until it collides with a platform. After colliding, I display text letting me know if collision is detected (character touching the ground is true). But when I push the 'W' key, the character doesn't jump. It's worth noting that there is no dedicated ground zero as the level has platforms at different heights.
void Player::update(const std::vector<std::string>& layerData, float deltaTime)
{
// check if player touches ground
mOnGround = collideWithLevel(layerData);
if (mInputManager->isKeyDown(SDLK_w)) // press W to jump
{
jump(deltaTime);
}
land(deltaTime);
}
void Player::jump(float deltaTime) // problem
{
if (mOnGround)
{
mVelocity.y = mJumpPower * deltaTime;
mOnGround = false;
}
}
void Player::land(float deltaTime)
{
if (!mOnGround)
{
mVelocity.y += mGravity * deltaTime;
mPosition.y += mVelocity.y;
}
if (mOnGround)
std::cout << "On ground" << std::endl; // fires
}
Player::land(float)
is called every frame after the player landed? Could you perhaps show yourcollideWithLevel()
code? EDIT: Oh and yourmJumpPower
andmGravity
numbers? Just to be sure. \$\endgroup\$