Thread: OpenGL rotation

  1. #1
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69

    OpenGL rotation

    I'm trying to make a little 2D rectangle rotate. But, it can also move up. When it does move up, it rotates around the center of the window, like its in orbit. I want it to rotate on its own center point help! I've been using
    Code:
    glRotate2f(x, 0.0f, 0.0f 1.0f);

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Is that the actual call you are usingd? It shouldn't compile.

    Something like
    Code:
    glRotate3f(x, 0.0, 0.0)
    should work for your case. The number does not correspond to how many things you're passing, and there's a comma missing.
    The last param, you can omit, because it defaults to 1 if not used.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    It is
    Code:
    glRotatef(angle, x, y, z);

  4. #4
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    i think I read something that glRotate() rotates the whole plane, not just the object. how do i make only the object rotate. If you give me any math equations, could you explain them please, im only in grade 9.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    /*draw stuff*/
    glTranslatef(0.0,x,0.0);/*move up like you said*/
    glPushMatrix();
         glRotatef(x,0.0,1.0,0.0);
         /*draw stuff to rotate*/
    glPopMatrix();
    you have to get used to using push pop matrix like that. Or else you are rotating everything. Does this help if not show me the code.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    OpenGL rotates the coordinate system. If you rotate first, then translate, the direciton it translates is now with respect to the *rotated* coordinate system.

    Say, for example, you want to rotate 45 degrees about the z axis, then you wan to translate down the x axis 5 units. Well, if you *just* translated down the x axis 5 units, then your position would be:

    (5,0,0)

    However, if you rotate 45 degrees about the z axis (glRotatef(45,0,0,1)) and THEN translate down the x axis 5 units (glTranslatef(5,0,0)) your position will *actually* be about:

    (3.5, 3.5, 0)

    So, in general, if you want an object to *just* rotate, you need to translate it *first*, and then rotate the object. Linuxdude's code works, but you don't need to push and then pop the matrix, just as long as you always translate before you rotate.

    Note that there are some times when it is convenient to do it the other way around. For example, if you want a planet to orbit another one, you can just rotate out through the current angle and then always translate down a constant axis (because that axis will actually be rotated if you call glRotate first).

    If something I or someone else has said (we're all saying basically the same thing) doesn't make sense, just say what doesn't make sense and we'll reiterate or draw some pics. Good luck.
    Last edited by Darkness; 01-30-2005 at 11:08 AM.
    See you in 13

  7. #7
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    thanks, that helped alot. There's only one more problem. I want the rectangle to move up in a certain direction, like a spaceship. So when I rotate, I wanna go in the direction I rotated. Using your theory, the rectangle always goes up, even if its shaped like a diamond.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    okay, well, you know the position of the space craft, and you also know it's orientation.

    To get the position, you compute it based off of it's velocity vector (the direction it is moving in, and the speed at which it is going).

    You also know the orientation of the spacecraft (which you probably want to be aligned with the direction of the velocity).

    In general, as I said above, you translate the object first, and then rotate it.

    edit:
    it's not theory, you just have to understand what i was talking about above (the bit about translating it before rotating it) and you also have to know how to compute it's position from it's velocity vector. I'll let you think about what I've said, and you can do the rest
    Last edited by Darkness; 01-30-2005 at 03:18 PM.
    See you in 13

  9. #9
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    so lets say I, when the ship rotates, I record the degrees, how would I adjust the glTranslate() to go a little up and a little left, or down and right, according to the degrees of the turn?

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    So, what you are saying is, when you know the direction the ship is pointing, you want to move a little bit in the x direction, and a little bit in the y direction, and you know how fast the ship is moving in that direction.

    So, you know a few things:

    -How fast the ship is moving (its speed)
    -The angle that the ship is traveling in (its direction), in this case it's in degrees (because glrotate uses degrees)

    So, what you want to know is this:
    -What is the change in the X coordinate of the position
    -What is the change in the Y coordinate of the position

    So, how do you solve for the change in position in the X and Y direction when you know an angle?

    The goldfish thing is the angle theta. The red line is your velocity vector. How do you figure out the change in the X coordinate and the Y coordinate of the space ship's position when you have an angle and a magnitude?

    Also, what grade are you in? I have no clue how much math you know.
    Last edited by Darkness; 01-30-2005 at 04:50 PM.
    See you in 13

  11. #11
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    I'm in grade 9

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    Have you done any trigonometry before? If not, you are going to need to do some in order to understand this stuff. It seems pretty evident that you haven't.
    See you in 13

  13. #13
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    In my last qbasic program, I asked for help with a speedometer needle, which had trigonemetry in it. The guy gave me the formula but didn't explain it. Is there a formula for this and if yes, could you explain some of it? Thanks, you've been a lot of help!

  14. #14
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    I want YOU to figure this out. I've already done a lot, but it is evident you are just inexperienced.

    Look up the definition for sine and cosine, either dive into one of your math books or find it on the internet. I just partially gave you the answer, but not quite.

    EDIT:
    I'm not trying to be pedantic, but you'll feel a lot better if you figure it out on your own. If you've found the definitions for sine and cosine, but are still truly confused, I will explain this better.
    Last edited by Darkness; 01-30-2005 at 06:11 PM.
    See you in 13

  15. #15
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Unless you already knew, matrix transformations take effect in a reverse order. If you call Translate then Rotate the rotation will be made first, then the translation. Quite mysterious at first, but once you use push/pop-matrix to its full power it'll seem natural.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL hue rotation
    By Blink in forum Game Programming
    Replies: 4
    Last Post: 08-14-2007, 03:42 PM
  2. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM