Having problems with timers

Started by paul188, October 17, 2015, 16:45:05

Previous topic - Next topic

paul188

Hey guys eehm, im having a problem with the lwjgl timer...i get a negative value back so heres my src code (made out of this tutorial:https://www.youtube.com/watch?v=0xsHwpzkNxQ)

if everything works, ishould get 16 or 17 back or so sth ;)

package com.base.engine;

import org.lwjgl.opengl.Display;

public class MainComponent {
   public static final int WIDTH = 800;
   public static final int HEIGHT = 600;
   public static final String title = "3d engine";
   
   

   private boolean isRunning;
   private Game game;

   public MainComponent() {
      isRunning = false;
      game = new Game();
   }

   public void start()// when we want to start
   {
      if (isRunning)
         return;
      run();
   }

   public void stop()// when we want to stop
   {
      if (!isRunning)
         return;
      isRunning = false;
   }
   

   private void run()// while the game is running
   {
      isRunning = true;
      
      long currentTime=Time.getTime();

      while (isRunning) {
         
         long thisTime=Time.getTime();

         System.out.println((Time.getDelta(thisTime, currentTime)));
         
         currentTime=Time.getTime();
         thisTime=0;

            if (Window.isCloseRequested()) {
               stop();
            }
            //Update the game
            game.input();
            game.update();
            game.render();
            render();
            }
          cleanUp();
         }


   

   private void render()// rendering
   {
      game.render();
      Window.render();
   }

   private void cleanUp()// Game has finished running and we want to delete                  // everything
   {
      Window.dispose();
   }

   public static void main(String[] args) {
      Window.createWindow(WIDTH, HEIGHT, title);

      MainComponent game = new MainComponent();
      game.start();
   
   }
}

And then heres my time class...(the window class doesnt matter)

package com.base.engine;

import org.lwjgl.Sys;

public class Time {
   
    public static long getTime(){
           return (Sys.getTime()*1000)/Sys.getTimerResolution();
       }
       public static int getDelta(long lastFrame, long currentTime){
           int delta=(int) (currentTime-lastFrame);
           lastFrame=currentTime;
            
           return delta;
       }

}
Can anyone tell me what im doing wrong ???Very thanful

Paul;) ???


Kai

You might notice that your time delta is just the negative of what you expect.
The reason is that in your game loop "currentTime" is always the point in time in the past and "thisTime" is always the current point in time. So "currentTime" should always be bigger than "thisTime."
But when you call Time.getDelta() this method expects the first parameter to be the time in the past and the second parameter to be the current time.
So in your game loop just swap the arguments to the call Time.getDelta().
Also, Time.getDelta() contains an assignment to the local argument "lastFrame" which will have no effect (it will not be visible by the caller!).

paul188

Oh...thank you...i overlooked that:)
Youre right;)