CodeHard_or_HardCode
CodeHard_or_HardCode

Reputation: 287

Ideal display.sync rates for OpenGL

I am using Box2D and OpenGL. I found that (at a 60 frame rate) when I apply quick changes in direction to a fast moving object, the rendering seems to jump or perhaps skip frames. (I am only operating in 2D). I want to push the physics to the very edge. (Lots of objects moving simultaneously, possibly breaking down shapes with welds etc.)

If I speed up the display.sync() from 60 to 180, it is much cleaner.

What is an ideal frame rate? Are there any other ways to keep the rendering clean?

With speed and only basic drawing being the priority, are there better libraries? Such as Slick2D?

Upvotes: 0

Views: 239

Answers (2)

Matthias E
Matthias E

Reputation: 36

MtRoad's answer as a possible implementation.

public class GameStateRunning {
  private final int TICKS_PER_SECOND = 30;
  private final double timePerTick = 1000 / TICKS_PER_SECOND;
  private final int MAX_FRAMESKIP = 5;
  private double next_game_tick = System.currentTimeMillis();
  private int loops;
  private double extrapolation;


  public void update() {
    loops = 0;
    while (System.currentTimeMillis() > next_game_tick && loops < MAX_FRAMESKIP) {

     // YOUR GAME UPDATE CODE GOES HERE

      next_game_tick += timePerTick;
      loops++;
    }

    if (next_game_tick < System.currentTimeMillis()) {
      next_game_tick = System.currentTimeMillis();
    }

    extrapolation = 1 - (next_game_tick - System.currentTimeMillis()) / timePerTick;
  }


  public void render() {
  //  YOUR GAME RENDER CODE GOES HERE
  }

Slick2d probably won't be faster than OpenGL, as Slick2d uses OpenGL.

Upvotes: 0

pyj
pyj

Reputation: 1499

Sometimes the problem isn't in the renderer, but rather in the fact your time step in your simulation is causing problems and making it look like your frame rate is off. I noticed similar problems in a program of mine using OpenGL and Box2D and fixing my timestep helped smooth things out significantly.

Really good article here.

Upvotes: 4

Related Questions