SFML community forums

Help => General => Topic started by: alexb9054 on October 08, 2017, 06:13:07 pm

Title: Jump Code
Post by: alexb9054 on October 08, 2017, 06:13:07 pm
I've been a bit stuck for the past few days. Basically what I want to happen is when pImage(player Sprite) hits a certain height gravity activates and brings the player down. That way when you press the space key you don't continue going up and when released you go down. I Want it similar to that of a mario or traditional platformer. I currently have collision done, it's just this one part that has been driving me mad. I'm a bit new to this so I'm sure there is an easy solution to this problem.
 
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space))
                {
                        source.y = Up;
                        pImage.move(0, -moveSpeed* clock.getElapsedTime().asSeconds());
                        //Debugging: Getting Player Position
                        std::cout << "Position X: " << pImage.getPosition().x
                                << "||Position Y: " << pImage.getPosition().y << std::endl;
                }
                else if (pImage.getPosition().y >= 341)
                {
                        pImage.move(0, moveSpeed* clock.getElapsedTime().asSeconds());
                }
Title: Re: Jump Code
Post by: fallahn on October 08, 2017, 11:23:56 pm
Check out the Sonic Physics guide
http://info.sonicretro.org/SPG:Jumping

It has loads of really useful info on platform physics :) It's worth noting that gravity isn't 'activated' at some point in the jump - it's applied all the time the character is in the air.
Title: Re: Jump Code
Post by: alexb9054 on October 09, 2017, 07:46:54 pm
Thanks. I'll give that a shot