# Thread: How to rotate a triangle?

1. ## How to rotate a triangle?

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:
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:
Code :
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;
}
}]

2. ## Re: How to rotate a triangle?

Are you using static variables in your code??? They should help you out as they retain values.

3. ## Re: How to rotate a triangle?

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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•