Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Rotating a moving object

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2000
    Posts
    7

    Rotating a moving object

    Hi,

    I'm encountering the following problem. I've got a dinosaur which I'd like to rotate around a point that is not the origin. Well that's not the problem, but here it comes.

    I'm moving the dinosaur using the numeric keyboard, 4+6 rotates, 8 moves, 2 stops the dinosaur. I cannot rotate the dinosaur and then translate it into it's correct position. This is my code:


    void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    gluLookAt(camera[0], camera[1], camera[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    drawEnvironment();

    if (move)
    moveDino();

    glTranslatef(10.5, 0.0, 1.55);
    glRotatef(angle, 0.0, 1.0, 0.0);
    glTranslatef(-10.5, 0.0, -1.55);

    drawDino();

    glFlush();
    glutSwapBuffers();
    }


    void keyboard(unsigned char key, int posx, int posy)
    {
    if (tolower(key) == 'q')
    exit(0);

    if (key == '8')
    move = GL_TRUE;
    else if (key == '4')
    rotateDino(left);
    else if (key == '6')
    rotateDino(right);
    else if (key == '2')
    move = GL_FALSE;
    }

    ---

    As you can see, I try to rotate the dino itself around the y-axis at the point (10.5, 0.0, 1.55). But I have to translate the dino to the position (dino_pos[0], dino_pos[1], dino_pos[2]). I've tried to add those coordinates to the rotation point, but that doesn't work, at least not in the way I'm doing it .

    Can anybody please help me out (trying to be really desperate here...) ??

  2. #2
    Junior Member Newbie
    Join Date
    Dec 2000
    Location
    Hungary
    Posts
    7

    Re: Rotating a moving object

    You always rotate around the origin. But you can fake a rotation around an arbitary point by moving the objects to make the new center point to be at the origin.
    So if you want to rotate around (10.5,0.0,-1.5) then move your dino with (-10.5,0.0,1.5), rotate it, then move back by (10.5,0.0,-1.5) thus you've rotated it around the new center point

    Hope this helps
    FooLman
    FooLman/United Force
    "Addicted to MAV"

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2000
    Posts
    7

    Re: Rotating a moving object

    Thanx for your help, but I already do that...

    The problem is translating the object to another position than (10.5, 0.0, 1.55). If I do that, the dino is making circles instead of rotating on the same position.

  4. #4
    Member Contributor
    Join Date
    Feb 2000
    Location
    Gothenburg, Sweden
    Posts
    68

    Re: Rotating a moving object

    Hello !

    Try this :

    ...
    glPushMatrix();
    glTranslatef(x,y,z);
    glRotatef(angle,0,1,0);
    drawDino();
    glPopMatrix();
    ...

    I'm not home so I can't try it but it might work...

    Osku

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2000
    Posts
    7

    Re: Rotating a moving object

    Thanx for your reply,
    but after the rotation the dino has to be translated into its current position. That's the tricky business, at least for me...

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2000
    Posts
    26

    Re: Rotating a moving object

    Code :
    glTranslatef(10.5, 0.0, 1.55);
    glRotatef(angle, 0.0, 1.0, 0.0);
    glTranslatef(-10.5, 0.0, -1.55);
    This code makes no sence to me.
    I am not sure but i think it's the same as
    glRotatef(angle, 0.0, 1.0, 0.0) only.

    Maybe You need something like this ...
    Code :
    glRotatef(angle, 0.0, 1.0, 0.0);
    glTranslatef(10.5, 0.0, 1.55);
    drawDino();
    glTranslatef(-10.5, 0.0, -1.55);
    [CS]
    -<[CS]>-

  7. #7
    Guest

    Re: Rotating a moving object

    I think this is what you need

    getNewPos()
    {
    float mat[4][4];
    glGetFloatv(GL_MODELVIEW_MATRIX,(float *)mat);

    Z+=deltaz*mat[2][2]+deltay*mat[2][1]+deltax*mat[2][0];
    Y-=deltaz*mat[1][2]+deltay*mat[1][1]+deltax*mat[1][0];
    X+=deltaz*mat[0][2]+deltay*mat[0][1]+deltax*mat[0][0];
    }

    where Z is your original z position
    X is your original x position
    and Y is your original y position
    and the deltas are the speed you want your dino to move ....

    run this everyframe (or time step or whatever)

    then in the display func do this

    glRotatef(-roll, 0,0,1);
    glRotatef(pitch,1,0,0);
    glRotatef(-yaw,0,1,0);
    glTranslatef(X, -Y, Z);

    of course your original xyz and rpy should be

    (10.5, 0.0, 1.55)
    and
    (0,0,0) respectively

    //to move to a _PARTICULAR_ position is easy too just figure out the deltas

    deltaz= zdest-zorig
    deltax= xdest-xorig
    deltay= ydest-yorig


    cheers

    write back to tell me if this works

  8. #8
    Member Contributor
    Join Date
    Dec 2000
    Location
    Connecticut/New Jersey/Nevada
    Posts
    51

    Re: Rotating a moving object

    I think you've been misunderstood. You want the dinosaur to be at position {10.5,0,1.55} and to rotate in place there, right? When you say you want him to rotate around that point, people think you want him to move in a circle around that point.

    glTranslate is not relative and is not affected by glRotate. However, glRotate IS affected by glTranslate. If the dino is centered around the origin, and you call glRotate, he'll rotate in place. If you call glTranslate first, then glRotate, he'll rotate in a circle around the origin, since he no longer is at the origin.

    Basically, the radius of his rotation circle is his distance from the origin. If he's at the origin, that distance is zero. To create third-person games, you translate the camera back a bit (0,0,-Amount) and then call glRotate.

    Anyway, what you want is this:

    Code :
     
    glPushMatrix( ); // Save ModelView
    //
    glRotatef( angle, 0, 1, 0 );
    glTranslatef( 10.5, 0, 1.55 );
    drawDino( );
    //
    glPopMatrix( ); // Restore ModelView
    [This message has been edited by CGameProgrammer (edited 12-12-2000).]
    ~CGameProgrammer( );

  9. #9
    Guest

    Re: Rotating a moving object

    What my code should do is rotate you around an arbitrary point in space (X, Y, Z) and then you can translate with respect to that point and your rotation

    so if you are at 1,0,0 and have rotated so the dino is facing the positive x axis and you want to move forward you just move and you will be rotating around 2,0,0 and you will be there too

    when I mean move I mean move the center of your object and when I say rotate around I mean the center of your object is at that point and you are spinning about it (not in a circle)

  10. #10
    Junior Member Newbie
    Join Date
    Dec 2000
    Posts
    7

    Re: Rotating a moving object

    What my program should do is this. This is my dino:

    x---------------|
    |---------------|
    |-------------o-|
    |---------------|
    |---------------|

    'x' is at the origin, and 'o' is the position to rotate around. This is because 'x' is the end of the dino's tail, and 'o' is the point between the legs. So if I rotate around 'o' it looks a bit more real. I've used Edward Angel's book to see how to rotate around a point that is not the origin (see page 180-181 if you got it too). This is the code that is used:

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(4.0, 5.0, 6.0);
    glRotatef(45.0, 1.0, 2.0, 3.0);
    glTranslatef(-4.0, -5.0, -6.0);

    So first you translate, then rotate, and then translate back. This works if my dino (read pos 'x') is at the origin. So I do this:

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(10.5, 0.0, 1.55);
    glRotatef(angle, 0.0, 1.0, 0.0);
    glTranslatef(-10.5, 0.0, -1.55);

    And it works if my dino is at the origin. But now the dino position is variable, so the dino is at (dino_pos[0], dino_pos[1], dino_pos[2]). I still need to rotate around the point (10.5, 0.0, 1.55), but when I want to translate it back to the correct position, it doesn't work. Now it rotates around in circles, instead of rotating at the same point.

    I've tried translating the dino back to (dino_pos[0] - 10.5, 0.0, dino_pos[2] - 1.55), and I've also tried to switch the signs, but nothing works.

    I hope this makes the problem a bit clearer.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 06-24-2013, 12:22 AM
  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 and moving camera for FPS
    By El in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 01-19-2004, 12:34 PM
  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