# Using a matrix for rotations in 3D space

• 02-25-2009
MK27
Using a matrix for rotations in 3D space
Hopefully someone has done this before or has some magical insight.

I'm using an openGL matrix, which is a 16-element float array (1D, not 2D!!). The first four elements are the X axis orientation, the next 4 are the Y axis, the next 4 the Z axis, and the last 4 actual coordinates (the last number of each is always 0, except the very last which is one). As a 2D array (which we do multiply them in that manner too), it would look like this:
Code:

```X  Y Z  CC 0  4  8  12 1  5  7  13 2  6  9  14 3  7 10 15```
So a dead center point will be
Code:

```1  0  0  0 0  1  0  0 0  0  1  0 0  0  0  1```
hence
Code:

`float GLmatrix[16]={1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 0.0,0.0,0.0,1.0};`
The purpose of the matrix is to use it as the base for the location and orientation of a 3D object.

The orientation of a single point seems irrelevent, but it can still be rotated, and you would rotate a cube (eg) the same way. So what I want is a simple function to spin this object around the y axis. I figured the way to do this would be to use the sin of the x-axis X value and the cos of the x-axis Z value:
Code:

```void YRotate (float deg, float matrix[16]) {         float rads;         if (deg==0.0) return;         rads=deg/(2.0*GL_PI);  /* math.h uses rads */         matrix[0]=sin(rads);                matrix[2]=cos(rads); }```
Which works but it flattens the cube! So I get something that looks like those spinning "live bait" signs they have outside gas stations during the summer.

Any deep thoughts on what I missed?
• 02-25-2009
tabstop
So I've think you missed the boat on a matrix, or at least that was poorly explained. A matrix is not a point in space -- a point in space is just four numbers. A matrix specifies a movement of some kind.

Anyway, to answer the actual question, if you rotate around the y-axis, then the x-values AND the z-values change. (Think about it: rotating around the y-axis is like turning around; the direction you think of as "right" changes (x), AND the direction you think of as "forward" changes (z).)

Code:

```cos  0 sin  0  0  1  0  0 -sin 0 cos  0  0  0  0  1```
• 02-25-2009
itCbitC
Quote:

Originally Posted by MK27
So what I want is a simple function to spin this object around the y axis. I figured the way to do this would be to use the sin of the x-axis X value and the cos of the x-axis Z value...

Just a couple of questions as in:
a. final orientation depends on the degrees you turn it by 90, 180 ??
b. not sure what is meant by "... the x-axis Z value" sentence?
• 02-25-2009
MK27
Quote:

Originally Posted by tabstop
So I've think you missed the boat on a matrix, or at least that was poorly explained. A matrix is not a point in space -- a point in space is just four numbers. A matrix specifies a movement of some kind.

Beg to differ sir. A vector is a point in space (x, y, z). A matrix is just a matrix (a 2D array, conceptually, even if the C implimentation is an array of 16). The openGL matrix does represent a point in space including its orientation relative to absolute (the orientation of it's own localized coordinate system with an X, Y, and Z axis).

The center of a cube could be at 1,1,1. Using a matrix to handle the cube, we would multiply the vectors of all the key points in the cube (eg, eight corners) by this MATRIX because a cube also has an orientation (picture tilting and turning it, but leaving the center stationary). So in essence, that matrix will represent the state of the cube (a 3D object). Remember, I said the orientation of a point is irrelevant (but still easily comprehended -- I mean irrelevant to cartesian math).

But thanks anyway, tabstop (for answering "the actual question") because I suspect you're right so I'll try this and let everyone know.

ps. technically, a "YRotate" function already exists in openGL, I just wanted to develop a slightly grander thing that would deal with rotations around multiple axises all at once, figuring they don't have a "battletank" function either -- you have to make them out of polygons.
• 02-25-2009
tabstop
Quote:

Originally Posted by MK27
Beg to differ sir. A vector is a point in space (x, y, z). A matrix is just a matrix (a 2D array, conceptually, even if the C implimentation is an array of 16). The openGL matrix does represent a point in space including its orientation relative to absolute (the orientation of it's own localized coordinate system with an X, Y, and Z axis).

This is most emphatically not true. The openGL matrix represents a transformation of space (a linear transformation represented by the proper 3x3 submatrix plus a translation by the fourth column). It does not represent a point. For instance, you can never draw (with glBegin etc.) anything but a glVector. The primitives that give matrices are glTranslate and glRotate (if there's a glScale or a glSkew -- I've never used them if there are -- it would be a matrix too).

Quote:

Originally Posted by MK27
The center of a cube could be at 1,1,1. Using a matrix to handle the cube, we would multiply the vectors of all the key points in the cube (eg, eight corners) by this MATRIX because a cube also has an orientation (picture tilting and turning it, but leaving the center stationary). So in essence, that matrix will represent the state of the cube (a 3D object). Remember, I said the orientation of a point is irrelevant (but still easily comprehended -- I mean irrelevant to cartesian math).

So you proved my point, and I thank you for it. The vertices of the cube are given by vectors, as you state. Since you need to transform the world to make the cube move around, you multiply those point vectors by a transformation matrix to put them in new places (and note that after you multiply, you get ... a vector).
• 02-25-2009
MK27
Well I really had not meant to probe anyone for openGL specific questions, instead betting on the (rather off) chance that some savant would just grasp this system, since it makes such mathematical sense, intuitively, and be thrilled enough with him or herself to give a sin/cosin novice like myself a clue.

Quote:

Originally Posted by tabstop
The openGL matrix represents a transformation of space (a linear transformation represented by the proper 3x3 submatrix plus a translation by the fourth column). It does not represent a point. For instance, you can never draw (with glBegin etc.) anything but a glVector.

I don't expect to do that either sir. I meant in theory (no "C", no "GLX") it is equivalent to a point in space with a trajectory, and an up/down. In openGL this point never exists because that matrix is actually used to multipy:

the vector of the point
the coordinate system of the point

which the last is always whatever coordinate system actually exists because of the push/pop system. Perhaps an issue is that it makes more sense to see multiple 3D coordinate systems in the final product, although in reality openGL itself may be more focussed on the real 2D melange of pixels (I guess that's why "3D" may appear in double quotes). But seriously: am I right to assume that the last row of these transformation vectors is ALWAYS 0,0,0,1 ??

Incidentally I'm gaining more faith in this theoretical matrix of yours and will be testing it after I install a webcam to call my mom on skye ;)
• 02-25-2009
tabstop
Quote:

Originally Posted by MK27
I don't expect to do that either sir. I meant in theory (no "C", no "GLX") it is equivalent to a point in space with a trajectory, and an up/down.

That isn't true either. A 4x4 matrix is either (1) a linear transformation of 4-dimensional space or (2) an affine transformation of 3-dimensional space in homogeneous coordinates (affine meaning the origin can move, i.e., a translation is added in). A matrix cannot give a history of the previous position of the point over time (which is what I assume you mean by trajectory, but I could be wrong), nor does it give any orientation at all (which is what I assume you mean by up/down, but could be wrong).

Quote:

the vector of the point
the coordinate system of the point
I suppose you can think of this this way, since the fourth column gives the new position of the origin, and the 3x3 matrix can be considered a change of coordinates. This is very practical for cut-and-paste programming, since for every object in your world you can reset back to the origin, transform by this matrix, and draw a constant object. For understanding what's going on, it ... well, it's probably not the worst way to view what's going on, but I can't think of a worse one right this minute.

Quote:

But seriously: am I right to assume that the last row of these transformation vectors is ALWAYS 0,0,0,1 ??
A legitimate affine transformation must have a final row of 0 0 0 1, so that the homogenous coordinate is maintained at 1.