Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
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  
  Game loop sync (Kevglass)  (Read 1948 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Posted 2006-09-12 21:45:44 »

I've been checking out your source for the 4k games, and I'm interested in this game loop technique.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
      
      long lastLoopTime = System.currentTimeMillis();
     
      while (true)
      {
         int delta = (int) (System.currentTimeMillis() - lastLoopTime);
         for (int i=0;i<delta/10;i++) {
            logic(10);
         }
         logic(delta % 10);
         
         lastLoopTime = System.currentTimeMillis();
         
         draw((Graphics2D) strategy.getDrawGraphics());
         
         strategy.show();
         if (!isVisible()) {
            System.exit(0);
         }
      }




Why do you call logic() inside that for-loop, and also after the for-loop. Aren't you calling the logic() way too often, and hence slowing down the game? Isn't it better to calculate the delta argument to pass to logic(delta) instead of calling logic() n-times? Like this:
1  
2  
3  
4  
5  
         int diff = 0;
         for (int i=0;i<delta/10;i++) {
            diff+=10;
         }
         logic((delta % 10) + diff);

??

Are there any better ways to keep the game logic at a static speed, independent from framerate? I'm not too familiar with this concept.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2006-09-12 21:58:58 »

What you're looking at there is fudged collision detection (which I actually use in everything these days Smiley). 4K isn't a great place to look for good coding examples btw Smiley

In the game there may be moving elements which want to collide with each other that are updated by the logic based on the delta in time (change in time). However, if the logic time step gets too big two elements might move so far that they pass right though each other. Imagine a bullet and alien. The bullet is moving - if the logic which moves it was stepped with 1000ms it might jump right past the alien and never intersect with it. The way the loop is above the time step is always small - meaning that no matter how much time is passed the logic is only updated in small sections. Logic tends to be cheaper than rendering (horrible generalisation) in this sort of simple game - so it doesn't hurt to call render often and makes the collision detection easy to implement and much more reliable.

The other way to do this which more reliable is swept collision detection when you work out the path of all the moving elements and determine where they cross based on time. However, this sort of collision is non-trivial and doesn't really suit 4K programming (or me generally for that matter).

Incidently, your rewrite:

1  
2  
3  
4  
5  
int diff = 0;
for (int i=0;i<delta/10;i++) {
   diff+=10;
}
logic((delta % 10) + diff);


essentially does this:

1  
logic(delta);


which is almost certainly what I had in place originally before noticing the collision problems.

Hope this helps,

Kev

Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Reply #2 - Posted 2006-09-12 22:05:27 »

Ah, ok, great explanation Smiley thanks.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Reply #3 - Posted 2006-09-13 13:31:51 »

So, ok,

Let's say I have a Sprite (bullet) that needs to move 100 pixels every 1 second (let's say from bottom to top of screen), and the delta is 7ms (since the last game loop), how do I calculate how much to move that sprite in that loop?


Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #4 - Posted 2006-09-13 13:48:13 »

with a delta of 7, surely the logic() in the loop will not get hit, and only the logic() outside will, and will get called with 7 (delta%10 where delta = 7)

in your logic code you will move the bullet 0.7 of a pixel ((pixels per second/millis in second) * delta in ms)  = (100/1000) * 7

Endolf

Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Reply #5 - Posted 2006-09-13 14:25:22 »

Ok, thanks.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #6 - Posted 2006-09-13 15:26:31 »

I've been checking out your source for the 4k games, and I'm interested in this game loop technique.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
      
      long lastLoopTime = System.currentTimeMillis();
     
      while (true)
      {
         int delta = (int) (System.currentTimeMillis() - lastLoopTime);
         for (int i=0;i<delta/10;i++) {
            logic(10);
         }
         logic(delta % 10);
         
         lastLoopTime = System.currentTimeMillis();
         
         draw((Graphics2D) strategy.getDrawGraphics());
         
         strategy.show();
         if (!isVisible()) {
            System.exit(0);
         }
      }


This is my favorite pet peeve these days, but that timing loop is leaking time!
The time between the two calls to System.currenTimeMillis() will be lost forever, resulting in a slightly slower running game on slow computers.

"Fixed"[1] version:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
      
      long lastLoopTime = System.currentTimeMillis();
     
      while (true)
      {
                        long now = System.currentTimeMillis();
         int delta = (int) (now - lastLoopTime);
         for (int i=0;i<delta/10;i++) {
            logic(10);
         }
         logic(delta % 10);
         
         lastLoopTime = now;
         
         draw((Graphics2D) strategy.getDrawGraphics());
         
         strategy.show();
         if (!isVisible()) {
            System.exit(0);
         }
      }



[1] I am well aware that you can't "fix" a 4k game. They were never designed to be accurate.

Play Minecraft!
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2006-09-13 16:09:32 »

Interesting, in my later stuff I replace:

Quote
long now = System.currentTimeMillis();
int delta = (int) (now - lastLoopTime);
lastLoopTime = now;

wtih

Quote
int delta = (int) (System.currentTimeMillis() - lastLoopTime);
lastLoopTime += delta;

is this appropriate? Seemed pretty elegant to me Smiley

Kev

Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #8 - Posted 2006-09-13 16:39:45 »

I usually do this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
long now = System.currenTimeMillis();
unprocessedTime+=(int)(now-lastTime);
lastTime = now;

while (unprocessedTime>millisPerTick)
{
    tick();
    millisPerTick+=unprocessedTime;
}


Not quite as elegant, but it leaves a really useful unprocessedTime variable.. To figure out how far to interpolate between the last two game ticks, just use
1  
float delta = unprocessedTime/(float)millisPerTick;



(of course, in the real world, I don't actually use System.currentTimeMillis..)

Play Minecraft!
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2006-09-13 17:20:31 »

Quote
    millisPerTick+=unprocessedTime;

Is that right? Would have though you'd keep millisPerTick constant and decrement unprocessedTime

Quote
    unprocessedTime -= millisPerTick

but maybe I'm just not getting it (it's early here Smiley)

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Amos Wenger

Senior Member




Everything's possible, but not everything's fun...


« Reply #10 - Posted 2006-09-13 17:45:26 »

(of course, in the real world, I don't actually use System.currentTimeMillis..)
System.nanoTime() ? Or another custom timer ? LWJGL ?

"Once you start working on something, don't be afraid of failure and don't abandon it. People who work sincerely are the happiest"
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #11 - Posted 2006-09-14 10:59:19 »

Quote
    millisPerTick+=unprocessedTime;

Is that right? Would have though you'd keep millisPerTick constant and decrement unprocessedTime

Quote
    unprocessedTime -= millisPerTick

but maybe I'm just not getting it (it's early here Smiley)

Kev

er, yes. I'm a tool. Cheesy

(this is why no of my programs work on the first try. baaaad typos)

Play Minecraft!
Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Reply #12 - Posted 2006-09-14 20:10:22 »

What does tick() do?

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #13 - Posted 2006-09-15 13:14:27 »

advance game logic one step.

Play Minecraft!
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #14 - Posted 2006-09-15 14:10:19 »

What does tick() do?

They grab on to your pets and suck thier blood Wink

Endolf (The oh so helpfull)

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.

pw (13 views)
2014-07-24 01:59:36

Riven (13 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (14 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (35 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!