Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Rotate object around its local coordinate system

  1. #1
    Newbie Newbie
    Join Date
    Feb 2014
    Posts
    3

    Rotate object around its local coordinate system

    Hi! I'm trying to rotate a 3d object (just a square plane) around ITS coordinate system, and not Processings global coordinate system. When using rotateX(), rotateY(), and rotateZ(), the rotation is always around Processings origo. I want to do a rotation around x at first, which is fine to use rotateX since it's the first rotation, and then rotate around the objects new y-axis. In other words, I want the rotation to always be around the object itself. So imagine there being a coordinate system on the square plane with z-axis orthogonal against the plane, and the x- and y-axes in the plane (parallel).

    I tried rotating around x first with rotateX, and then calculate a new y-axis (variable called new_vy) by multiplying the vector vy = {0, 1, 0} by x's rotational matrix, and then use rotate(angle, new_vy[0], new_vy[1], new_vy[2]);

    Some semi-psuedocode: //Code begin

    rotateX(angleX);

    new_vy = Rx(angleX)*vy; //Rx is the rotational matrix for x-rotation. Note that this is a matrix operation,
    //and the actual code doesn't look like this.
    //vy is just the unit vector for the y axis, vy = {0, 1, 0}

    rotate(angleY, new_vy[0], new_vy[1], new_vy[2]); //this function is pretty much the same as glRotatef(angle, x, y, z);

    draw3dobject();

    //Code end

    This should be done for z aswell, but x and y is enough for starters.

    So, any suggestions? Doesn't have to be a modification to my code, can be an entirely different method.

    I've found something about quaternions, but I would prefer not to use them, since I don't entirely understand them, and I think that this should work with out them.

    The purpose of this is to simulate a quadcopter, so therefor I need to input three separate angles, pitch, roll yaw, (euler angles).

    /Daniel

  2. #2
    Newbie Newbie
    Join Date
    Feb 2014
    Posts
    3
    Wasn't allowed to post a link, so I'll just post it in a comment. A picture to describe my problem: postimg.org/image/5blmdo6bn/

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    24
    glLoadIdentity();
    glTranslatef(ctrx,ctry, ctrz);
    glRotatef((pitch, 0, 1, 0);
    glRotatef((roll, 1, 0, 0);
    glRotate(yaw, 0,0,1);

    model();

    glRotate(-yaw),0,0,1);
    glRotate(-(roll,1,0,0);
    glRotate(-(pitch,0,1,0);
    glTranslate(-ctrx, -ctry, -ctrz);

    where crx, ctry, ctrz are the coordinates that you want to rotate around
    Depending on how you are rotating your widget, you may want to change the order of roll, pitch & yaw. Just step out in the reverse order that you stepped in.
    What you are doing is moving the center of the world to where you are building your widget; building your widget; and then moving the world back where it started from while taking your widget along.
    This works between glBegin() and glEnd(). Unless somebody knows something that I don't know, changing ctrz has no effect whatsoever on a VBO. As a kludge, subtract ctrz from each vertex while building the VBO.

  4. #4
    Newbie Newbie
    Join Date
    Feb 2014
    Posts
    3
    Yeah, heard somewhere that you should load the identity matrix. I did get another answer on another forum though, using quaternions. But thanks for the help wmelgaard!

  5. #5
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Posts
    167
    Have you found a solution to your problem yet?

Posting Permissions

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