View Full Version : question regarding rotation
montujj 04-22-2008, 10:35 PM I have 2 vectors sharing same starting point let say at world origin 0 0 0 but different ending point in 3d space pointing different direction and I want vector 2 to line up with vector 1.
Q1. how do i find out required rotation in x y and z direction needed for vector 2 to line with vector 1.
Q2. xform -q -m objectName;
gives me 16 matrix values. can someone explain to me what these value means? how can i use them to find rotations?
i did some research online for this but the text i found so far are beyond my understanding as i dont have math background. any help would be great.
|
|
sj_bee
04-23-2008, 11:09 PM
Q1. how do i find out required rotation in x y and z direction needed for vector 2 to line with vector 1.
The angle command gets the angle between two vectors. Note that the return value is in radians, so use the rad_to_deg command to convert it to degrees:
rad_to_deg(`angle <<1,0,0>> <<0,0,1>>`);
// Result: 90.000001 //
The angle between the x and the z axis is 90 degrees.
The angleBetween command gets the axis and angle requred to rotate one vector onto another
angleBetween -v1 1 0 0 -v2 0 0 1;
// Result: 0 -1 0 90 //
The angle between the x and z axis is 90 degress about the -y axis.
Q2. xform -q -m objectName;
gives me 16 matrix values. can someone explain to me what these value means? how can i use them to find rotations?
Here is a brief explanation of matrices
The 16 numbers make up a 4x4 matrix which defines a transform. The matrix holds, among other things, infomation for translation, rotation and scale transformations.
The following matrices define rotation of an object about the x,y or z axis.
For a rotation of "A" degrees about the x, y and z axis, the following matrices are used:
////////
//rotate about the x axis
1 0 0 0
0 cosA -sinA 0
0 sinA cosA 0
0 0 0 1
//so a rotation of 90 degrees in x is defined by
1 0 0 0
0 0 1 0
0 -1 0 0
0 0 0 1
//apply the transform to an object
xform -matrix 1 0 0 0 0 0 1 0 0 -1 0 0 0 0 0 1 objectName;
////////
//rotate about the y axis
cosA 0 sinA 0
0 1 0 0
-sinA 0 cosA 0
0 0 0 1
//so a rotation of 90 degrees in y is defined by
0 0 -1 0
0 1 0 0
1 0 0 0
0 0 0 1
//apply the transform to an object
xform -matrix 0 0 -1 0 0 1 0 0 1 0 0 0 0 0 0 1 objectName;
////////
//rotate about the z axis
cosA sinA 0 0
-sinA cosA 0 0
0 0 1 0
0 0 0 1
//so a rotation of 90 degrees in y is defined by
0 1 0 0
-1 0 0 0
0 0 1 0
0 0 0 1
//apply the transform to an object
xform -matrix 0 1 0 0 -1 0 0 0 0 0 1 0 0 0 0 1 objectName;
For more complex transforms, such as the rotation of an object about an arbitary axis, a number of matrices will have to be multiplied together. If you really want to find out about it you can get info on the web, Im not going to go into it here.
To be honest i would not bother manipulating objects with matrices unless you really need to. There is usually a hack or workaround in MEL that you can use to get the infomation you want.
sergioduque
04-23-2008, 11:57 PM
Did you just launch a spaceship?
jk, but damn, passed by trying to learn some more stuff and everything, now that finally came to realise that all this mel scripting thing is hard but might be doable, made my first one an all, suddently just felt like yep! better leave this stuff for the big boys kiddo!
please don't take this in a wrong way. i'm not making fun of your explanation, just had to take this one out.
I didnt get nothing that you said but it sounded like a good explanation for someone who speaks the same language.
montujj
04-24-2008, 02:12 AM
thanks a lot for the reply. that is indeed a detail explanation. will try your suggestion using angleBetween command and see if i can get this working for me.
thanks for dymistifying matrix for me. i honestly want to avoid using matrix for my purpose but i ended up in the situation where i could not find easier solution. will try and post example of what i am trying to do.
montujj
04-24-2008, 03:34 AM
i tried angleBetween command for these two vectors like you suggested
angleBetween -v1 1 0 0 -v2 1 1 1;
// Result: 0 -0.707107 0.707107 54.73561 //
so how do you read this. how much is the angle between which axis and what direction. what does first 0 represent here. i am still confused. can you please explain.
lab00
04-24-2008, 09:52 AM
angleBetween -v1 1 0 0 -v2 1 1 1;
// Result: 0 -0.707107 0.707107 54.73561 //
This means that to rotate the vector v1 << 1, 0, 0>> into vector v2 << 1, 1, 1 >>
you need to rotate v1 around the arbitrary axis << 0, -0.707107, 0.707107>> by 54.73561°.
sj_bee
04-24-2008, 10:02 AM
i tried angleBetween command for these two vectors like you suggested
angleBetween -v1 1 0 0 -v2 1 1 1;
// Result: 0 -0.707107 0.707107 54.73561 //
so how do you read this. how much is the angle between which axis and what direction. what does first 0 represent here. i am still confused. can you please explain.
The two vectors <<1 0 0>> and <<1 1 1>> define a plane, there can only be one possible plane in 3D that both vectors lie on, and to rotate from one vector onto the other you must rotate through the plane.
The first three values <<0 -0.707107 0.707107>> of the return array define a normal to the plane, a third vector which lies at right angles to the first two vectors. This vector is the axis that you will need to rotate around to rotate <<1 0 0>> onto <<1 1 1>>.
The fourth value is the angle in degrees required to rotate <<1 0 0>> onto <<1 1 1>>.
It might be clearer to you if you had a look at the values in maya. If you run this code it will build a simple representation of what the numbers mean.
This code will build representations of the three vectors and keyframe the rotation. The red curve is the vector <<1 0 0>>, the yellow curve is the vector <<1 1 1>> and the blue curve is the vector about which the rotation occours <<0 -0.707107 0.707107>>, which is the normal to the plane defined by <<1 0 0>> and <<1 1 1>>.
modelEditor -e -grid 0 modelPanel4;
//a curve to represent the vector <<1 0 0>>
string $V1 = `curve -d 1 -p 0 0 0 -p 1 0 0 -k 0 -k 1`;
//make it red
setAttr ($V1 + ".overrideEnabled") 1;
setAttr ($V1 + ".overrideColor") 13;
//a curve to represent the vector <<1 1 1>>
string $V2 = `curve -d 1 -p 0 0 0 -p 1 1 1 -k 0 -k 1`;
//make it yellow
setAttr ($V2 + ".overrideEnabled") 1;
setAttr ($V2 + ".overrideColor") 17;
//a curve to represent normal <<0 -0.707107 0.707107>>
string $V3 = `curve -d 1 -p 0 0 0 -p 0 -0.707107 0.707107 -k 0 -k 1`;
//make it blue
setAttr ($V3 + ".overrideEnabled") 1;
setAttr ($V3 + ".overrideColor") 6;
//create a group to roatate the first vector onto the second
string $grp = `createNode transform`;
//orient the group to the normal vector
string $tmp[] = `spaceLocator -p 0 -0.707107 0.707107`;
xform -cp $tmp[0];
string $tmpAim[] = `aimConstraint -offset 0 0 0 -aimVector 0 0 1 -upVector 0 1 0 -worldUpType "vector" -worldUpVector 1 1 1 $tmp[0] $grp`;
delete $tmpAim[0];
delete $tmp[0];
//create a second grp with the same orientation as the first but zero rotations
string $grp2 = `createNode transform`;
setAttr ($grp2 + ".displayLocalAxis") 1;
parent $grp2 $grp;
makeIdentity-apply false -r 1 $grp2;
//parent first vector curve to this grp
parent $V1 $grp2;
//add keyframe for a rotation of 54.73561 [the 4th return value]
currentTime 1;
setKeyframe ($grp2 + ".rz");
currentTime 10;
setAttr ($grp2 + ".rz") 54.73561;
setKeyframe ($grp2 + ".rz");
currentTime 1;
select -cl;
Hope this helps, I am not very good at interpreting mathematical symbols either but I can understand a practical example or diagram.
montujj
04-24-2008, 03:45 PM
thanks again for taking time and pain to explain this to me.
I am going to try your code step by step to understand. i read half way and now i understand better about how this works. thats indeed a big help. i will get back once i go through your test code.
thanks again
montujj
04-24-2008, 04:48 PM
what does makeIdentity command do?
o.k. i tried your test code and it definately works. thanks a million. Now i am actually going to incorporate this in my script.
I am making a script which aligns, scale and orient trackdata group (with all the 3d points from tracking software ) to the geometry. i got align and scale thing worked while ago but i was having trouble with orientation. with your help and test code i will try and make it work now.....
sj_bee
04-24-2008, 05:31 PM
The code was just to help you or anyone else reading visualise the maths, but if it is of practical use then by all means take it.
what does makeIdentity command do?
The MEL help says...
The makeIdentity command is a quick way to reset the selected transform and all of its children down to the shape level by the identity transformation. You can also specify which of transform, rotate or scale is applied down from the selected transform. The identity transformation means:
translate = 0, 0, 0
rotate = 0, 0, 0
scale = 1, 1, 1
shear = 1, 1, 1
Have a look in the MEL command reference if you dont know what a command does. It is pretty good and it might save you from posting here.
The makeIdentity command is basically what is called when you go Modify > Freeze Transforms or Modify > Reset Transforms
montujj
04-24-2008, 11:38 PM
thank you for the response. i really appreciate your time for this. i looked into mel command reference for makeIdentity command earlier but i was not clear so i thought of posting it here for better explanation.
I still have couple of questions regarding the code.
Q1. does the vectors need to start at world 0 0 0 or it can be anywhere in 3D space.
Q2. How did we come to conclusion of rotating on Z axis.
Thank you
sj_bee
04-25-2008, 09:47 AM
Q1. does the vectors need to start at world 0 0 0 or it can be anywhere in 3D space.
The two vectors do not need to start at the origin, as they are direction vectors rather than position vectors.
As direction vectors it does not matter where they are in space, they just define a direction. You can read about position vectors and direction vectors here:
http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/geometry/basic.html (http://www.cs.mtu.edu/%7Eshene/COURSES/cs3621/NOTES/geometry/basic.html)
Q2. How did we come to conclusion of rotating on Z axis.
we rotate about the z axis because that was the axis i decided to aim down the normal vector when I created the aim constraint
string $tmpAim[] = `aimConstraint
-offset 0 0 0
-aimVector 0 0 1 //<<<HERE!
-upVector 0 1 0
-worldUpType "vector"
-worldUpVector 1 1 1 $tmp[0] $grp`;
I could have chosen the x or y axis though, it does not matter. One axis must point down the vector <<0 -0.707107 0.707107>> though, cos the vector is the axis of rotation. As defined by the origonal return value:
angleBetween -v1 1 0 0 -v2 1 1 1;
// Result: 0 -0.707107 0.707107 54.73561 //
montujj
04-26-2008, 12:33 AM
I got this rotation thing working finally. i have tested with different conditions and its works so far..thanks a lot for sharing your knowledge.
i had to calculate all vector rotations from 0 0 0 for this to work.
CGTalk Moderation
04-26-2008, 12:33 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.