movement in 2D: UPS or getting time ?

Hi guys, i’m new in game programming, in i would like to undestand some concept before to write wrong code
i would like to know wich is the strategy that you use to calculate the movement of a sprite in a 2D world

consider that i have implemented a loop cicle that has the target to mantain the UPS costant and that is able also to lost some
frame to rich the target of the number of UPS/seconds

now my loop manage a game that is an interface that make separately
game.updateGame();
game.renderGame();
game.paintScreen();

obvisuly to calculate the movement i should pass a parameter to updateGame() method => updateGame(parameter)
that could be time in the way that i can conside the movement based on a concept of pixel/seconds
and mantain for any sprite a variable, called speed, that indicate the pixel/second that i want to move that sprite
so in my logic when i have to calculate the next position, it should be NewX = PreviousX+speed*parameter

but in my loop to mantain costant the update i can skip some frames, and repeat the game logic more time,
and in this case i can not pass the correct parameter to the updateGame() method

what do u think
1)if i pass my variable period to the updateGame(parameter) variable parameter
2)if i try to dipend my logic for the calculation of the movement from same function that dipend from the time and the UPS
3)if i create a mixed strategy: depend from time in the normal update depend from the period in the update executed for the skip frame?

will be well appreciated every experience shared with me, thanks

this is the code of the loop:

int NO_DELAYS_PER_YIELD = 16;
/* Number of frames with a delay of 0 ms before the animation thread yields
to other running threads. */

int MAX_FRAME_SKIPS = 5; ;
// no. of frames that can be skipped in any one animation loop
// i.e the games state is updated but not rendered

long period = (1000/TARGET_FRAME_SECOND) *1000000L; // period in ms -> nano
beforeTime = System.nanoTime();

while(!gameStatus.isStop()){

		game.updateGame();
		game.renderGame();
		game.paintScreen();

		afterTime = System.nanoTime();
		timeDiff = afterTime - beforeTime;
		sleepTime = (period - timeDiff) - overSleepTime;  
		
		if (sleepTime > 0) {   // some time left in this cycle
			try {
				Thread.sleep(sleepTime/1000000L);  // nano -> ms
			}
			catch(InterruptedException ex){}
			overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
		}else {    // sleepTime <= 0; the frame took longer than the period
			excess -= sleepTime;  // store excess time value
			overSleepTime = 0L;

			if (++noDelays >= NO_DELAYS_PER_YIELD) {
				Thread.yield();   // give another thread a chance to run
				noDelays = 0;
			}
		}

		beforeTime = System.nanoTime();

		/* If frame animation is taking too long, update game state without render it, to get the updates/sec         
		int skips = 0;
		while((excess > period) && (skips < MAX_FRAME_SKIPS)) {
			excess -= period;
			game.updateGame();    // update state but don't render
			skips++;
		}
	}

Hello new member. Welcome to JGO and remember to use code tag next time :wink:

Yes normally we will put parameter to update method, which is float/double of delta time.

Thanks for your answer!

but in case of lost frames! what I should pass to the updateGame method??

if i don’t give a value (i mean put 0 as value of the time spent ) the game logic shouldn’t move the sprites
so in this case the effect in the games is a usless update becouse the logic of movement calculation applicated to each entity of the game that is:

NewX = PreviousX+speed*parameter
so New position is the same of the old one

so at this point why use an loop alghoritm that want to mantain a constant UPS if the forced update dont move the sprites?

It is not clear for me the sense of this


  /* If frame animation is taking too long, update game state without render it, to get the updates/sec   */      
         int skips = 0;
         while((excess > period) && (skips < MAX_FRAME_SKIPS)) {
            excess -= period;
            game.updateGame();    // update state but don't render
            skips++;
         }

Maybe it just makes sense to me, but if you are working with a fixed framerate, you really shouldn’t care about delta (at least not for now). But your updates are based on sleeping time and you are calling the updateGame() method right off the bat on your gameloop, without even checking when the last update happened.

So, i guess the point of that frameskipping is to update the game based on a standard update period, or your period variable

PS: I don’t like your gameloop. Too confusing for a newcomer. (Why in the world would you have two methods for rendering? :yawn:)

Hi Rorkien thanks you!
you helped me to undestand some points

I’m working with fixed framerate, and the gameloop is implemented with the target to converge the number of UPS to a costant number each second, and this number is the TARGET_FRAME_SECOND that i use to calculate the duration time of the period

long period = (1000/TARGET_FRAME_SECOND)

and to mantain costant this number of UPS i’m also ready to skip same frames.

[quote]you really shouldn’t care about delta
[/quote]
yes you are right, i shouldn’t care the delta if the period is defined (this is the response to my question)

[quote]So, i guess the point of that frameskipping is to update the game based on a standard update period, or your period variable
[/quote]
this is also true and to ensure this, finally the last game update has sense

if the normal cicle is execute in less the period time sleep appear, instead if the execution time take more
i save the overtime related to cicle update,render and paint and save in the excess variable
becouse the period can be execute with more that that a period time


if (sleepTime > 0) {   // some time left in this cycle
           ...
           ...
         }else {    // sleepTime <= 0; the frame took longer than the period
           [b] excess[/b] -= sleepTime;  // store excess time value
            overSleepTime = 0L;

            if (++noDelays >= NO_DELAYS_PER_YIELD) {
               Thread.yield();   // give another thread a chance to run
               noDelays = 0;
            }
         }

and when the excess is more that a period time i can run all the update to reach the target of number of UPS for second if i’m in delay

/* If frame animation is taking too long, update game state without render it, to get the updates/sec   */      
         int skips = 0;
         while((excess > period) && (skips < MAX_FRAME_SKIPS)) {
            excess -= period;
            game.updateGame();    // update state but don't render
            skips++;
         }

so the period is the time to pass to the update