-
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);
-
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.
-
It is
Code:
glRotatef(angle, x, y, z);
-
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.
-
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.
-
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.
-
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.
-
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 :)
-
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?
-
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.
-
-
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.
-
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!
-
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.
-
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.