Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

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

Thread: Transforming + Rotating an object without going weird?

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    12

    Question Transforming + Rotating an object without going weird?

    Hi, i guess im fairly advanced in lwjgl however this is a newbie question.

    When i rotate an object and then translate the object (its a model ship in a VBO), the object rotates around the centeral point where it first spawned and not from its own centre.
    When i translate the object and then rotate it the ship rotates around the centre but does not translate the right way (so the ship always moves the same direction).

    How can i fix this?

    Thanks.

    Edit: Am using the glRotatef() and glTranslatef().
    Last edited by joehot200; 12-18-2013 at 08:58 AM.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    Just to get a clue where we can pick you up, do you have a basic understanding of linear algebra?

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    12
    No, i don't unfortunatley. I may be using it or whatever however i do not actually know a lot of the names of the code i am using as i often copy it from elsewhere and change bits around to learn what certain pieces do.
    I actually work more with lwjgl rather than opengl, though the calls/code are the same.
    I also don't go to school (i am 13), so don't expect me to be able to understand cosigns etc so well.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    Even though you're 13, if you're into this stuff, I recommend reading Lengyel's book, "Mathematics for 3D Game Programming and Computer Graphics". It's good learning material and when I look up the most common stuff, this is the book I go to. If you're not solid in 3D math, don't slip anything. Read it, understand it and be amazed at how awesome math is.

    i often copy it from elsewhere and change bits around to learn what certain pieces do
    That's fine if you're trying to figure out what a specific funtion does. However, if you want to really do 3D programming, there is absolutely no way around the appropriate math portions.

    I actually work more with lwjgl rather than opengl, though the calls/code are the same.
    The interface doesn't change how the encapsulated GL calls operate. LWJGL is just a wrapper usable with Java (since OpenGL implementations are usually implemented in C and LWJGL needs to interface with the C library through the JNI).

    I also don't go to school (i am 13)
    At that age, aren't you supposed to go to school? (Don't know where you're coming from.)

    Regarding your problem at hand, for now, it should suffice to know that matrix rotation leads to rotation of a position around the origin, i.e. (0, 0, 0, 1), of the current coordinate system. There is nothing you can change about that, but there's a simple trick to rotate around an arbitrary point in space: you translate the center of your object to the origin, then rotate, and then translate it back to whatever position you want it to be located at. If the original and resulting position are the same, you effectively rotated around your objects center.

    Now, a translation is nothing more than moving a point from one place to another. To achieve the above, in 3D you'll need a 3D vector to describe the direction of the translation and the distance you want to translate the object. The distance is implicit - it is the length of the translation vector. So, if you have a vertex at position (1, 1, 1) and want to translate it to the origin, you'll need a translation vector (-1, -1, -1). The resulting position will be (0, 0, 0) (because (1,1, 1) + (-1, -1, -1) = (0, 0, 0))

    To translate it back to where it came from, you'll need to translate in the inverse direction, i.e. you need to negate the translation vector: (-1, -1, -1) then becomes -1 * (-1, -1, -1) = (1, 1, 1). Apply that to the current position (0, 0, 0) and you'll get (0, 0, 0) + (1, 1, 1) = (1, 1, 1). And where back to our original position.

    Does that make sense to you? Does it help?

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    12
    I just typed a whole reply... and it didnt appear when i posted it. :S

    Anyway your answer was very good, very informative and helpful, however could you post the exact code in the exact order i need? Certain sites say i should put the code in different orders or backwards etc, and it confuses me.

    In england home "education" is allowed. Unfortunatley a lot of home "educated" people don't actually educate themselves, unlike me.

    I will look into the book, thanks. Will probably get it too.

    I did have a longer reply however i am too tired to type it all out again. Thanks a lot for the informative reply. I do not get that on many forums.

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    however could you post the exact code in the exact order i need?
    I'll first go into the math a bit.

    1. Assuming you know the center of your object to be at point C. For each vertex, a translation T encoded into a translation matrix MT needs to be applied which translates in direction -C, i.e. for each vertex vi, the initial transformation is vi' = MT * vi (with i in [0, #vertices]).

    2. Then a rotation R follows, encoded in a rotation matrix MR. For each translated vertex vi', a rotation is applied as follows: vi'' = MR *vi'.

    3. Afterwards, the rotated vertices are translated by MT-1. For each rotated vertex vi'', the final position results vi''' from vi''' = MT-1 * vi''.

    The reason I seperate the steps like that is simple: you'll probably implement the above exactly the same way using the fixed-function pipeline, i.e. you first call glTranslatef(), then glRotatef() and then glTranslatef() again. However, what you actually end up with is a concatenated transformation matrix, which encodes both translations and the rotation: vi''' = MT-1 *MR * MT * vi = (MT-1 * MR * MT) * vi =Mcomplete * vi

    Expressing this in fixed-function OpenGL can be achieved in more than one way, but the usual approach is this (let's assume you want to rotate your object about the y-axis and an angle of 45.0 degrees):

    // assume top of the MODELVIEW stack is the identity M_ident

    // This is equal to computing M_ident * M_complete = M_complete
    glTranslatef(Cx, Cy, Cz);
    glRotatef(45.f, 0.f, 1.f, 0.f);
    glTranslatef(-Cx, -Cy, -Cz);

    // This is then equal to v_i''' = M_complete * v_i (with i in [0, VERTEX_COUNT]).
    drawObject();


    HTH! If you have any further questions, let us know.

    I do not get that on many forums.
    That's what seperates us from the masses.
    Last edited by thokra; 12-19-2013 at 02:59 AM.

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    12
    Unfortunatley that did not work.
    (Using spaces in links due to site denying links)

    My current code is:
    http://pastebin.com/1a2Nk6HC

    The problem is:
    http://youtube.com/watch?v=6Rviu9dXBKw&feature=youtu.be

    Thanks.
    Last edited by Dark Photon; 12-19-2013 at 07:29 PM.

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    The video is funny.

    So, you obviously want the ship to sail along a path, right?

  9. #9
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    12
    Yes, i would like it to sail in a straight line and turn at the ship's location and not the origin location before translation.

  10. #10
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    But that works just the same. As long as the direction of the ship doesn't change, you translate along this direction. As soon as you hit a point where the ship is supposed to be rotated, you first translate to the origin, then rotate, translate back and then sail along the new direction.

    Translating like that normally involves linear interpolation between points of rotation. Beware that, depending on the length of the line segments, translation speeds will vary, but you'll see that yourself. It's a different topic and should be tackled separately once you got the basic stuff down.

    For some path, defined with n control points, the (pseudo) code might look a little like this:

    Code :
    // move along a path
    Vector3 currentPoint = start;
    Vector3 nextPoint     = currentPoint + 1;
    Vector3 position       = currentPoint;
     
    while(nextPoint != end)
    {
      // We reached a control point, rotate here
     if(position == nextPoint)
     {
       rotateShip(position, angle);
     
       // set the new interval for interpolaton
       currentPoint = nextPoint;
       nextPoint     = nextPoint + 1;
     }
     else
     {
       // set position to a new point in the interval [currentPoint, nextPoint]
       translateShip();
     }
     
     renderShip();
    }

    The linearly interpolated position is computed with: position = currentPoint + (nextPoint - currentPoint) * n (where n is in [0, 1]). To calculate the angle between the current direction of the ship and the direction of the ship at the next control point, you can take the angle of the current direction and the x-axis and the angle of the next direction and the x-axis, compute the difference and then rotate accordingly.
    Last edited by thokra; 12-19-2013 at 08:14 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
  •