Wednesday, April 1, 2020

Tutorial 1.6b: World unit velocity + DeltaTime

One last small topic for today, and then I'm going back to sleep. Like a bear, I'll just have a long nap until Corona is over. Oh crap, there is the woman, pointing at that shovel in the garden.

This part belongs to the previous topic really, but since I try to keep the topics short and not covering too many things at the same time (which I always find extremely difficult, always floating up to "the bigger picture", as if I was filled with Helium)... But, look again at the GxCamera MOVE function:
public void move( float deltaMetersX, float deltaMetersY )
{ deltaMetersX, deltaMetersY );
} // move

As most functions in our "wrapper" class, it just calls the internal LibGDX camera, which does the actual work. In this case "move" calls "translate", which is a more proper world for shifting things in 2D or 3D games. But I called it move anyway. The important thing to know, is that it ADDS the given arguments, not REPLACES. A "setPosition" would actually replace the current coordinates. As in "Bad Billy, you stand over there, in the corner of the classroom". Whereas translate means "Bad Billy, you move over 1 meter to the left, in that lava pit".

Hence the "delta" (difference) names of the arguments. And delta is given in meters. +0.25 means the camera moves 25 centimeters to the right (assuming + is right, and minus is left). So in the same fashion how we moved that sprite a bit every cycle in chapter 1.5, we can call "move" every cycle. For example: "camera.move( -0.25f, 0.f );". That will make the camera move 25 centimeters to the left every cycle.

If you run that code, you will notice the camera flies away at almost warpspeed though. Not sure if this is by default. but here it seems the Phone (emulator) tries to run at 60 FPS. So we actually move 60 x 0.25 = 15 meters per second. Which is even faster than Usain Bolt. But if your phone can't handle sixty frames per second, say it only runs 30 FPS because the phone is an old piece of shit, the speed would reduce to 7.5 meters per second. If the framerate varies all the time because your phone is doing all kinds of background things, the speed may go up and down, and up and down.

You don't want that. Therefore, install some stabilizers:
float camMoveSpeed = -2.5f *;
camera.move( camMoveSpeed, 0.f );
This example (almost) guarantees that the camera moves with 2,5 meters per second, to the left. To guarantee that, it takes the "deltaTime" into account. LibGDX measures the elapsed time since last (render) call. So at a steady 60 FPS, the deltaTime should always be near 1/60 ~= 0.0166667.So the camera actually moves with 2.5 x 0.016666 := 0,041666 meters per CYCLE. If there was a hick-up for whatever reason, the deltaTime is larger, meaning the movement step will be larger that frame as well, to compensate the lost time.

Pretty easy right? You will see later on that pretty much every Update function that has something to do with time (rendering, physics, animation, counting, ...) uses such a "deltaTime" or "deltaSeconds" argument.

No comments:

Post a Comment