Hello World ~ Making Objects Move
- We are going to start with the code from the previous tutorial: a JFrame with a GLCanavs that draws a teapot.
- Create a Swing Timer as a class level variable and a class level variable that keeps track of the time.
- YourTimer = new Timer(TIMER_DELAY, new ActionListener() { public void actionPerformed(ActionEvent e) {tick(); }});
- Create a function called tick() (or what ever you named the target of your timer) and inside the function update the time and refresh the display.
- yourTimeVariable += time_passed; YourGLCanvas.display()
- Before you draw your teapot you can transform, scale, and rotate it.
- You must do your transformations in the order above (transform => scale => rotate). This is because of the way OpenGL performs matrix math. If you did it in another order you would have very different result.
- Transform
- glTranslated( x, y, z)
- The d at the end does not mean translated past tense, it means translate with double values.
- Scale
- glScaled( x, y, z)
- Rotated
- glRotated( amount in degrees, x, y, z)
- In glRotated the x, y, and z are not amounts of change (although they can be but that gets confusing). You should treat them as boolean values: 1.0 is on and 0.0 is off.
- In the sample code, I am doing time based movements with transformations and rotations. Also, I am not storing the time. Instead I am computing the radians every time the timer fires.
- The transformations in the code
-
gl.glTranslated(2.0 * Math.cos(_radians), 0.0, 2.0 * Math.sin(_radians));
gl.glRotated(-Math.toDegrees((2.0 * _radians)), 0.0, 1.0, 0.0); - Computing the radians
-
_radians += DELTA; if (_radians > 2 * 3.14){ _radians = 0; } - That's all!
