I have an app, in which I have constant int speed = 5; On each update, the game object (player) is moving 5px ahead. Problem is, that I can't handle that on different devices - older devices is having fx. 20fps (so 20 updates per second), and other one have 60fps. Then, my object is moving faster or slower, than it should.

I tried some screen width divisions, but didn't managed to work it.

I also tried to stick to some FPS (25) and skip frames if necessary. But on my tablet and phone, it just run on its own; it works like there isn't any frame skip code at all.

  • 5
    \$\begingroup\$ Try using delta time, or the time passed between this frame and the previous one. For example, speed = 5 m/s then position += speed * deltaTime (in seconds). \$\endgroup\$ – EvilTak Nov 21 '15 at 18:17
  • \$\begingroup\$ To add to what the other commenter said, you should research "fixed time step". Here is a link for you gafferongames.com/game-physics/fix-your-timestep \$\endgroup\$ – bazola Nov 21 '15 at 18:20
  • \$\begingroup\$ Remember objects in the real world don't have "pixels moved per frame" they have velocities. And a velocity is a movement per time. Everything should be expressed as something per second. Then calculate what that means in terms of something per frame in the frame itself when you know how much of a second that particular frame is worth \$\endgroup\$ – Richard Tingle Nov 21 '15 at 21:16

Firstly you should find delta time and then multiply your movement velocity on that.

position.x = velocity * deltaTime;

Your Answer

