Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibGdx too much FPS. How to limit?

Tags:

java

libgdx

I'm doing a game and I have a problem on some (strange) low end android devices (Galaxy S mini, Galaxy ACE ), with actually bad hardware. The FPS is on both devices 85+, and on other devices (HTC Desire, Sony Xperia Arc S, Samsung Galaxy S, HTC Wildfire ) the FPS is "normal" (around 60FPS). Also 60 FPS is showing on computer too. Obviously 85+ FPS is too much, because the game experience is different and it may cause unfair advantage to those players who are playing on 85FPS+.

I want to limit the FPS to MAX 60.

I've already searched this forum and I've found the exact question I'm asking right now. Limit FPS in Libgdx game with Thread.sleep() doesn't work

The (accepted) solution from user @daniel was this:

Thread.sleep((long)(1000/30-Gdx.graphics.getDeltaTime()));

But the thing is that doesn't work for me. If I use that code (30 replaced with 60 ), I get 30-33 FPS or if I use just the Daniel's code I get around 15-17 FPS. Why is that , and why is not working for me ? How to limit FPS on all devices to MAX 60 ? I tested this code on computer, and on devices also.

Any help HIGHLY appriciated. Thank you.

like image 493
rootpanthera Avatar asked May 28 '14 01:05

rootpanthera


People also ask

How can I limit my 60 fps?

System Config > Display Settings > Frame Rate > select the 60 fps option.


1 Answers

In your launcher class;

AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.foregroundFPS = 60;
initialize(new MyGdxGame(), config);

I don't know if this is a new feature or an old one. Use this before trying to sleep the thread.

I highly recommend not limiting the frames by sleeping the thread, just to the keep the motion at the same pace for all the devices running fast or slow. Use something like this to move things on the screen.

x+= speed*Gdx.graphics.getDeltaTime();

Good luck on your adventure with libGDX, it seems that you are just started. Because when I was just started I was asking the same questions :)


EDIT: Here is a tip:

If you have collision in your game, and if the user sleeps the game by dragging the window etc. Gdx.graphics.getDeltaTime() function will return an unexpectedly high value. For example 2 seconds instead of 16 milliseconds (60fps) you were expecting. And in that case your character will jump, through walls and collision detection will fail. So here is what you can do:

  1. You can override that function and either take average of the last 10 frames and return that value (it is a little flawed in some cases but it is better in some other cases).
  2. You can return 30 milliseconds if the value is more than 30 seconds. So the character doesn't jump, go through walls... Even if the player sleeps it for a century.
  3. Combine both. (best) Don't count values more than 30 milliseconds when taking average.

I wrote a page about this subject, it is at my website in tutorials section. Check it out. nightlybuilddigital.ga

like image 51
ossobuko Avatar answered Sep 19 '22 13:09

ossobuko