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

Author Topic: Time-based movement: recommended?  (Read 2874 times)

0 Members and 2 Guests are viewing this topic.

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Time-based movement: recommended?
« on: April 10, 2012, 03:31:49 pm »
Is it a good idea to do time-based movement instead of frame-based? I see, the pro is that the speed seems constant on every computer even though it's old. But what about the negatives? What, if object A misses collision with object B because it goes completely through it by 1 frame?

const float Speed = 50.f;
float Left = 0.f;
float Top  = 0.f;

while (App.IsOpened())
{
    if (App.GetInput().IsKeyDown(sf::Key::Left))  Left -= Speed * App.GetFrameTime();
    if (App.GetInput().IsKeyDown(sf::Key::Right)) Left += Speed * App.GetFrameTime();
    if (App.GetInput().IsKeyDown(sf::Key::Up))    Top  -= Speed * App.GetFrameTime();
    if (App.GetInput().IsKeyDown(sf::Key::Down))  Top  += Speed * App.GetFrameTime();
}
Please note that my previous display name was "Shy Guy".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Time-based movement: recommended?
« Reply #1 on: April 10, 2012, 03:57:44 pm »
If your app is able to produce this kind of situation, then you should check collisions with the line from the previous position to the next one, instead of the just the next position point. This way you can never go through a wall.

But collision detection and physics computing are generally more reliable with a fixed timestep anyway.
Laurent Gomila - SFML developer

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: Time-based movement: recommended?
« Reply #2 on: April 10, 2012, 05:14:16 pm »
A very good article on timesteps and fixing them can be found here:
http://gafferongames.com/game-physics/fix-your-timestep/
« Last Edit: April 10, 2012, 05:22:27 pm by MorleyDev »
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Re: Time-based movement: recommended?
« Reply #3 on: April 10, 2012, 05:19:25 pm »
If your app is able to produce this kind of situation, then you should check collisions with the line from the previous position to the next one, instead of the just the next position point. This way you can never go through a wall.

But collision detection and physics computing are generally more reliable with a fixed timestep anyway.
Exactly what I thought, too.


A very good article on timesteps and fixing them can be found here:
http://gafferongames.com/game-physics/fix-your-timestep/
Okay, I will take a look at that, maybe it helps :)
Please note that my previous display name was "Shy Guy".