PDA

View Full Version : How to rotate a triangle?



marce
07-14-2009, 07:34 AM
Hello,

I have drawn a 3D triangle and I would like the user to be able to rotate the triangle by pressing the left, right, up or down buttons.
I have succeeded with the rotation by using the following code, but if the user rotates the triangle to, letīs say, the left (by pressing the left button) and then rotate it upwards (by pressing the up button), the rotation starts over, which means the triangleīs position is getting the start values, but I want the triangle to continue the rotation from where it last stopped. The rotation continues from where it last stoppep if I just rotate it with the left and right buttons OR just rotate it with the up and down buttons, but if I mix the rotation by rotating it up and then rotate it to left, then it starts over. Here comes the code:

if(keyDirectionLeftRight)
{
gl.glRotatef(cameraView, 0.0f, 1.0f, 0.0f);
gl.glRotatef(cameraView, 0.0f, 1.0f, 0.0f);
gl.glRotatef(cameraView, 0.0f, 1.0f, 0.0f);
gl.glRotatef(cameraView, 0.0f, 1.0f, 0.0f);
}
else // if the user has pressed up och down button, then keyDirectionLeftRight = false
{
gl.glRotatef(cameraView, 1.0f, 0.0f, 0.0f);
gl.glRotatef(cameraView, 1.0f, 0.0f, 0.0f);
gl.glRotatef(cameraView, 1.0f, 0.0f, 0.0f);
gl.glRotatef(cameraView, 1.0f, 0.0f, 0.0f);
}

And here goes the KeyListener:

public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
cameraView += 3.0f;
System.out.println(cameraView);
keyDirectionLeftRight = true;
}
else if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
cameraView -= 3.0f;
System.out.println(cameraView);
keyDirectionLeftRight = true;
}
else if(e.getKeyCode() == KeyEvent.VK_UP)
{
cameraView += 3.0f;
System.out.println(cameraView);
keyDirectionLeftRight = false;
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
cameraView -= 3.0f;
System.out.println(cameraView);
keyDirectionLeftRight = false;
}
}]

awhig
07-15-2009, 02:56 AM
Are you using static variables in your code??? They should help you out as they retain values.

marce
07-15-2009, 08:46 AM
Actually, I got it working yesterday. I have no clue what change I made or what it was that made it working. I guess it was something about the updating of the rotate-variable values, but Iīm not sure. :)