Results 1 to 3 of 3

Thread: How to rotate a triangle?

  1. #1
    Member Newbie
    Join Date
    Jul 2009
    Posts
    33

    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. #2
    Senior Member Regular Contributor
    Join Date
    Jun 2009
    Location
    FL , USA
    Posts
    192

    Re: How to rotate a triangle?

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

  3. #3
    Member Newbie
    Join Date
    Jul 2009
    Posts
    33

    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.

Similar Threads

  1. OpenGL Android - Rotate objects other than a cube or triangle
    By androidDev in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-20-2016, 07:03 PM
  2. Replies: 0
    Last Post: 11-13-2015, 10:38 PM
  3. Replies: 3
    Last Post: 10-09-2012, 09:39 PM
  4. how to rotate a cube an efficent rotate ???!!!!!!
    By Nader El Masry in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 04-08-2010, 08:35 PM
  5. Replies: 6
    Last Post: 10-09-2000, 09:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean