Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  movement in 2D: UPS or getting time ?  (Read 1474 times)
0 Members and 1 Guest are viewing this topic.
Offline alesky

Junior Member


Medals: 2


mmm....


« Posted 2012-09-26 00:57:43 »

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++;
         }
      }
      
Offline ReBirth
« Reply #1 - Posted 2012-09-26 05:58:04 »

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.

Offline alesky

Junior Member


Medals: 2


mmm....


« Reply #2 - Posted 2012-09-26 17:23:49 »

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

1  
2  
3  
4  
5  
6  
7  
  /* 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++;
         }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Rorkien
« Reply #3 - Posted 2012-09-26 20:18:18 »

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)
Offline alesky

Junior Member


Medals: 2


mmm....


« Reply #4 - Posted 2012-09-27 01:23:12 »

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

1  
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
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
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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
1  
2  
3  
4  
5  
6  
7  
/* 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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (48 views)
2014-04-15 18:08:23

BurntPizza (44 views)
2014-04-15 03:46:01

UprightPath (60 views)
2014-04-14 17:39:50

UprightPath (42 views)
2014-04-14 17:35:47

Porlus (58 views)
2014-04-14 15:48:38

tom_mai78101 (82 views)
2014-04-10 04:04:31

BurntPizza (140 views)
2014-04-08 23:06:04

tom_mai78101 (240 views)
2014-04-05 13:34:39

trollwarrior1 (200 views)
2014-04-04 12:06:45

CJLetsGame (207 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!