BMan
JGO n00b  Posts: 26
Java games rock!
|
 |
«
Reply #4 on:
2005-06-25 14:00:54 » |
|
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(); } }
|