SFML community forums

Help => General => Topic started by: kurasu1415 on October 05, 2012, 10:54:41 pm

Title: Correct way to handle physics time-stepping
Post by: kurasu1415 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.
Title: Re: Correct way to handle physics time-stepping
Post by: Laurent 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.
Title: Re: Correct way to handle physics time-stepping
Post by: kurasu1415 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.
Title: Re: Correct way to handle physics time-stepping
Post by: eXpl0it3r on October 06, 2012, 12:05:12 am
An often referred to article is this one (http://gafferongames.com/game-physics/fix-your-timestep/).