Separating logic and render code

Anything libgdx related goes here!

Separating logic and render code

Postby andreibarsan » Tue Dec 06, 2011 10:53 am

Hello again, libgdx community!

I have been learning a lot about this framework lately, and I have even started working on a game of my own.
I just have a small question regarding the way the framework works. So far, all I see is one big render method, in which I'm supposed to iterate through my scene graph, update the physics world and render everything. Shouldn't the render and logic codes reside on separate threads? Everything I've read so far about Android games and game development in general seems to suggest this. Usually so as not to affect the gameplay should the framerate drop (in the situations where delta time can't really handle it).

Should I just run my logic in the render method, and then just check if I have enough time to actually render that frame?
Is libgdx secretly handling all of this and I'm just missing it?

Thanks in advance for your time! :)
andreibarsan
 
Posts: 8
Joined: Mon Nov 21, 2011 6:52 pm

Re: Separating logic and render code

Postby pitbuller » Tue Dec 06, 2011 12:29 pm

You definetly want to run logic and rendering on same thread. Android scheduler is pretty bad. It's also yield cleaner code and better performance in most cases.
This don't mean that you should not separate rendering and logic.
I do handle this thing like this:

Code: Select all
void render(){

  float delta = Gdx.graphics.getDeltaTime();

  //physics do run fixed 60times per second
 physics(delta);
 
 // logic do run fixed 15times per second
  logic(delta);
 
 //this is done allways, everything rendered is also interpolated between two physic state
  render(delta);
}



If you have some really heavy weight and independent task like pathfinding put those at different thread but otherwise safe yourself from troubles.
pitbuller
 
Posts: 134
Joined: Mon Jul 25, 2011 9:07 am

Re: Separating logic and render code

Postby andreibarsan » Tue Dec 06, 2011 12:54 pm

Ah, thanks for the quick reply. :)
I've also read this article which I've found really helpful: http://gafferongames.com/game-physics/fix-your-timestep/ !

So I should just run everything in the main render method, and maybe just watch out if the render part starts taking too much, and if it's the case, skip 1-2 frames.
I was a bit confused by the Google IO Real-Time Game Talk where Chris Pruett strongly suggests to keep logic and rendering in different threads on Android:
http://www.youtube.com/watch?feature=player_detailpage&v=7-62tRHLcHk

EDIT: fixed some bb codes
andreibarsan
 
Posts: 8
Joined: Mon Nov 21, 2011 6:52 pm

Re: Separating logic and render code

Postby gcoope » Tue Dec 06, 2011 2:21 pm

Yup that gafferongames site is excellent. I recommend keeping everything in one Android thread, but having a game loop with a fixed time step that can be looped multiple times between rendering.

For my current game I have a timestep of 1/60th of a second, on high end devices it ends up alternating between game update, renderer and sleeping, hitting 60fps. On a lower end device (for me the N1 is the bottom end I am targeting) it ends up doing 2 physics updates for every render loop, rendering at 30fps. Note this is all automatic and I also do some interpolating so objects appear to move smoothly at any framerate. If the game is run on an even lower end system then the player will see slowdown as I don't allow the physics to loop more than twice (to stop getting into slowdown loops). This way I have good-looking, scalable performance with 100% consistent physics on any platform. Variable time-step is a horrible bodge you should only use for non-gameplay critical stuff, e.g. UI transition elements etc. (this is just my opinion).

EDIT: I just read that gafferongames thing again after seeing it a year or so ago, he explains it so well and the final solution he suggests is absolutely ideal. It should be mentioned that you don't have to repeat your entire game code (input handling, etc) over and over, just your physics engine (anything handling movement and collision).
gcoope
 
Posts: 141
Joined: Tue May 17, 2011 9:42 pm

Re: Separating logic and render code

Postby pitbuller » Tue Dec 06, 2011 6:49 pm

pitbuller
 
Posts: 134
Joined: Mon Jul 25, 2011 9:07 am


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot] and 1 guest