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

Author Topic: Correct way to handle physics time-stepping  (Read 1652 times)

0 Members and 1 Guest are viewing this topic.

kurasu1415

  • Newbie
  • *
  • Posts: 37
    • View Profile
    • Email
Correct way to handle physics time-stepping
« on: October 05, 2012, 10:54:41 pm »
Hey guys,
So I have written a simple physics engine which works great for me, but I want to make sure it works well on other PCs. I have found a lot of vague and complicated tutorials online that describe multiple methods of handling your timesteps. Could someone please give me a rundown, or point me in the direction of information that is complete?

Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Correct way to handle physics time-stepping
« Reply #1 on: October 05, 2012, 11:12:54 pm »
You can write a physics engine but not a step algorithm? :P

In order to get a well-defined and reproducible physics simulation, you need a fixed timestep.

float timeStep = 1 / 30.f; // 30 steps per second

void Physics::update(float elapsedTime)
{
    m_totalTime += elapsedTime;
    while (m_totalTime >= timeStep)
    {
        m_totalTime -= timeStep;
        step();
    }
}

It may not be 100% correct, but you get the idea:
- accumulate time until you can run at least one step
- run multiple steps if elapsed time is big

I'm not an expert in physics, but this is what I've learnt. And "physics fixed timestep" in Google should give you good tutorials / code examples.
Laurent Gomila - SFML developer

kurasu1415

  • Newbie
  • *
  • Posts: 37
    • View Profile
    • Email
Re: Correct way to handle physics time-stepping
« Reply #2 on: October 05, 2012, 11:47:03 pm »
Haha, my Physics engine isn't too complex. It just has a couple weird features, and uses Separating Axis Theorem to handle my collisions.

I believe my issue is that I was over-complicating the problem. I'll test my code out using your example and see how that works.

Thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10828
    • View Profile
    • development blog
    • Email
Re: Correct way to handle physics time-stepping
« Reply #3 on: October 06, 2012, 12:05:12 am »
An often referred to article is this one.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/