Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Am I using the sf::Clock/sf::Time correctly?  (Read 4639 times)

0 Members and 1 Guest are viewing this topic.

Lillu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Am I using the sf::Clock/sf::Time correctly?
« 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Am I using the sf::Clock/sf::Time correctly?
« Reply #1 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" 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;
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Lillu

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Am I using the sf::Clock/sf::Time correctly?
« Reply #2 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.

 

anything