1
\$\begingroup\$

I am thinking about where to call the physics simulation. The current situation is that I first update the entities and then the physics immediately before rendering. As you can see in the following example:

// Only an example, my loop uses not true :) while(true) { this.UpdateScene(); this.ProcessPhysics(); this.RenderScene(); } 

Is this the correct order?

\$\endgroup\$
5
  • 1
    \$\begingroup\$ If it works it is correct. There are different game loops for different use cases. With that said, there might be room for improvement. For example you might want to tie the rendering frequency to the display refresh rate, and you might want to detach the physics update frequency from the rendering frequency. You should be able to find questions about that on this site. Is that what you want? What are you trying to make? \$\endgroup\$ Commented Feb 23, 2024 at 17:41
  • \$\begingroup\$ Thanks for your comment. I try to get my game loop done in a proper way. And just want to ask what the best priority is.. Or is there even a "best" priority. \$\endgroup\$ Commented Feb 23, 2024 at 17:57
  • \$\begingroup\$ I don't know about "best", I'd say that is a matter of opinion. But what I mentioned is the most common. Usually we do not want to render more frames than will be displayed, and making the physics update frequency somewhat stable can be either a matter of making the simulation code simpler by assuming the elapsed time between updates is constant, or to make it less susceptible to discrepancies when executing the code on different machines, or it might be a matter of optimization by doing the simulation less frequent, and using interpolation to render intermediary steps. \$\endgroup\$ Commented Feb 23, 2024 at 18:20
  • 1
    \$\begingroup\$ Some games use a fixed timestep for physics calculations, so perhaps you could look into that? In general though, the game loop is up to you, and you can do whatever suits your needs/works the best for you or your use case. \$\endgroup\$ Commented Feb 27, 2024 at 11:02
  • \$\begingroup\$ This is like a thought experiment. Let me think, swapping physics first with game logic will mean that you can update velocities sooner within that frame, but lose on existing velocities and simulation updates… \$\endgroup\$ Commented Feb 29, 2024 at 9:57

1 Answer 1

1
\$\begingroup\$

I'd recommend running the physics step before your other update code. That gives code in your UpdateScene function a chance to react to things that changed in the physics step.

Maybe you have an object whose sprite should change to face the direction it's currently moving in, or you're using velocity to control squash and stretch on a cartoony character. If that code runs before the physics step, the velocity might have changed by the time you render the scene, leading to these visual changes appearing to lag a frame behind.

As noted in the comments, many games will also implement a fixed timestep, where physics and other gameplay-sensitive code is run with a constant amount of game time elapsing in each tick, decoupled from the current rendering framerate. This can be good for consistency across a range of hardware. That pattern usually looks something like this:

while (ShouldRun()) { // Handle quitting float deltaTime = GetTimeSinceLastFrame(); accumulator += deltaTime; while (accumulator >= fixedDeltaTime) { // Handle simulation updates that need to be ticked at a constant // rate (0, 1, or more times) to catch up to real-time. ProcessPhysics(fixedDeltaTime); ProcessGameSystems(fixedDeltaTime); accumulator -= fixedDeltaTime; } // Handle visual updates that need to be ticked exactly once per frame. // Doing this after the sim update lets us react/fix-up visuals just in time. VariableRateUpdate(deltaTime); // Render the scene - potentially interpolating between the two // most recent simulation steps for smoothness. float interpolation = accumulator / fixedDeltaTime; RenderScene(interpolation); } 
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.