Results 1 to 10 of 10

Thread: Rotating around an object

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    10

    Rotating around an object

    Hello
    Imhaving a little problem with rotating around a cube. First tried using glRotatef ariund the three different vectors. this seemed to work nice, but... When i have rotated 90 degrees around the y axis and then want to rotate over the cube( "standing" along the x axis ) and want to rotate "over the cube" the cube spins like a wheel. this is so because, when i push the up button i increase the rotation around the x axis. but when im not standing orthogonally on the x axis, i must also rotate around the z axis too...

    i have tried building up a projection matrix this way:
    void buildMatrix()
    {
    float phi = mat.xgrad;
    float kappa = mat.ygrad;
    float omega = mat.zgrad;

    mat.trans[0] = cos(kappa)*cos(omega);
    mat.trans[1] = -sin(omega);/*-cos(kappa)*-sin(omega);*/
    mat.trans[2] = sin(kappa);

    mat.trans[3] = 0.0f;

    mat.trans[4] = sin(omega);/*sin(phi)*sin(kappa)*cos(omega) + cos(phi)*sin(omega);*/
    mat.trans[5] = cos(phi) * cos(omega);/*-sin(phi)*sin(kappa)*sin(omega) + cos(phi)*cos(omega);*/
    mat.trans[6] = -sin(phi);/**cos(kappa);*/

    mat.trans[7] = 0.0f;

    mat.trans[8] = -sin(kappa); /*-cos(phi)*sin(kappa)*cos(omega) + sin(phi)*sin(omega);*/
    mat.trans[9] = sin(phi); /*cos(phi)*sin(kappa)*sin(omega) + sin(phi)*cos(omega);*/
    mat.trans[10] = cos(phi)*cos(kappa);

    mat.trans[11] = 0.0f;

    mat.trans[12] = 0.0f;//mat.xgrad;
    mat.trans[13] = 0.0f;//mat.ygrad;
    mat.trans[14] = 0.0f;//mat.zgrad;

    mat.trans[15] = 1.f;

    }

    this doesnt work at all. i need tips! how do i freely move around an object?

  2. #2
    Member Contributor
    Join Date
    Nov 2001
    Posts
    77

    Re: Rotating around an object

    I hope I'm understanding this correctly. You want to be able to move freely around in 3D space? The easiest way I know of to do that is simply keep three vectors: your position, a forward or "looking" vector, and the up vector. Any other vectors can be calculated from those three. Also, you can then use gluLookAt() which will set up the projection matrix.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    10

    Re: Rotating around an object

    Thanks. this was the solution i also came up with after some drawing and thinking. I want to rotate around it as i was on the boundary of an sphere. what is the most efficient way to do this? using polar coordinates? wouldnt this be computational expensive?

  4. #4
    Member Contributor
    Join Date
    Nov 2001
    Posts
    77

    Re: Rotating around an object

    If you use the standard math sine and cosine functions, yes. However, if I remember correctly, spherical coordinates are:

    x = r*cos(theta)*cos(phi)
    y = r*cos(theta)*sin(phi)
    z = r*sin(theta)

    where theta is in the x-y plane and phi is in the y-z plane.

    If you keep a table of sine values in memory {don't forget: cos(x) = sin(pi/2+x)}, you can refer to these up to 20 times faster than actually calculating the sine of some arbitrary angle. Unfortunately, you might have some granularity problems, but with enough values, this problem becomes neglible.

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    10

    Re: Rotating around an object

    i used gluLookAt an came up with this solution:
    void calculateAndLook(){
    //regner ut posisjon i kuleskallet
    eye[Y] = radius * sin(xzrot*M_PI/180);
    eye[X] = radius * cos(xzrot*M_PI/180) * sin(yrot*M_PI/180);
    eye[Z] = (radius * cos(xzrot*M_PI/180) * cos(yrot*M_PI/180));

    //renger ut hva som er opp, tangenten til kuleskallet
    up[Y] = 1.0 * sin(xzrot*M_PI/180 + (M_PI/2));
    up[X] = 0.0;
    up[Z] = 0.0;

    //center er i origo
    center[X] = 0.0f;
    center[Y] = 0.0f;
    center[Z] = 0.0f;

    //ser
    gluLookAt(eye[X],eye[Y],eye[Z],center[X],center[Y],center[Z],up[X],up[Y],up[Z]);
    }
    where yrot is the rotation around the y axis and xzrot is the rotation of the xz plane about the origin, in degrees.

    but now i want to do the same using a rotation matrix. do you know how to go from the eye and origin coordinates to a rotation matrix? I want to do thos because i want to only rotate av few objects, not the whole world as with gluLookAt( the camera is rotating)

    thorsan

  6. #6
    Member Contributor
    Join Date
    Nov 2001
    Posts
    77

    Re: Rotating around an object

    glPushMatrix();
    // glLookAt code
    // "a few objects" render code
    glPopMatrix();

    // rest of render code

    Making your own matricies should only happen when OpenGL doesn't have something specific that you need, like mapping shadows.

    [This message has been edited by Nychold (edited 02-23-2004).]

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    10

    Re: Rotating around an object

    thanks. this gave me som good insight into the workings of opengl

  8. #8
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    10

    Re: Rotating around an object

    <qoute>
    if you use the standard math sine and cosine functions, yes. However, if I remember correctly, spherical coordinates are:
    x = r*cos(theta)*cos(phi)
    y = r*cos(theta)*sin(phi)
    z = r*sin(theta)

    where theta is in the x-y plane and phi is in the y-z plane.
    </qoute>

    what about around the xz plane? wouldtn "xy plane = zy plane + pi/2"

    thorsan

  9. #9
    Guest

    Re: Rotating around an object

    Since this is math I think I can help (I'm a beginner with openGL)

    <quote>
    where theta is in the x-y plane and phi is in the y-z plane.
    -------
    what about around the xz plane? wouldtn "xy plane = zy plane + pi/2"
    </quote>

    What you should think about is that phi is the angle of rotation around the z-axis. Theta is the angle that start equal to the z-axis and moves down into the xy-plane.

    If you set phi to zero or pi then rotating theta will move you around in the zx-plane

    If you set phi to pi/2 or 3*pi/2 then rotating theta will move you around in the zy-plane

    If you on the other hand set theta to pi/2 or 3*pi/2 and rotate phi you will move around in the xy-plane.

  10. #10
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    10

    Re: Rotating around an object

    i update my rotation angles with the arrowkeys. should i change the phi with right left an theta with up down. hen you say yz plane, do you meen around the y axis?

Similar Threads

  1. Rotating object
    By 3DPrgmer in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-26-2016, 08:38 AM
  2. Replies: 0
    Last Post: 10-28-2014, 08:51 PM
  3. rotating object
    By pbivens in forum OpenGL: Basic Coding
    Replies: 13
    Last Post: 07-19-2013, 01:35 PM
  4. rotating and object without rotating its axis
    By scam in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 12-17-2003, 08:30 AM
  5. Rotating about an object
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-01-2003, 12:12 PM

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