pSecondEplased in the AndEngine engine is the time between the current and the last frame. the exact same variable is in libgdx. This variable is needed for the animation to be smoother. But how to calculate this time yourself? and where in these engines is the code that implements this method? I found nothing but this in the AndEngine engine `private long mLastTick; private float mSecondsElapsedTotal;

void onTickUpdate() { final long secondsElapsed = this.getNanosecondsElapsed(); this.onUpdate(secondsElapsed); } private long getNanosecondsElapsed() { final long now = System.nanoTime(); return now - this.mLastTick; } public void onUpdate(final long pNanosecondsElapsed) { final float pSecondsElapsed = pNanosecondsElapsed * 0.0000000001f; this.mSecondsElapsedTotal += pSecondsElapsed; this.mLastTick += pNanosecondsElapsed; }` 

But I still don’t have enough understanding at what point in time the method should run to correctly read this time and for some reason when I run the void onTickUpdate () method through a different class, a reference to the null object is shown ..

  • I understand that there are other ways that the graphics were smooth. For example, to set Fps, but this method allows you to give the maximum possible Fps, as they say is critical for the battery, but nevertheless, this method is implemented by the engines - Turalllb
  • There is another question: this getDeltaTime () or pSecondsElapsed (in EndAngine) is needed to make the frames smoother. If 100 should pass in a second, and the frame was processed in half a second, we received by multiplying the time by 50. and the picture will be true. But there is a FPS which is also used to make the picture correspond to reality. we put 60 frames per second and everything goes smoothly. What is better to use? or is it the same thing? or is it better to use both? - Turalllb

1 answer 1

To manually calculate the time interval between the current frame and the previous one, this should be done in the so-called main loop. In libgdx, the main loop is the render method. And getDeltaTime () is very convenient to use, since the system itself notes this time. Then, for example, multiplying it by the speed of movement of an object, we get the distance in pixels, which runs / flies the object for 1 frame.

But it is easy to do it manually even without libgdx just on java. Set 2 variables of the long type: currentKadrTime, PrevKadrTime. In the main loop of the current screen, we write: currentKadrTime = System.currentTimeMillis (); float delta = currentKadrTime - PrevKadrTime; PrevKadrTime = currentKadrTime: The resulting delta is the same as getDeltaTime () in libgdx - the time between frames. Sorry, but I do not know anything about AndEngine.

When I started learning Android Studio and Libgdx, I tried to write a famous example of a game with falling drops, first on libgdx, and then without libgdx just by drawing on the canvas. And applied the method described above. In both cases, the movement of the droplets turned out smooth.

If we talk about fps, then you can artificially limit it for example like this: set a constant in milliseconds so that frames are drawn no more often than after this period of time. Again, in the main loop, before checking, we check the condition: if (current time) minus (last detected time before zeroing) is greater than or equal to a constant, then we draw a scene. This will really reduce the load and save the battery.

  • Thank you figured out and especially interesting about the limitations of fps, if I’m not mistaken in AndEngine it works, just when you said I understood what was written there. It remains to limit a little and everything will work as I want. - Turalllb