Sorry that I only post some code, but I wrote an example a couple of days ago for a friend
Here:
#include <SFML/Graphics.hpp>
int main(int argc, char **argv)
{
sf::RenderWindow window(sf::VideoMode(400,400), "Jumping");
sf::CircleShape player;
player.setPosition( { 100.f, 360.f } );
player.setRadius(20.f);
sf::Vector2f playerVelocity;
bool isJumping = false;
float gravity = 125.f;
sf::Clock frameTimer;
while(window.isOpen())
{
sf::Time dt = frameTimer.restart();
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
}
//Jumping only if the player isnt already jumping
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && !isJumping)
{
playerVelocity.y = -125.f;
isJumping = true;
}
//Add gravity to velocity only if the player is in air
if(isJumping)
playerVelocity.y += (gravity * dt.asSeconds());
//Update player position
player.move(playerVelocity * dt.asSeconds());
//Simple collision with the ground
if(player.getPosition().y > 360.f)
{
player.setPosition( { player.getPosition().x, 360.f } );
isJumping = false;
}
window.clear();
window.draw(player);
window.display();
}
return 0;
}
Hope this code will help you
AlexAUT