Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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  
  Update stuff unsync with the rendering loop  (Read 1251 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2011-01-21 10:55:57 »

I have a framerate 30 roughly, but I don't want all my things to go that fast. What if I only want my particles to update every third, or 27th loop?  Cheesy

Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #1 - Posted 2011-01-21 11:41:40 »

use counters . for example, if you want your characters to update animation every 2 frames, and particle every 3 frames :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
character.animCount++;
if (character.animCount >=2 )
  {
   character.animCount=0;
   character.update();
  }

particle.animCount++;
if (particle.animCount >=3 )
{
   particle.animCount=0;
   particle.update();
}


if you want more 'broken' numbers, use floats for your counters and update 0.1f each frame, or something like that .

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 81
Projects: 15


★★★★★


« Reply #2 - Posted 2011-01-21 11:57:57 »

Use delta time to have frame independent movement, there is an explanation here on roughly how its done.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #3 - Posted 2011-01-21 12:34:48 »

Aye, use delta time if you want to update not in multiples of frames. i.e
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
long particleDelta = 1000 / 20; // update particles at 20 fps
long particleTime = 0;
long aiDelta = 1000 / 40; // AI at 40 fps
long aiTime = 0;

private void advance( long delta )
{
   particleTime += delta;
   while( particleTime >= particleDelta )
   {
      particles.advance( particleDelta );
      particleTime -= particleDelta;
   }

   aiTime += delta;
   while( aiTime >= aiDelta )
   {
      ai.advance( aiDelta );
      aiTime -= aiDelta;
   }
}
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2011-01-22 09:01:51 »

Thanks guys  Cheesy I used counters before, so I'm going to try using delta timing.

Aye, use delta time if you want to update not in multiples of frames. i.e
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
long particleDelta = 1000 / 20; // update particles at 20 fps
long particleTime = 0;
long aiDelta = 1000 / 40; // AI at 40 fps
long aiTime = 0;

private void advance( long delta )
{
   particleTime += delta;
   while( particleTime >= particleDelta )
   {
      particles.advance( particleDelta );
      particleTime -= particleDelta;
   }

   aiTime += delta;
   while( aiTime >= aiDelta )
   {
      ai.advance( aiDelta );
      aiTime -= aiDelta;
   }
}


advance(long) is called from update() right? Where do I get the delta from, that goes as a parameter?   Smiley

Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #5 - Posted 2011-01-22 14:10:51 »

Use System.currentTimeMillis() to track the time between frames
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #6 - Posted 2011-01-22 20:30:43 »

Just to make sure:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
public void run() /* The frames of the animation are drawn inside the while loop. */ {
        long beforeTime, afterTime, timeDiff, sleepTime;
        long overSleepTime = 0L;
        long delta;
        int noDelays = 0;
        long excess = 0L;

        gameStartTime = J3DTimer.getValue();
        prevStatsTime = gameStartTime;
        beforeTime = gameStartTime;

        running = true;

        while (running) {
            gameUpdate();
            gameRender();
            paintScreen();

            afterTime = J3DTimer.getValue();
            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 = (J3DTimer.getValue() - 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;
                }
            }
            delta = ((beforeTime + J3DTimer.getValue()) / 1000000L);
            beforeTime = J3DTimer.getValue();

            /* If frame animation is taking too long, update the game state
            without rendering it, to get the updates/sec nearer to
            the required FPS. */

            int skips = 0;
            while ((excess > period) && (skips < MAX_FRAME_SKIPS)) {
                excess -= period;
                gameUpdate();    // update state but don't render
                skips++;
            }
            framesSkipped += skips;

            storeStats();
        }

        printStats();
        System.exit(0);   // so window disappears
    } // end of run()


Then the delta long (time between frames), would go as parameter in the advance method, which is called from gameUpdate()? The J3DTimer.getValue() return the same as System.getNano()
Did I do this right?  Undecided

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.

CopyableCougar4 (13 views)
2014-12-28 02:10:29

BurntPizza (15 views)
2014-12-27 22:38:51

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
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!