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.
System Config > Display Settings > Frame Rate > select the 60 fps option.
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:
I wrote a page about this subject, it is at my website in tutorials section. Check it out. nightlybuilddigital.ga
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With