how o you implement frame indepenent movment?

If I wish to have an object move at the same speed wether it as running at 60fps or 30fps how would I go about doing that?

You need a accurate timer (and we all now the timer under windows suck)… some psudo code


lastTick = System.currentTimeMillis();
do{
   currTick = System.currentTimeMillis();
   sprites.tick(currTick-lastTick);
   sprites.render();

   Thread.sleep(alittle);
    
   lastTick = currTick;
}while (isRendering);

public class Sprite{

   public void tick(long deltatime){
      xpos = xpos + (vel_x * deltatime);
      ypos = ypos + (vel_y * deltatime);
  }
}


something like that. The sprite is now moving depending on the amount of time that elapsed from the last tick was called.

edit added the lastTick update as pointed out by kevglass

You missed the lastTick update :wink:

Kev

Well can’t remember everything ::slight_smile: Thanks for the reminder

I will have to edit the above pos then…

Thanx for the code. I have an accurate timer so ill try to implement it when I get home.
Some theoretical stuff before I get home and implement. So basically if my x velocity is 5 units/sec and the delta time is .5 sec then it will move the sprite 2.5 units. And if it was slower frame rate which means the dleta time would be like .8 then it would move it 4.0 units. I get it :slight_smile: