def update
if button_down? Gosu::Button::KbRight then
@yrot -= 1.5
end
if button_down? Gosu::Button::KbLeft then
@yrot += 1.5
end
if button_down? Gosu::Button::KbUp
@xpos += Math.sin(@yrot * (Math::PI/180.0)) * 0.05
@zpos += Math.cos(@yrot * (Math::PI/180.0)) * 0.05
end
if button_down? Gosu::Button::KbDown
@xpos -= Math.sin(@yrot * (Math::PI/180.0)) * 0.05
@zpos -= Math.cos(@yrot * (Math::PI/180.0)) * 0.05
end
if button_down? Gosu::Button::KbLeftControl
@lookupdown -= 1.0
end
if button_down? Gosu::Button::KbLeftAlt
@lookupdown += 1.0
end
if @walkbiasangle < 1.0
@walkbiasangle = 359.0
else
@walkbiasangle -= 1.0
end
@walkbias = Math.sin(walkbiasangle * (Math::PI/180.0)).to_f
end
then it's no longer looks too great. Instead, if I go rotate around the x-axis, the triangle drawn looks like it's being pulled to one direction or the other, it's almost as if it's on a path of a slanted ring. When I visualize this, they do the exact same thing and are in the exact same positions.
Does the order matter here for some reason or is the fact that my sceneroty's default value being 360.0 degrees messing things up?
You are simply visualizing it wrong. For example, the second rotation is affected by the first rotation. Transformations happens, when read from top to bottom, in the objects local coordinate system. So if you first rotate about the Y-axis, as in your example, the object's X-axis is rotated, so the axis of rotation for the second rotation is also rotated. In this case, rotations are not about the global axes, but about the local axes, and the local axes transforms along with the object.
Say your first rotation is 90 degrees around the Y-axis (sceneroty=90). Since this is your first transform, the object's local Y-axis is the same as the global Y-axis. The local X-axis, however, is also rotated, and is now equal to the global Z-axis, so the second rotation is about the local X-axis, or equivalently about the global Z-axis. This is probably where your visualization is going wrong. If you track the local axes and visualize the rotations around them instead, you will see that order does matter.
Which order is correct is entirely dependent on what you want to achieve. Only you can answer that.
You've have to consider several things when dealing with transformations. First, OpenGL uses column vectors, what means that a matrix-vector product looks like M * v
where M is a transformation matrix (e.g. build on one of OpenGL's matrix stacks) and v a vertex position. Notice that v is on the right of M, what is a necessity for column vectors. The transformation matrix will typically be composed. Let's say by a rotation R and a translation T like in T * R * v
In OpenGL, you would have to write
glTranslatef(...);
glRotatef(...);
glVertex3f(...);
to get the transformation above. Notice that the OpenGL commands from top to bottom correspond with the formula terms from left to right!
Next you have to consider that all of the glTranslate, glRotate, glScale, and glMultMatrix commands are multiplicative, i.e. the corresponding matrix is ever multiplied (on the right, you know) of what is already on the active matrix stack. The only possibility to overcome this is to use glLoadIdentity which overwrites the current matrix with the identity matrix (that is a matrix equivalent to the scalar 1, i.e. multiplying a matrix with the identity matrix gives the matrix itself). So in certain situations yo may need to write
glLoadIdentity();
glTranslatef(...);
glRotatef(...);
to get in fact something like I * T * R == T * R
When you're doing
glTranslatef(...);
glRotatef(...);
foreach vertex do glVertex3f(...);
then OpenGL computes the matrix product and applies the composed matrix to each vertex, like so
( T * R ) * v
However, these parentheses are just for clarity, because the matrix product is associative. When you are interpreting what happens to the vertex, it is IMHO better to think about it as T * ( R * v )
instead: The vertex position is rotated, and the rotated position is translated. Notice please that terms that are closer to the vertex appear to be applied "earlier", and terms that are more to the left are applied on a vertex position that is already transformed by all the matrices to the right of it!
Next, whenever a particular transformation is applied, it is done relative to a reference co-ordinate system. This system is, at the moment of application, the global co-ordionate system. For example, assume 2 consecutive rotations R[sub]x[/sub] * R[sub]y[/sub] * v
This means that the original vertex position is rotated around the cardinal y axis. The y axis is identical to the global y axis. The so rotated vertex will be rotated around the cardinal x axis. This x axis is again identical to those of the global co-ordinate system. Notice that this is not the same x axis as it was before R[sub]y[/sub], because R[sub]y[/sub] has changed the space.
If you desire to rotate around local axes instead, you have to transform into a space where the local axis is coincident with the global one, apply the transformation, and undo the former space transformation. For our example of 2 consecutive rotations, the first rotation R[sub]y[/sub] is applied in a space where local and global co-ordinates are the same, so the first step is still R[sub]y[/sub] * v
Next we would rotate around the global x axis, but we want to rotate around the local x axis. So we need to map the local x axis onto the global one; this means to "undo" the former rotation R[sub]y[/sub][sup]-1[/sup] * R[sub]y[/sub] * v
then to apply the x rotation R[sub]x[/sub] * R[sub]y[/sub][sup]-1[/sup] * R[sub]y[/sub] * v
and then to "redo" the former rotation R[sub]y[/sub] * R[sub]x[/sub] * R[sub]y[/sub][sup]-1[/sup] * R[sub]y[/sub] * v
When simplifying this, the fact that R[sub]y[/sub][sup]-1[/sup] * R[sub]y[/sub] == I (similar to s / s == 1 for a scalar s != 0) gives us R[sub]y[/sub] * R[sub]x[/sub] * v
Err, well, exactly changing the order of rotations gives us the effect of using local axes for these rotations!
I known that this is damn much stuff to think about. But it is both sufficient and necessary to determine the correct order of transformation matrices.