How to calculate rotation axis and angle? - math

I am trying to rotate a model in 3D so that it faces the right direction. The rotation I want is fairly trivial and can be broken down into two steps:
Rotate the model 90 degrees on its x-axis.
Rotate the model 180 degrees on its z-axis (relative to the first rotation).
The way to set a model's rotation in the framework I'm using (openFrameworks) is by calling its setRotation method. This method takes an angle, as well as floats x, y and z that specify the axis of rotation. How do I calculate the axis of rotation and angle for this particular rotation? I can't rotate the model two times sequentially because any call to setRotation overwrites previous rotations.
Please let me know if I can provide more information or clarity.
EDIT: In case anyone has the same question, this post helped me a lot.

weird that you can not apply more then one transform ... maybe you just use wrong function but anyway:
If you have direct access to transform matrix (or by get,set)
google for transform matrices if you do not have the knowledge
I suspect you are using 4x4 homogenous cartesian transform matrices
transform matrix anatomy
generate first rotation matrix and store it to M1
can use the setRotation for that
generate second rotation matrix and store it to M2
multiply them M=M1*M2
use this M instead of setRotation
If yo do not have the direct access to transform matrix and have to use just the setRotation
in that case you have to use quaternion which is the 4D vector you call the setRotation with
google for quaternion math and find the application of 2 rotations
I do not use them so I can not help with that but there are also equations out there
which converts 3x3 rotation matrix into quaternion and back
so you can still use the algorithm above
obtain M
extract the rotation matrix from it (it is just sub matrix you omit last row and column)
compute quaternion from it
and call setRotation with the result

Related

Transform 3d point using angles

I have a 3d point that I would like to rotate using angles (yaw, pitch and roll) around { 0, 0, 0 }.
How would I go about it, without converting the angles into a matrix?
Well, you don't really "convert angles into a matrix". Strictly speaking, rotations are linear transformation and are in general comprised of an angle and an axis (vector) about which the rotation occurs. The two easiest approaches to define an angle / axis rotation is by using quaternions or rotation matrices. There may be other approaches, but these two methods are used largely because they are the easiest methods anyone has proposed to date. Of the two, I personally prefer quaternions for rotations since they are easier to implement and require fewer arithmetic operations. 3x3 matrices have the benefit that they are able to handle general 3D->3D linear transformation; 4x4 matrices can perform general 3D->3D affine transformation on 3D vectors.
If you want to use separate rotations for yaw, pitch and roll, you should probably review issues related to Euler Angles. You can model these using either rotation matrices or quaternions. Both approaches will essentially be equivalent. It's simply a matter of defining a sequence of angle / axis pairs and multiplying them to get the final rotation. That rotation is then applied to whatever points you have to arrive at the rotated value.

Calculate forward and up vectors from euler position and rotation?

I have an object in 3D space where all I have is a euler position and rotation. How can I calculate forward and up vectors from the information I have?
I know that I can calculate the forward vector in this way:
Vector3 forward = (target.getPosition() - object.getPosition()).normalize();
.. where target is any point along the axis which the object is looking. Using the information I have, how can I pick an arbitrary point in this way to normalize?
I'm not sure how to go about solving the "up" vector at all.
First create a transform matrix from your euler angles (with the same method as you are using while rendering). Then extract the axises vectors for forward and up from it directly. For example my view matrices uses Z axis for forward/backward and X axis for left/right so I would just use those two. You will find the location of the vectors here:
Understanding 4x4 homogenous transform matrices

Constructing transformation matrix from final position

I have an object in 2D space, that is rotated/scaled around. Normally I would find the transformation matrix straightforwardly:
Translate by origin
Rotate
Scale
Translate by -origin
Translate by original position
Given an original object position at (0,0), I can easily get the new location as a vector by multiplying that by the transformation matrix.
However, for this problem I don't have the original position. I only have the final position.
How can I construct the same transformation matrix, when I only have the already-multiplied vector, not the original position?
I still have the transformation point, rotation and scale.
Not sure if I am telling you anything new but you use the inverses of the matrices applied in reverse order to find the original point(s) that was(were) transformed. Not entirely sure that this is what you are looking for but if so then there is a solution below.
Below is an example for 2D transformations. The method is easily extended for 3D
as you know the vector t and the rotation R and the scale s the inverses are easily calculated and applied.

How to convert points between two coordinate systems with different rotations

Imagine two coordinate systems layed on top of each other, with a rotation and scale difference between the two:
The problem is to convert a point from the non-rotated system to the other. What we do have, are four corner points forming a rectangle, with coordinates known for both systems at each point. We also know the rotation difference, and I think I at least should know the scale difference too. How do I convert a point from the non-rotated system to the rotated system? I have Unity3D at use.
Extra points for clarity in math :)
PS: I'm writing this really late, going to edit later for more clarity.
Some linear algebra does the trick:
Express each operation as a matrix and matrix multiply those to combine them into a single resulting matrix (for efficiency).
If translation is involved you need to add a dimension to your matrices, see homogenous coordinates.
The reason is that the mappings are affine ones then, not linear ones. You can ignore the extra dimension in the end result. It is just a nice way to embed affine mappings into linear ones, so the algebra is easier.
Example
M = M_trans * M_rot * M_scale
x' = M x
The order here is right to left: vector x is first scaled, then rotated, then translated into vector x'. (Using column vectors).
Hints on the matrices: Rotation Matrix, Scaling Matrix
For deriving 2D formulas when given 3D ones: either keep z = 0 or delete the 3rd row and 3rd column from each matrix.

Adjust camera co-ordinates to represent change in azimuth, elevation and roll values

I'm currently working with libQGLViewer, and I'm receiving a stream of data from my sensor, holding azimuth, elevation and roll values, 3 euler angles.
The problem can be considered as the camera representing an aeroplane, and the changes in azimuth, elevation and roll the plane moving.
I need a general set of transformation matrices to transform the camera point and the up vector to represent this, but I'm unsure how to calculate them since the axis to rotate about changes after each rotation ( I think? ).
Either that, or just someway to pass the azimuth, elevation, roll values to the camera and have some function do it for me? I understand that cameraPosition.setOrientation(Quaterion something) might work, but I couldn't really understand it. Any ideas?
For example you could just take the three matrices for rotation about the coordinate axes, plug in your angles respectively, and multiply these three matrices together to get the final roation matrix (but use the correct multiplication order).
You can also just compute a quaternion from the euler angles. Look here for ideas. Just keep in mind that you always have to use the correct order of the euler angles (whatever your three values mean), perhaps with some experimentation (those different euler conventions always make me crazy).
EDIT: In response to your comment: This is accounted by the order of rotations. The matrices applied like v' = XYZv correspond to roation about z, unchanged y and then unchanged x, which is equal to x, y' and then z''. So you have to keep an eye on the axes (what your words like azimuth mean) and the order in which you rotate about these axes.

Resources