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

0 Members and 1 Guest are viewing this topic.

#### Lillu

• Newbie
• Posts: 2
##### 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: 3035
• My number of posts is shown in hexadecimal.
##### 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
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision