Results 1 to 6 of 6

Thread: Moving and Rotating Objects

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    5

    Moving and Rotating Objects

    Hi

    I am new to programming in OpenGL and any help will be very much appreciated

    I am currently trying to make the rectangle that I have drawn move in a circle. I was told that it will require some Mathematics regarding differentiating the x and y positions of the rectangle with respect to time and angle.

    How do I go about doing that?

    Please help. Thanks!

  2. #2
    Member Contributor
    Join Date
    Mar 2008
    Location
    Singapore
    Posts
    67

    Re: Moving and Rotating Objects

    hi cwzcwz,

    Provided I understood you correctly, I think what you want to do it rather straight forward.

    Just use glRotatef(..) calls to make your rectangle rotate. I'll assume you want your rectangle about the origin about the z-axis.


    This is the pseudo code.

    glMatrixMode( GL_MODELVIEW );
    glPushMatrix();

    /////////////////////////////////////////////
    glRotatef( angle, 0.0, 0.0, 1.0 );
    ////////////////////////////////////////////

    glBegin( GL_QUADS );

    glVertex2f( x1, y1 );
    glVertex2f( x2, y1 );
    glVertex2f( x2, y2 );
    glVertex2f( x1, y2 );

    glEnd();


    glMatrixMode( GL_MODELVIEW );
    glPopMatrix();



    Let me know if this is what you wanted.

  3. #3
    Junior Member Newbie Optik's Avatar
    Join Date
    Jun 2009
    Location
    Los Angeles, CA
    Posts
    22

    Re: Moving and Rotating Objects

    You're question is very vague.
    Please elaborate a little more in your next post.

    What I understand from you're question is that you want to rotate the rectangle about its center but it is unclear whether you want it to rotate uniformly or in a predetermined haphazard fashion.

    We'll if you want uniform rotation its simple. All you need to do is increment (or decrement) you're angle in the pseudo code provided by MatchStickMan in certain steps (say 5 degrees every step) and run you're animation using the glutTimerFunc.

    This is nothing but a timer call-back where you can run any segment of you're code periodically. For animations usually you would simply want to redraw you're screen so you call a glutPostRedisplay.

    heres the pseudo code for the timer call back

    void timer(int i)
    {
    glutPostRedisplay();
    glutTimerFunc(TIME,timer,1); //here TIME is you're
    // timer period in
    // milliseconds.
    }

    you can register the time call back by including the following line in you're main function

    glutTimerFunc(TIME,timer,1);

    As for the haphazard rotation. You do need certain math depending on you're requirements.

    For a controlled rotation you may want to refer to my post "Rotating a cube with the mouse".

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    5

    Re: Moving and Rotating Objects

    Sorry for not being clear enough.

    What I meant was something like having a rectangle and moving it around a pivot, not the centre of the rectangle but something like about an imaginary point on the screen.

    Eventually I would like to develop it to move it with the help of differential equations.

  5. #5
    Junior Member Newbie Optik's Avatar
    Join Date
    Jun 2009
    Location
    Los Angeles, CA
    Posts
    22

    Re: Moving and Rotating Objects

    That would be pretty easy. First translate to the point where you would like to place you're axis. Then rotate the rotate by the amount you would like to rotate in the desired sense. Finally translate to a point which places you're rectangle at the required distance from your axis. Then draw your rectangle.

    Here is an example where I rotate the rectangle about one of its vertex. Assume the vertices are v1 v2 v3 v4.

    Code :
    void draw()
    {
       ...
       ...
     
       glTranslatef(v1);
       glRotatef(angle_step,0.0f,0.0f,1.0f);
       glTranslatef(center_of_rect);
       draw_rect();
    }

    I hope this cleared your doubt.

  6. #6
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    5

    Re: Moving and Rotating Objects

    Hi

    Thanks all for the response. Got it working already

    Cheers!

Similar Threads

  1. Rotating Objects - Light Rotating not expected
    By SparkyNZ in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 08-12-2015, 07:52 PM
  2. Moving/rotating object
    By SuperSQL in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-17-2012, 06:36 PM
  3. rotating and moving a box...
    By owen_holiday in forum OpenGL: Windows
    Replies: 1
    Last Post: 01-31-2004, 06:35 PM
  4. Rotating a moving object
    By Munky in forum OpenGL: Basic Coding
    Replies: 18
    Last Post: 12-17-2000, 10:36 AM
  5. Moving and rotating a "camera"
    By skippyj777 in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 08-23-2000, 09:36 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