Update stuff unsync with the rendering loop

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? :smiley:

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


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 .

Use delta time to have frame independent movement, there is an explanation here on roughly how its done.

Aye, use delta time if you want to update not in multiples of frames. i.e


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;
	}
}

Thanks guys :smiley: I used counters before, so I’m going to try using delta timing.

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

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

Just to make sure:


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? :-\