Game Loop delay the right way and game speed

I am a newbie game developer. I am trying to find a right way to create my game loop.

Consider following example. Simple Android Game I have a game with a simple gameplay - bouncing ball.

I want to have a control of the ball speed. Also I want my game to have the same framerate (framespeed) on all devices.

1. Where the speed logic should be implemented ? In my case I have simple ball object with a velocity vector. I can change the velocity vector properties in order to make my ball move faster or slower, am I right ?

    double previous = getCurrentTime();
double lag = 0.0;
while (true)
{
double current = getCurrentTime();
double elapsed = current - previous;
previous = current;
lag += elapsed;

processInput();

while (lag >= MS_PER_UPDATE)
{
update();
lag -= MS_PER_UPDATE;
}
render();
}


I have the similar loop in my game.

 @Override
public void run() {
while (mIsRunning) {
processGameInput();
updateGameState();
drawGame();
}
}


I cannot understand while update(); is called while waiting for a constant delay ? Why not the render method ? Maybe I am missing something.

Here is how my update for the ball looks like.

   public void updatePosition() {
mCenterX += mVector.x;
mCenterY += mVector.y;
}


As you could see, each time I increase the position of the ball by the value of the velocity vector.

But as can guess, in case of using the example from the article, the update method will be called for instance 100 times. And the render method only once per 100 update method calls. So in this case I guess, I the ball will be changing its position lagging.

My goal is to control the speed of the ball, for example from 0.1s - 2s.

How can achieve this ?

First of all, the game main loop is not a trivial problem, you have to understand exactly what happens before you get it wrong. Some facts:

• You don't know how long render takes
• You don't know how long game update takes

Any pre-emption on the time taken by rendering will not work, causing some stuttering, in general it is ok to have the game update with some lag compared to input collection. Unless the lag is very long, we will not notice it.

In general you want to update graphics not too fast (because anyway you can get only 60 to 100 images displayed on the screen every frame).

You also want your game to update as often as necessary, physical simulations usually have to be updated in fixed time steps, while you want your game to process to all inputs.

Rendering is updated accordign to values computed in your game update. Basically you want your game update to last as few as possible ( < 10 ms) so you can update graphics according to it.

AND you want to be frame rate indipendent:

Basically you have just to measure time elapsed since last update, and use it in your update functions.

I'm not suprised that important piece is missing in the link you provided (since that site also missed important points about ECS programming, so don't warry, it is not your fault! but I suggest you better readings!)

while (lag >= MS_PER_UPDATE)
{
update(); // ops!! how much time elapsed here? you don't know!
lag -= MS_PER_UPDATE;
}


Instead I would do the following:

double previous = getCurrentTime();
double totalElapsed = 0.0f;
while (true)
{
double current = getCurrentTime();
double elapsed = current - previous;
previous = current;

processInput();
update( elapsed);
totalElapsed += elapsed;

if(totalElapsed > MS_PER_FRAME)
{
render();
totalElapsed -= MS_PER_FRAME;
if(totalElapsed > MS_PER_FRAME)
std::cout<<"Performance warning, rendering or update took too long"<< std::endl;
}
}


And in your ball position computation use:

 public void updatePosition(double elapsed) {
mCenterX += mVector.x * elapsed;  // framerate indipendent
mCenterY += mVector.y * elapsed;
}