[Solved]So I think I have a problem with my fps

is my game loop right? :S



public void run() {
		while (running) {
			long time = System.currentTimeMillis();

			update();
			render();
			draw();
			
			long deltaTime = System.currentTimeMillis() - time;
			time = fps - deltaTime;
			currentFps = time;
			if (time > 0) {
				try {
					Thread.sleep(time);
				} catch (Exception e) {
					System.out.println(e);
				}
			}
		}
		setVisible(false);
	}


fps is set to 60

your fps variable appeared out of nowhere.
or do you mean int fps = 1000/60?

If you don’t care about an exact fps, this works:
then multiply velocities etc by the deltaTime variable.

deltaTime should be calculated before the processing so you can use it in the current frame.


public void run() 
{
      long lastFPS = 0;
      int fps = 0;
      int fpsCount = 0;

      long lastTime = System.currentTimeMillis();
      while (running) {
         long time = System.currentTimeMillis();
         long deltaTime = time - lastTime;
         lastTime = time;
         
         if (time - lastFPS > 1000)
         {
               lastFPS = time;
               fps = fpsCount;
               fpsCount = 0;
         }
         else
         {
                  fpsCount++;
         }

         update();
         render();
         draw();
         
         int sleepTime = 1000/60; //sleep 16ms per frame = 60fps
            try {
               Thread.sleep(sleepTime);
            } catch (Exception e) {
               System.out.println(e);
            }
         }
      }
      setVisible(false);
   }

Well my problem isn’t so much the calculation of the fps (I think). Maybe I wasn’t clear… But I got a lot of lag in my game… And my cycle only takes about 0.1 sec… Maybe it not there the problem is? :S

did you try using the loop I posted? I think its because you are allowing your program to sleep long.
Also, 0.1 seconds for a cycle is slow. - most games run at about 60fps = (0.016 seconds). 10 frames per second will look laggy.

i meant 0.001 :stuck_out_tongue: forgot two 0s there :stuck_out_tongue:

I tried to make it work, but maybe I should give it another try… I’ll be back with a post in a few mins

Well it dosen’t help, baybe I should try post a link with the game, so you/others can see what I mean… :slight_smile:

http://sudopwn.net/Files/Zompocalypse.zip

System.currentTimeMillis() has a very low resolution on some machines, especially Windows. This causes it to be quite unaccurate. Try using this trick in your main method before your game starts:


new Thread() {
    {
        setDaemon(true);
        start();
    }

    public void run() {
        while(true) {
            try {
                Thread.sleep(Long.MAX_VALUE);
            }
            catch(Exception exc) {}
        }
    }
}

ra4king I’m using the method you said to me, about using the update method to check for inputs, and it like if the program doesn’t react on every input, but only some times… Don’t know if it’s there the problem is?

tried your solution, but no luck

So I tried your loop, hopefully I understood it right. It looks like this now:



public void run() {
		double startTime, endTime, deltaTime, time;
		double timeCounter = 0;
		int framesDrawn = 0;

		while (running) {
			startTime = System.currentTimeMillis();

			update();
			render();
			draw();

			endTime = System.currentTimeMillis();

			deltaTime = endTime - startTime;
			time = fps - deltaTime;

			timeCounter += deltaTime;
			if(timeCounter > 1000){
				currentFps = framesDrawn / 1000;
				timeCounter = 0;
				framesDrawn = 0;
			}else{
				framesDrawn++;
			}

			long sleepTime = (long) (1000 / 60);

			try {
				Thread.sleep(sleepTime);
			} catch (Exception e) {
				System.out.println(e);
			}

		}
		setVisible(false);
	}


but it says I have 1.7E-6 fps o.O

So did some testing on my game, and found out my loop takes 500+ milliseconds, that could be the problem for the lag… :stuck_out_tongue: