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

Author Topic: DeltaTime  (Read 10496 times)

0 Members and 1 Guest are viewing this topic.

noaboa

  • Newbie
  • *
  • Posts: 14
    • View Profile
DeltaTime
« on: January 11, 2016, 08:59:40 pm »
Hi, I am new to SFML and trying to make a moving rectangle. I am wondering if this is the correct use of deltaTime


while (window.isOpen()){
                sf::Event event;
               
                time = clock.restart();
                float dt = time.asSeconds();

                while (window.pollEvent(event)){

                        sf::Vector2f playerXY;
                       
                        if (acceleration > 1)
                                acceleration = 1;

                        if (event.type == sf::Event::Closed)
                                window.close();

                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
                                acceleration += 0.1;
                                playerXY = sf::Vector2f(-150.0f * dt, 0.0f);
                        }

                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
                                acceleration += 0.1;
                                playerXY = sf::Vector2f(150.0f * dt, 0.0f);
                        }
                        else{
                                acceleration = 0.2;
                        }

                        std::cout << dt << std::endl;

                        playerXY *= acceleration;

                        player.move(playerXY);
                       
                }
                window.clear();
                window.draw(player);
                window.display();
        }
 
« Last Edit: January 13, 2016, 02:36:35 am by eXpl0it3r »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: DeltaTime
« Reply #1 on: January 12, 2016, 12:48:28 am »
Anything that changes should be multiplied by delta time. That includes your modification of acceleration.
e.g. acceleration += 100.f * dt;
It will require larger numbers here since it's being multiplied by dt (a usually very small value) but that's okay because this is the rate of change for acceleration and not the acceleration itself.

Acceleration is a float, of course, right?

I find playerXY to be an odd name for a vector offset but it's your choice to name it whatever you like  ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Ventus

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: DeltaTime
« Reply #2 on: January 13, 2016, 03:28:41 pm »
You are reseting the clock and storing the time as seconds after that, is that what you intended?
I might be mixing things up, but maybe you would want something like this:

float dt = clock.restart().asSeconds();

Then using it would be as simple as:

//Before the game loop
sf::Vector2f velocity(0.f, 0.f);
sf::Vector2f acceleration(0.f, 10.f);
...

//In the main loop
velocity.x = acceleration.x * dt;
velocity.y = acceleration.y * dt;

Then again, I might be mixing things up here.
King Crimson

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: DeltaTime
« Reply #3 on: January 13, 2016, 09:47:11 pm »
float dt = clock.restart().asSeconds();
is equivalent to
sf::Time time = clock.restart();
float dt = time.asSeconds();
dt has the same value both ways.

The velcoty/acceleration way that Ventus mentioned is the usual way to do this. However, the acceleration should be added to the velocity, not just replace its current value. Also, you don't need to working with each component separately, thus:
velocity += acceleration * dt; // (sf::Vector2f += sf::Vector2f * float)
where acceleration is the "thrust" in a particular direction (a vector)
If you use acceleration as a single value, it will need to either be relative to the current velocity, or be multiplied by a unit vector in the same direction to the velocity

Also, as I mentioned earlier, change to acceleration would also be multiplied by dt so:
acceleration += jerk * dt; // (float += float * float)
or
acceleration += jerk * dt; // (sf::Vector2f += sf::Vector2f * float)
where "jerk" is the rate of change for acceleration
This time, if you use acceleration as a vector and a single value for jerk, jerk will need to either be relative to the current acceleration, or be multiplied by a unit vector in the same direction as the acceleration.

A unit vector is a vector divided by its length (its length becomes one but its angle is the same).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Ventus

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: DeltaTime
« Reply #4 on: January 13, 2016, 10:19:34 pm »
float dt = clock.restart().asSeconds();
is equivalent to
sf::Time time = clock.restart();
float dt = time.asSeconds();
dt has the same value both ways.

Oh, my mistake, I must've over-read it. I thought he did something like this:
time = clock.restart();
float dt = clock.getElapsedTime().asSeconds();

King Crimson

 

anything