SFML community forums

Help => System => Topic started by: Lillu on February 01, 2015, 05:13:16 pm

Title: Am I using the sf::Clock/sf::Time correctly?
Post by: Lillu on February 01, 2015, 05:13:16 pm
So to start of with: I'm fairly sure that I am, but just trying to cover all my bases.

I'm trying to implement a frame rate independent acceleration based movement system.
The code that I have so far is as follows:

void Actor::Update(sf::Vector2f GravityStrength){
    float UpdateTime = UpdateTimer.restart().asSeconds();
   
    //Apply Gravity To velocity
    ModVelocity(UpdateTime,sf::Vector2f(GravityStrength.x * Weight, GravityStrength.y * Weight));

   
    //Checks for collision and moves the actor.
    ComputeMotion();

}

void Actor::ModVelocity(float UpdateTime, sf::Vector2f Mod){
    Velocity.x += Mod.x * UpdateTime;
    Velocity.y += Mod.y * UpdateTime;
}

This results in the actor falling at double the speed if I double the frame rate, but if I set the Velocity to 0 at start of the "Update" function the falling rate becomes constant regardless of the frame rate. This leads me to believe that I'm using SFML correctly and there is just something wrong with the overall logic, but just asking to be sure and in case any of you would willing to help with a more general problem.
Title: Re: Am I using the sf::Clock/sf::Time correctly?
Post by: Hapax on February 01, 2015, 07:28:22 pm
Your use of the clock looks okay to me although I would recommend considering using a fixed time-step if you want reliable physics and "Free The Physics (http://gafferongames.com/game-physics/fix-your-timestep/)" if you want it to be reliable on different frame rates.

This results in the actor falling at double the speed if I double the frame rate
Since frame rate is never reliably stable, you can't rely on the speed of falling, as mentioned above.

Just as an aside...
It's possible to mutiple float vectors with floats, rather than multiplying each member individually.
i.e.
Velocity += Mod * UpdateTime;
Title: Re: Am I using the sf::Clock/sf::Time correctly?
Post by: Lillu on February 01, 2015, 08:51:42 pm
Interesting article. I'll see if I can implement this and thanks for the tip about vectors.