// Here's Where We Do All The Drawing
int DrawGLScene(GLvoid)
{
// Clear The Screen And The Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset The View
glLoadIdentity();
//Position camera at one corner of the grid, looking at the center of it
gluLookAt cposxz,cposy,cposxz,8.0f,1.0f,8.0f,0.0f,1.0f,0.0f);
//move to where the upper left die would be
glTranslatef(1.0f,1.0f,1.0f);
//move to the actual position of the die
glTranslatef(xpos*2.0f,0.0f,yloop*2.0f);
//rotate the die
glRotatef(zrot,0.0f,0.0f,1.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
//draw the die
glCallList(box);
// Keep Going
return TRUE;
}
In the game I'm trying to clone, you have some dice on a playing field and can turn them over to line up the numbers and make them vanish. This is where I run into problems, probably because of my limited knowledge of 3D math... when I have the current rotation values for my die, and I want to rotate 90° around any of the three axes, how do I get the new rotation values? I mean I can't just add up the numbers, because I'm only doing three rotations in the given order x,y,z, and so if I for example rotate 90° around the z axis first, and then 90° around the x axis, what I actually had to do is rotate 90° around the x axis and 90° around the y axis. How do I calculate this? Or is this approach entirely wrong? I wouldn't want to do more than three rotations, would I? This is probably really simple, but I got no clue of what to do...
Edited by - knthrak83 on July 25, 2001 11:25:04 AM
Beginner question on rotation
Hi everybody!
I'm an absolute beginner in Open GL programming / 3D programming in general, so the question I have might be kinda dumb... but one doesn't learn nothing without asking dumb questions, right?
I've worked through most of the NeHe tutorials, and started out with modyfing the code of lesson 12. I altered the display list so I now have a fully textured die that I can move on an imaginary 7x7 grid (I want to make a Devil Dice clone if you know that game). My drawing function looks like this:
you should take a look at gimbal lock and quaternion theory at www.gamasutra.com . That will be useful to understand what is a gimbal lock (which is the problem you are into now iiuc) and how to solve it using quaternions. If you want i can also send you a simple quaternion tutorial.
bye
glHorizon_Project
www.web-discovery.net
bye
glHorizon_Project
www.web-discovery.net
Here''s a quick lesson on matricies and rotating. :D
The modelview matrix (which is the one you''re messing with) is actually a 4-by-4 matrix that holds the current perspective of how the view is being stored. Every time you call glTranslate(*), glScale(*), or glRotate(*), you change this matrix.
However, rotating can only change 9 out of the 16 entries, namely the 3-by-3 box in the upper-left-hand corner, or...
There''s a special way to think of this matrix too. Designate the first row the "x row", the 2nd the "y row" and the 3rd the "z row". Do the same for the columns.
Now we''re ready to get started. All rotating is is multiplying the current modelview matrix with another matrix that we''ll create. (If you don''t know how to do this, check out the gaming tutorials on how to do this at NeHe. :D) Since you''re only rotating about each of the axes (x, y, and z), figuring out this new matrix is rather easy. If you were to do it around an odd vector, then it becomes challenging. However, for what we''re doing, it''s fine.
Now, we''re going to start filling in the matrix shown above. First off, pick an axis to rotate about. I''ll do them all for you, in the order of the x-, y-, and z-axes for. So take your axes (say "x") and put a 1 in the intersection of that axis''s row and column
Now, for everything else in that row and column, put 0''s.
Now the tricky part. For the last four entries, you''re going to need basic trigonometry. You will need to know that, in mathematics, the standard direction of rotation is counterclockwise, not clockwise. If you do clockwise rotations, your angle of rotation is negative, rather than positive.
Anyways, the last four entries consist of this matrix in some form or another (where t is the angle of rotation).
So far so good. For the rotation matricies around the x- and z-axes, plugging this is isn''t a problem, since you already have a 2-by-2 grid of "?"''s left over. The y-axis isn''t much harder though. Simply put the top left entry in the top-left corner, the top-right in the top-right, and so on...
As a result, your matricies will look like this.
Since all of your angles are multiples of 90 degrees, these matricies should just be all 0''s, 1''s, and -1''s, which are easy to multiply together.
Now is where the fun stuff begins. :D So make a good number of rotations, and multiply all your matrices together to get a final matrix. Now, to find out how the die will really be, take any two perpendicular vectors. I will choose the vector coming out of the front of the die (the part that you can see, coming off in the +z direction), and the one going upwards (+y direction).
The two vectors (call them Z and Y for now) are equal to...
Note that these will correspond to the faces that were originally on the die before rotation. So if a "1" was showing when we started, Z would refer to the "1" side of the die.
If you take your final matrix and multiply it by these two vectors, you''ll eventually find out where these two vectors (after all that rotation) are. Call these new vectors Z'' and Y''. Z'' will still be the side of the die with the "1" on it.
Now all you have to do is, in your mind, come up with a way to figure out how to rotate Z and Y (and the rest of the die) so that it looks like it does with Z'' and Y''.
Whew, I think I''ve just written a book here! Well, if you get lost in that, or you need any more help, feel free to let me know! :D
~ Dragonus
The modelview matrix (which is the one you''re messing with) is actually a 4-by-4 matrix that holds the current perspective of how the view is being stored. Every time you call glTranslate(*), glScale(*), or glRotate(*), you change this matrix.
[m11 m12 m13 m14][m21 m22 m23 m24] // The Modelview Matrix[m31 m32 m33 m34][m41 m42 m43 m44]
However, rotating can only change 9 out of the 16 entries, namely the 3-by-3 box in the upper-left-hand corner, or...
x y zx [m11 m12 m13]y [m21 m22 m23] // The "Rotation Matrix"z [m31 m32 m33]
There''s a special way to think of this matrix too. Designate the first row the "x row", the 2nd the "y row" and the 3rd the "z row". Do the same for the columns.
Now we''re ready to get started. All rotating is is multiplying the current modelview matrix with another matrix that we''ll create. (If you don''t know how to do this, check out the gaming tutorials on how to do this at NeHe. :D) Since you''re only rotating about each of the axes (x, y, and z), figuring out this new matrix is rather easy. If you were to do it around an odd vector, then it becomes challenging. However, for what we''re doing, it''s fine.
Now, we''re going to start filling in the matrix shown above. First off, pick an axis to rotate about. I''ll do them all for you, in the order of the x-, y-, and z-axes for. So take your axes (say "x") and put a 1 in the intersection of that axis''s row and column
"X" x y z "Y" x y z "Z" x y zx [1 ? ?] x [? ? ?] x [? ? ?]y [? ? ?] y [? 1 ?] y [? ? ?]z [? ? ?] z [? ? ?] z [? ? 1]
Now, for everything else in that row and column, put 0''s.
"X" x y z "Y" x y z "Z" x y zx [1 0 0] x [? 0 ?] x [? ? 0]y [0 ? ?] y [0 1 0] y [? ? 0]z [0 ? ?] z [? 0 ?] z [0 0 1]
Now the tricky part. For the last four entries, you''re going to need basic trigonometry. You will need to know that, in mathematics, the standard direction of rotation is counterclockwise, not clockwise. If you do clockwise rotations, your angle of rotation is negative, rather than positive.
Anyways, the last four entries consist of this matrix in some form or another (where t is the angle of rotation).
[cos(t) -sin(t)][sin(t) cos(t)]
So far so good. For the rotation matricies around the x- and z-axes, plugging this is isn''t a problem, since you already have a 2-by-2 grid of "?"''s left over. The y-axis isn''t much harder though. Simply put the top left entry in the top-left corner, the top-right in the top-right, and so on...
As a result, your matricies will look like this.
"X" x y z "Y" x y z "Z" x y zx [1 0 0 ] x [cos(t) 0 -sin(t)] x [cos(t) -sin(t) 0]y [0 cos(t) -sin(t)] y [ 0 1 0 ] y [sin(t) cos(t) 0]z [0 sin(t) cos(t)] z [sin(t) 0 cos(t)] z [ 0 0 1]
Since all of your angles are multiples of 90 degrees, these matricies should just be all 0''s, 1''s, and -1''s, which are easy to multiply together.
Now is where the fun stuff begins. :D So make a good number of rotations, and multiply all your matrices together to get a final matrix. Now, to find out how the die will really be, take any two perpendicular vectors. I will choose the vector coming out of the front of the die (the part that you can see, coming off in the +z direction), and the one going upwards (+y direction).
The two vectors (call them Z and Y for now) are equal to...
[0] [0]Z = [0] Y = [1] [1] [0]
Note that these will correspond to the faces that were originally on the die before rotation. So if a "1" was showing when we started, Z would refer to the "1" side of the die.
If you take your final matrix and multiply it by these two vectors, you''ll eventually find out where these two vectors (after all that rotation) are. Call these new vectors Z'' and Y''. Z'' will still be the side of the die with the "1" on it.
Now all you have to do is, in your mind, come up with a way to figure out how to rotate Z and Y (and the rest of the die) so that it looks like it does with Z'' and Y''.
Whew, I think I''ve just written a book here! Well, if you get lost in that, or you need any more help, feel free to let me know! :D
~ Dragonus
Thanks a lot, Dragonus! That''s the piece of math I was missing, and a very nice and understandable explanation of it. You''ve written any tutorials about programming? If not, you should do
*laughs* About the closest I''ve ever come to writing a gaming tutorial is explaining to my friends little coding concepts or tidbits of code. But seeing your suggestion, I might one of these days... Hmmmm...
~ Dragonus
~ Dragonus
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement