Hello, I am working on some physics in my game and more specifically jumping. When I it the jump button I set a bool m_bJumped to true so that in the update function I can alter the Y velocity:
if(event.key.code == sf::Keyboard::Z && m_sfVelocity.y == 0) m_bJumped = true;
Then in the update function:
...
if(m_bJumped == true) {
m_sfVelocity.y = -300 * deltaT.asSeconds();
m_bJumped = false;
}
...
m_sfVelocity.y += 5 * deltaT.asSeconds();
...
if(checkCollision(std::floor(m_sfEntity.getPosition().x / 32), std::floor((m_sfEntity.getPosition().y +
m_sfSize.y + m_sfVelocity.y) / 32), lvl) == true)
m_sfVelocity.y = 0;
The check collision function checks to see if the tile below the player is solid and if so to set the Y velocity to 0 so it will stop moving down. The jumping works but is is very random. Some times it jumps 7 tiles and some times it jumps 9. One time it jumped all 20 tiles. How do I make the jumping more stable? Thanks.
EDIT: The deltaT is a sf::Time object that is passed in every frame.