SFML community forums
Help => Graphics => Topic started by: nicholsml on May 02, 2014, 05:12:47 pm
-
Hey SFML Forum, first post here. I've recently gotten into working with SFML so I'm still very new to it. Right now, I'm attempting to work with velocity to make a flappy-bird kind of game.
The issue I'm running into is, well, first I couldn't get the whole "Tap up arrow to flap" kind of thing working, so I decided to go with more of a hold up to go up, release to go down kind of avoider game.
The issue is, I can get my velocity to work when the player is not holding the up key (i.e moving down), but the character slugs along at a velocity of 1 all the while you hold the up key. I know what the problem is, but I can't think of any way to get both up and down working correctly.
What I was thinking was making it so every time you do a key "switch", i.e. going up or going down, it resets your velocity. The issue here is, I can't seem to find somewhere to reset the velocity without it resetting it every loop. Here is the movement part of my code:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
clock.restart();
up = true;
down = false;
}
if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
up = false;
down = true;
}
if(up == true)
{
graham.move(0,-velocity*acceleration);
velocity = velocity*acceleration;
if(velocity >= 10)
{
velocity = 10;
}
if(velocity > 1)
{
velocity = velocity / 2;
}
}
if(down == true)
{
velocity = velocity*acceleration;
graham.move(0,velocity*acceleration);
if(velocity >= 10)
{
velocity = 10;
}
}
That whole portion of code is located in the while (window.isOpen()), btw.
Thanks in advance for helping me out here. SFML is awesome so far and It's opened up a whole land of possibilities for me!
-
Hmm, why do you use 2 bools up and down when 1 is enough? And why do you only restart your clock when the up-key is pressed?
I don't think you have the correct formulas to calculate your position and velocity. You use velocity = velocity*acceleration which seems very strange to me. Velocity is in m/s and acceleration in m/s², thus your new velocity is now in m²/s³ which doesn't make a lot of sense. The correct formula for calculating your velocity:
(http://s22.postimg.org/pyzbssmkd/Capture.png)
with a the acceleration, v0 the starting velocity and t the time. Mind that sf::Sprite::move needs a position to move to and not a velocity.
Also, if you want to have a little bit of accurate physics you should only change the acceleration. Calculate the correct velocity under the new acceleration and with that velocity the position to move to.