Learning About Rotation

Started by
25 comments, last by Zakwayda 4 years, 10 months ago
16 minutes ago, calioranged said:

I can picture all this just fine. What I cannot picture is a situation where there would be more than one non-zero value in the rotation vector - and how this would affect the viewing direction.

Okay, first of all, there is no such thing as a rotation vector - at least to my knowledge. There is only a rotation matrix!

A vector "... is a geometric object that has magnitude (or length) and direction" (https://en.wikipedia.org/wiki/Euclidean_vector)

Let's try something different. Draw a 2d coordinate system with the y-axis pointing upwards, and the x-axis pointing to the right. Make the distance between each value on the axis 1cm. Now draw a point with x coordinate 2 and y coordinate also 2. Now draw a direct line from the Origin to this point. Make an arrow out of that line by placing an arrowhead at the location of the point. Now you have drawn the vector (2,2). You can measure the length of this line and will probably get to the result sqrt(2*2 + 2*2) = sqrt(8) = 2,83cm. You will also see that the vector points into the northeast if you treat your coordinate system as a compass with the y-axis pointing to the north. For a direction vector, its length does not matter. The vector (1,1) has half the length, but it points in the same direction as the vector (2,2): Northeast! 

I hope you can follow so far. The next step we will do is to switch to 3d by just adding a 0 to our vector. (2,2,0). If the third dimension is now the height, our vector is still pointing to the north-eastern direction, but parallel to the surface, since its third component is 0. So now try the following: Take a pen and place it vertically onto the origin of your coordinate system. It is now aligned with your z-axis. Think of the other end of the pen as a point which is located on your z-axis where the length of the pen is the distance to the origin. Let's call the distance L. If you would create a vector to the pen's tip, it would be (0, 0, L). Is everything clear so far?

Now tilt the pen towards your y-axis while keeping contact to the origin until it lies on the y-axis. You have just rotated the point on the tip of the pen by 90 degrees around the x-axis, right? So the new vector is now (0, L, 0) since your pen is now lying on the y-axis with the distance to the origin still being L. The axis you rotated around was the x-axis, which can also be described by a vector: (X, 0, 0). X can have any value because all that matters for rotation is the direction of the axis.

So next thing you should do is reset everything. Put the pencil pointing upwards again. This time tilt the pen into the direction of the x-axis until it lies on the paper. Now you have rotated around the y-axis. So your pen's tip is now located at (L, 0, 0). So if you repeat those two steps, you will notice, that rotating around one of both axes will maintain a right angle between the axis and the pen.

So now comes the last step. Do the same but with the vector being the axis of rotation. Tilt the pen until it lies on the paper while keeping up a right angle between pen and vector. There only two possible outcomes. The pen can either be pointing to the northwest or the southeast, otherwise, there would not be a right angle between the vector ant the pen. So now you just rotated 90 degrees around a vector with more than one non-zero value!!!  So if you measure now, where the tip of your pen is located in your coordinate system, You will get a new vector.

Since your pen lies flat on the paper, it has only x and y values. The z value is 0. Due to the special construction of this example, both values for x and y are identical, but with a different sign. The exact value depends on the length of the pen that you have chosen and which one of the values is negative depends on the rotation direction. If you rotated to the northwest, your vector would look like this (-V, V, 0) where V is the length dependent value.

 

Okay... after this rather long example, let's translate it to your code:


glm::mat4 rotator { glm::rotate(glm::mat4(1.0F), 
			        glm::radians(degrees), 
				glm::vec3(rotation.x, rotation.y, rotation.z)) };

MVP.view.direction = rotator * MVP.view.direction;

 

Let's consider your view direction for the moment not being your view direction. Instead, it is a vector to an object... the tip of the pen ;). So give it the value (0,0,2). Now use the rotation vector (2,2,0) and 90 degrees as in the previous example. 

Run the calculation and what you should get is either the vector (-V, V, 0) or (V, -V, 0) with V being 1,414 = sqrt(2). Which vector you get depends only on the direction of rotation. You have just programmed the pen example.

Keep in mind, that glm calculates you a rotation matrix out of the axis and the degrees you provide. So you are actually not using a vector and an angle for the rotation itself but a matrix. You just need them to create the matrix.

 

Greetings

Advertisement
29 minutes ago, DerTroll said:

Okay, first of all, there is no such thing as a rotation vector - at least to my knowledge. There is only a rotation matrix!

Sorry I should have been clearer with my language. In the glm documentation this is referred to as the 'axis of 3 scalars'. I think the correct terminology would be rotation axis. 


//               matrix             angle                           'rotation vector' / axis
glm::rotate(glm::mat4(1.0F), glm::radians(Movement.y) / 2, glm::vec3(Rotation.x, Rotation.y, Rotation.z)

That was probably the best possible explanation I ever could have hoped for, thanks so much for taking the time to go through that example with me. 

I followed the instructions you gave all the way through and I feel I now have a basic level of understanding. For some reason I just couldn't conceptualise the idea of rotating around an axis that was represented by its distance from the centre/origin of the camera, but thanks to your example, I am now able to visualise this.

Thank you @DerTroll and @Green_Baron for the endless patience you have both shown towards me. 

 

 

9 hours ago, calioranged said:

Sorry I should have been clearer with my language. In the glm documentation this is referred to as the 'axis of 3 scalars'. I think the correct terminology would be rotation axis. 

Don't be. I regularly mess up terms myself. ;) Just wanted to clarify that we are getting a matrix and multiply that by a vector.

 

9 hours ago, calioranged said:

Thank you @DerTroll and @Green_Baron for the endless patience you have both shown towards me. 

You are welcome. A long time ago, I have been where you are now and I know how hard it can be to learn the stuff all by yourself.

 

Greetings

On 5/27/2019 at 9:53 AM, Green_Baron said:

In OpenGL the y axis goes up and z goes into the depth, so the rotation in your example is around y for OpenGLers. I think this is different on Windows/D3D ?

On 5/27/2019 at 10:02 AM, calioranged said:

In OpenGL; +X is to the right, +Y is up and +Z is coming out of the screen towards the viewer.

Some clarification might be useful here. In both OpenGL and D3D, once the library 'takes over' (e.g. from clip space on), both handedness and axis orientations are at least partially fixed. For example, in both libraries, clip space is (conceptually at least) left-handed, with +x to the right, +y up, and +z away from the viewer (this is with respect to how the geometry will typically ultimately be viewed, e.g. on a display).

However, prior to OpenGL or D3D 'taking over', there are no constraints on either handedness or axis orientation, so to say, for example, that +y is up or that +z is towards or away from the viewer with respect to either library is a little misleading. Just as an example, you might opt to use a right-handed system with +z up in world space (this might be appropriate, for example, for a strategy game where it's convenient to think of the 'ground' as lying in the XY plane).

Yes, for the own models and world one can choose any system one deems reasonable, as long as one can manage the transformations and projections. Spherical stuff for example ?.

But at an early stage when rotation is on the table such abstractions and how to get back and forth may be confusing. They certainly were for me ... ?  That's why i brought that up.

4 hours ago, Zakwayda said:

Just as an example, you might opt to use a right-handed system with +z up in world space (this might be appropriate, for example, for a strategy game where it's convenient to think of the 'ground' as lying in the XY plane).

 

This is probably what most people do. But I have to agree with this statement:

 

3 hours ago, Green_Baron said:

But at an early stage when rotation is on the table such abstractions and how to get back and forth may be confusing.

First, you have to understand how and why the basic operations translation, scaling and rotation work before you can start messing around with multiple coordinate systems. But once you really understood all those details, the following Futurama cite does actually make sense ? :

Spoiler

"I understand how the engines work now. It came to me in a dream. The engines don't move the ship at all. The ship stays where it is and the engines move the universe around it."

 

5 minutes ago, DerTroll said:

But I have to agree with this statement:

3 hours ago, Green_Baron said:

But at an early stage when rotation is on the table such abstractions and how to get back and forth may be confusing. They certainly were for me ... ?  That's why i brought that up.

Yes, sticking with +y-up in world space can remove some conceptual hurdles :) I still think it's worth keeping in mind though that there are no particular constraints, in particular because ±z-up can be more practical and intuitive in some cases (arguably at least).

This topic is closed to new replies.

Advertisement