   [Java] Why won't this float insert into a string properly?
  I have a very basic gameloop:private void gameLoop()    {   float deltaTime = (float)timer.deltaTime();   Entity e = m_entities.get(0);   e.moveBy((50 * deltaTime), (0 * deltaTime));   e.rotateBy(10 * deltaTime);   ArrayList<String> textList = new ArrayList<String>();   textList.add("FrameRate: " + (1 / deltaTime));   window.setText(textList);   window.repaint();   timer.tick();  }deltaTime is in seconds.I send an Array of strings (textList) to my window to print debug text. It draws the text perfectly, but the problem is that it always draws 'Framerate: infinity'. If I just put deltaTime instead of 1 / deltaTime, it shows 'Framerate: 0.0'. For whatever reason it's acting as if deltaTime is zero when it appends it to the string.The entities (only one of them at the moment) are moving and rotating properly, so deltaTime *is* non-zero and when I run in debug mode and put a breakpoint there I can see that both deltaTime has a non-zero value and the string is showing as something like 'Framerate: 2.4'( 2.4 seconds because the breakpoint has stopped the loop running). So why is it putting '0.0' into the string when it runs at normal speed?   
Quote:If I just put deltaTime instead of 1 / deltaTime, it shows 'Framerate: 0.0'. For whatever reason it's acting as if deltaTime is zero when it appends it to the string.deltaTime is 0.Show the timer.deltaTime() implementation.   ;public class Timer {    private long oldTime;    private long currentTime;    public Timer()    {   currentTime = System.currentTimeMillis();   this.tick(); // tick once to initialise currentTime and oldTime    }    public void tick()    {   oldTime = currentTime;   currentTime = System.currentTimeMillis();    }    public double deltaTime() // returns seconds since last tick    {   return (currentTime - oldTime) * 0.001;    }}deltaTime *can't* be zero, or the entity I'm rotating wouldn't rotate, and trust me it is.Fixed it now anyway. I added a Thread.sleep(1) to the end of the gameloop and it works fine now. Seems the framerate was so high deltaTime was almost zero.   ;  You need to be careful with solutions that involve sleeping.If you want to hit 60fps, your frame time needs to be less than 16/17 milliseconds. On many operating systems, sleep() has a granularity of about 10 milliseconds, so you are more than halving the amount of time you have to complete your game computations and still hit your frame deadline.You can often get away with it on new machines, but if you want to support older hardware its best not to have an unconditional sleep statement. For example, you could have a check that says if the frame time is less than X ms, to add a sleep.Another solution is to have a fixed internal frame timer, and to use an accumulator to determine when to call the game logic. Something like this.   ;Quote:Original post by BattleMetalChrisFixed it now anyway. I added a Thread.sleep(1) to the end of the gameloop and it works fine now. Seems the framerate was so high deltaTime was almost zero.No, it was zero.Quote:(currentTime - oldTime) * 0.001;They are both longs, and currentTimeMilis can have resolution of more than 1 ms, meaning it can frequently return 0 for delta.Returning value as double when float is needed is also redundant.The problem with sleep is that on a machine with low timer resolution, your problem will show up again, since sleep of 1ms will not be enough.A better way to update the loop is something like this:private void gameLoop(){  long currentTime = System.currentTimeMillis();  if (currentTime > oldTime) {    double delta = (currentTime - oldTime) * 0.001;    oldTime = currentTime;   // update the rest  } else {    Thread.Sleep(1);  }This prevents the situation where timer doesn't have enough resolution. This happens either due to JVM implementation or hardware issues.Also, if using appropriate Java version, use System.nanoTime(). Modified update loop is still needed due to same resolution issues, but it offers potentially higher resolution timer.Also, very small time deltas are undesirable due to numeric resolution problems. Ideally, the loop would look something like this:private void gameLoop(){  double currentTime = System.nanoTime();  if (currentTime - oldTime > 10e6) { // deltaTime > 10ms    double delta = (currentTime - oldTime) / 1e6;    oldTime = currentTime;   // update the rest  } else {    Thread.Sleep(1);  }Also, fixed time step is preferred for such updates. This article covers the details, all of which are proven methods on how to avoid precision and resolution issues.   ;  If it was zero, then why were my entities rotating then? They definitely were.I really don't get that part.EDIT: aha, I think I get it. :)If the framerate is something stupid high, like 200K fps, and then almost every frame deltaTime *will* be zero due to the granularity of timeInMillis, but every 200th frame, the PC's clock will have advanced one millisecond so for that frame deltaTime *will* be >0 and my entities will rotate. As this still happens 1000 times a second it looks like they are rotating normally, but deltaTime is 0 on so many frames I only see the text as showing it as 0.This sounds right, am I correct?   ;Quote:Original post by BattleMetalChrisIf the framerate is something stupid high, like 200K fps, and then almost every frame deltaTime *will* be zero due to the granularity of timeInMillis, but every 200th frame, the PC's clock will have advanced one millisecond so for that frame deltaTime *will* be >0 and my entities will rotate. As this still happens 1000 times a second it looks like they are rotating normally, but deltaTime is 0 on so many frames I only see the text as showing it as 0.This sounds right, am I correct?Most likely, yes. This is why either fixed update rate, or some minimal step are preferred.Update rates above 200 or so are not practical anyway, since dt becomes so small that floating point accuracy errors start affecting the rest of the simulation, especially if other numbers are fairly large.   