You can write a physics engine but not a step algorithm?
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.