How do I go about changing the velocity of the player then?
You measure the frame time
dt with
sf::Clock. Then, you offset the position according to the velocity multiplied with the passed time:
sf::Vector2f velocity = getVelocityFromInput();
sf::Vector2f offset = velocity * dt.asSeconds();
sprite.move(offset);
Won't changing the velocity from 1 to -1 cause the sprite to jump from one side of the screen to the other?
No, it will revert the direction.
How are gravity and friction factored into the velocity? How about acceleration and deceleration?
All can be modeled as an acceleration which is continuously added to the velocity. The simplest case is just Euler integration:
const float friction = ...;
const float gravity = ...;
sf::Vector2f acceleration;
acceleration.x = onGround ? -friction * velocity.x : 0.f;
acceleration.y = gravity;
velocity += acceleration * dt.asSeconds();
This should work for most cases. If it's not accurate enough, you could have a look at the articles about
RK4 integration and
fixed timestep.
I have player inherit sf::Sprite because of the way I draw the game using an ordered list of many different objects that are all sub-classes of sf::Sprite.
Yes, but why are they subclasses of
sf::Sprite? That is an unnecessary coupling and restriction. The classes should rather derive from
sf::Drawable (and maybe
sf::Transformable) and hold a
sf::Sprite as a member. Like this, you can easily change the representation, like drawing multiple sprites for an object, or drawing a shape. This is not possible with your current approach.
Also,
sf::Sprite is not designed as a polymorphic base class, for example you can't have your own virtual functions that describe the object's behavior.