yet another Timer question

I apolgize ahead of time for this question. I’ve gone through all the forums trying to figure out how to add a simple little timer
to my 3d pong game made with JOGL. It seems like lots of folks have different ways of doing it, so would like to ask of a simple one.
This is going to be a very basic game, so trying to avoid 3rd party stuff like GateTimer.

I am doing this in 1.5, so looks like I need to use the nanoTimer().
But would someone be able to supply a little help on what lines of code to use?

Does all of the Timer code go in display() ?

Again sorry about this.

Also, as I’m doing this, I am creating a walkthrough/ride along of each step to add a new piece to the game. I am going to turn it into a tutorial that may be helpful to newbies like myself. Not sure where the best place to submit/host this would be, if there is anything like that on these sites, please let me know.

thanks in advance for all the help!!!

What’s the timer used for ? for FPS or some other stuff?

As to where to put the timer, I think it depends the thread model you use.

If you use Animator like rendering that call JOGL display in your own thread then you can put the timer where you like, however, if you leave the rendering thread controlled by system I’m afraid that you have to put the code in display.

Sorry, poorly described question above.

I am looking for help with a timer to control the movement of my objects: ball/paddles,
So they just don’t move based off of the speed of your cpu, but with some control.

thanks!

ok, found the following on this forum elsewhere, not sure where to put it, but going to mess around with it.
Any hints would be appreciated. I’m guessing this goes in the display() method.

int frameCount = 0;
long startTime = System.nanoTime();
final int NANO_FRAME_LENGTH = 1000000000/60; //frame length in nanoseconds for 60fps
while(true)
{
// game code
frameCount++;
bs.show();
while((System.nanoTime()-startTime)/NANO_FRAME_LENGTH <frameCount)
{
Thread.yield();
}
}

OK, this may be helpful, I"ll post what I have so far, basically it’s a ball moving left to right.
How can I get this to be constant with the timer, versus just going as fast as your computer flips through the code?
Thanks for help in advance! Sorry for such newbie question.

import java.awt.;
import java.awt.event.
;
import net.java.games.jogl.*;

public class Pong2
{
static Animator animator = null;

static class Renderer

implements GLEventListener, KeyListener
{

//Variables
static GLUquadric q;			// Quadratic For Drawing A Sphere
float ballx = 0.0f, bally = 0.0f, ballz = 0.0f;			//location of ball   


public void display(GLDrawable gLDrawable)
{
  final GLU glu = gLDrawable.getGLU();
  final GL gl = gLDrawable.getGL();
  gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  gl.glLoadIdentity();
  
  //Set up Camera  (first 3 = camara loc, second 3 where we look)
  glu.gluLookAt(0, 10, 10, 0, 0, 0, 0, 1, 0);
      
  //Draw Ball
    gl.glPushMatrix();
    gl.glColor3f(1.0f,1.0f,0.0f);			// Set The Color To Yellow
    gl.glTranslatef(ballx, bally, ballz);
    glu.gluSphere(q, .5f, 32, 16);	
    gl.glEnd();
    gl.glFlush();
    gl.glPopMatrix();
    
    ballx=ballx+.025f;
}


public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged)
{
}

public void init(GLDrawable gLDrawable)
{
  final GL gl = gLDrawable.getGL();
  final GLU glu = gLDrawable.getGLU();
  gl.glShadeModel(GL.GL_SMOOTH);              // Enable Smooth Shading
  gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // Black Background
  gl.glClearDepth(1.0f);                      // Depth Buffer Setup
  gl.glEnable(GL.GL_DEPTH_TEST);							// Enables Depth Testing
  gl.glDepthFunc(GL.GL_LEQUAL);								// The Type Of Depth Testing To Do
  gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);	// Really Nice Perspective Calculations
  gLDrawable.addKeyListener(this);
  
  //Initialize for Sphere
  q = glu.gluNewQuadric();                                    // Initialize Quadratic
  glu.gluQuadricNormals(q,GL.GL_SMOOTH);                      // Enable Smooth Normal Generation
  glu.gluQuadricTexture(q,false);                             // Disable Auto Texture Coords
}

public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height)
{
  final GL gl = gLDrawable.getGL();
  final GLU glu = gLDrawable.getGLU();

  if (height <= 0) // avoid a divide by zero error!
    height = 1;
  final float h = (float)width / (float)height;
  gl.glViewport(0, 0, width, height);
  gl.glMatrixMode(GL.GL_PROJECTION);
  gl.glLoadIdentity();
  glu.gluPerspective(45.0f, h, 1.0, 100.0);
  gl.glMatrixMode(GL.GL_MODELVIEW);
  gl.glLoadIdentity();
}

public void keyPressed(KeyEvent e)
{
  //Checks to see if Escape is pressed
  if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
  {
    animator.stop();
    System.exit(0);
  }
}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

}

public static void main(String[] args)
{
//Generic Set up of windowed view
Frame frame = new Frame(“Pong”);
GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(640, 480);
animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
animator.stop();
System.exit(0);
}
});
frame.show();
animator.start();
canvas.requestFocus();
}
}

OK folks, I think I figured it out. Seems to be doing what I want…

import java.awt.;
import java.awt.event.
;
import net.java.games.jogl.*;

public class Pong2
{
static Animator animator = null;

static class Renderer

implements GLEventListener, KeyListener
{

//Variables
static GLUquadric q;			// Quadratic For Drawing A Sphere
float ballx = 0.0f, bally = 0.0f, ballz = 0.0f;			//location of ball   


public void display(GLDrawable gLDrawable)
{
  final GLU glu = gLDrawable.getGLU();
  final GL gl = gLDrawable.getGL();
  gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  gl.glLoadIdentity();
  
  
  int frameCount = 0;
  long startTime = System.nanoTime();
  final int NANO_FRAME_LENGTH = 1000000000/60; //frame length in nanoseconds for 60fps
  //while(true)
  //{
  //       game code
  
  	
    //Set up Camera  (first 3 = camara loc, second 3 where we look)
    glu.gluLookAt(0, 10, 10, 0, 0, 0, 0, 1, 0);
      
  //Draw Ball
    gl.glPushMatrix();
      gl.glColor3f(1.0f,1.0f,0.0f);			// Set The Color To Yellow
    gl.glTranslatef(ballx, bally, ballz);
    glu.gluSphere(q, .5f, 32, 16);	
    gl.glEnd();
      gl.glFlush();
      gl.glPopMatrix();
      
      ballx=ballx+.025f;
  	
  	
  	
  	
  frameCount++;
  //bs.show();
  while((System.nanoTime()-startTime)/NANO_FRAME_LENGTH <frameCount)
  {
    Thread.yield();
  }
  //} 

  

}


public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged)
{
}

public void init(GLDrawable gLDrawable)
{
  final GL gl = gLDrawable.getGL();
  final GLU glu = gLDrawable.getGLU();
  gl.glShadeModel(GL.GL_SMOOTH);              // Enable Smooth Shading
  gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // Black Background
  gl.glClearDepth(1.0f);                      // Depth Buffer Setup
  gl.glEnable(GL.GL_DEPTH_TEST);							// Enables Depth Testing
  gl.glDepthFunc(GL.GL_LEQUAL);								// The Type Of Depth Testing To Do
  gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);	// Really Nice Perspective Calculations
  gLDrawable.addKeyListener(this);
  
  //Initialize for Sphere
  q = glu.gluNewQuadric();                                    // Initialize Quadratic
  glu.gluQuadricNormals(q,GL.GL_SMOOTH);                      // Enable Smooth Normal Generation
  glu.gluQuadricTexture(q,false);                             // Disable Auto Texture Coords
}

public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height)
{
  final GL gl = gLDrawable.getGL();
  final GLU glu = gLDrawable.getGLU();

  if (height <= 0) // avoid a divide by zero error!
    height = 1;
  final float h = (float)width / (float)height;
  gl.glViewport(0, 0, width, height);
  gl.glMatrixMode(GL.GL_PROJECTION);
  gl.glLoadIdentity();
  glu.gluPerspective(45.0f, h, 1.0, 100.0);
  gl.glMatrixMode(GL.GL_MODELVIEW);
  gl.glLoadIdentity();
}

public void keyPressed(KeyEvent e)
{
  //Checks to see if Escape is pressed
  if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
  {
    animator.stop();
    System.exit(0);
  }
}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

}

public static void main(String[] args)
{
//Generic Set up of windowed view
Frame frame = new Frame(“Pong”);
GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(640, 480);
animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
animator.stop();
System.exit(0);
}
});
frame.show();
animator.start();
canvas.requestFocus();
}
}

yes, it can work, but not the best way. The best way should write an custom animator by yourself :slight_smile:

And I don’t think using the static class a good method.