FPS Independant motion

I am coming along on RPG… It really does exit.
(Although most time latley has been spent on Editor)
And I have moved it to a different machine w/ a faster graphics card. Now my character runs at a faster speed, even though I am using the gage timer. Any Ideas on this? i.e. can I crank up the fps and still keep my character moving at the same pace?

To make the character movement independant from the fps, make all movement time based, not frame based:

Something like:

x += (frameTime * xSpeed);

where frameTime is the elapsed time measured between the last frame and this frame (ie the time the previous frame took).
I hope this helps.

Erik

Alternatively, you could fix the frame rate using sleepUntil().

Either way would be fine, although eriks is a bit more flexible

Kev

[quote]Alternatively, you could fix the frame rate using sleepUntil().

Either way would be fine, although eriks is a bit more flexible

Kev
[/quote]
I’m already limiting frame speed I just don’t want it to pace differently on different machines. I assume Eric’s solution will fix this. Will the need to cast the calculation to an int give me a performance hit?

are you not driving updates off the display loop?

Kev

you will need to store your x and y as floats/doubles anyway.

Otherwise; at a very high framerate, the rounding to an int could potencially prevent your character from ever moving!

Once you are storing x,y,dx,dy and dt as floats everything becomes much simpler. It just means you have to cast to an int whenever you want to render something.

[quote]are you not driving updates off the display loop?

Kev
[/quote]
I am, but what I mean is If I want to set the FPS Limit higher, I don’t want it to affect the rate of movement of players.

Ah ha! That makes more sense, you’re purposely moving your frame rate up :slight_smile: Fair enough. I thought it was just cause you’d moved to a graphics card.

Kev

This might be of intrest. That’s what I use to separate game logics and game rendering.

In a recent C++ game project I’ve been using the Model View Controller design pattern (or MVC in short. Please see http://www.javaworld.com/javaworld/jw-04-1998/jw-04-howto.html without SWING).
The timing of the model (simulation) we did by using interrupts. The free CPU time has been used to let the view paint the screen as often as possible (until it reached a max value).

If you would like to do something similar in Java (with JOGL): couldn’t you just use Java’s TimerTask (with 50 ticks per second for example) to let the model do all its logic (like movement, simulation, etc)? And let JOGL render the view as often as it can (in a seperate thread then) ?

Having looked at the TimerTask it looks like it fits these needs well. However I read here many times about “time based movement” etc. What’s the pro of that compared to interrupt driven MVC ?

[quote]This might be of intrest. That’s what I use to separate game logics and game rendering.
[/quote]
I’ll check it out, it looks like it will take me quite a while to digest it, then try to intertwine it w/ the GAGE Timer stuff. Here is my main loop:


 public void run()
    {
        //Set up collision groups
        K2CollisionManager.get().addItem(goldBag);
        K2CollisionManager.get().addItem(jewels);
        K2CollisionManager.get().addItem(meat);
        K2CollisionManager.get().addItem(bread);
        K2CollisionManager.get().addItem(wine);
        K2CollisionManager.get().addPlayer(hero);
        K2CollisionManager.get().addNPC(damsel);
        K2CollisionManager.get().addNPC(baldie);
        K2CollisionManager.get().addEnemy(knight);
        K2CollisionManager.get().addPortal(portal);
                
        
        timer.start();
        int sleepTime = (int)Math.max(timer.getTicksPerSecond()/45,1);
        long time = 0;
        int frames = 0;
        long lasttick = 0;
        
        
        System.out.println("using sleep time of " + sleepTime);
      //Play Music
      MusicEngine.play(0);
      
      state = PLAY;
      while( running )
        {
            // This is where you would call a method that updates your game state (ie: moves
            // objects, checks for collisions, etc.
            
            updateGame();
            
            // Call our render function to draw everything.
            gfxEng.render();
            
            //Check for collisions
            K2CollisionManager.get().checkCollisions();
                
            //Play Sounds
            WaveEngine.render();
            
            
            // Sleep
            timer.sleepUntil(time+sleepTime);
            time += sleepTime;

            if(frames >= 10) 
            {
                sprite.setValue((int)((timer.getTicksPerSecond()/((timer.getClockTicks()-lasttick)/frames))));

                frames = 0;
                lasttick = timer.getClockTicks();
            }
            
            frames++;
        }
    }