View Full Version : Rotation from a vector
sigh...it's friday and my brain has stopped functioning...help..
basically I have a normal's vector from the command 'polyInfo fn' and I have an object facing the positive Z axis with frozen transforms. What I want is to figure out the Y rotation value, so I can match the objects Y rotation with the face normal's Y rotation...
I hope that made sense..


sirpalee
10272008, 10:37 AM
To get the best results you need to access, the binormal and the tangent information of the face, and create a rotation matrix like this :
 t.x t.y t.z 
 n.x n.y n.z 
 b.x b.y b.z 
t > tangent vector
n > normal vector
b > binormal vector
(I have written a similar constrain node in the c++ api, and I used the method above)
In case anyone cares, I did manage to solve this. While the math may or may not be 'correct' I get the angle I'm looking for.
I stored the vector data from the face normal as $v and then used the angleBetween command ignoring the faceNormal's Y vector info (since I could assume the plane will always be flat)
angleBetween er v1 0 0 1 v2 $v1 0 $v3
GennadiyKorol
10282008, 07:26 PM
You are basically measured the angle between the projection of normal on XZ plane, but this will not solve the more complex rotations.
I have used the solution proposed by sirpalee on numerous occasions and it never lets you down. You just compose the matrix and be done :)
lol, I wouldn't know what to do with that matrix once I made it :P
I'm still wrapping my head around 3D math again as I haven't had to use if for quite some time.
vsPiotr
06102009, 02:49 AM
Gennadiy/Sirpalee, could you elaborate on that in mel format?
I'm facing the "more complex rotation"... but have no idea how to use transform matrices.
sirpalee
06102009, 07:14 PM
Hi, you should read the corresponding wiki section.
http://en.wikipedia.org/wiki/Matrix_(mathematics)
http://en.wikipedia.org/wiki/Rotation_matrix
http://en.wikipedia.org/wiki/Linear_transformation
It's a trivial problem, you should solve it yourself, don't except anyone to tell you how to transform a vector using matrices.
You should create a transformation matrix, that I described above, and after that, transform the up vector with that matrix using simple matrix / vector multiplication.
vsPiotr
06102009, 10:40 PM
Trivial for some :)
You're neglecting the fact that in contrast to most of Eastern Europe, Australian high schools teach basic trigonometry as an optional advanced subject, matrixes don't get mentioned, so my math skills, to the great disappointment of my parents, are piss weak :)
...besides that, I'm not really a TD.
Thanks for the links though, much appreciated. I will study them in more detail.
I did manage to get a somewhat working bit of mel using a matrix but I don't quite understand it yet.
vsPiotr
06112009, 09:34 AM
Hi,
Is this correct?
The below code essentially aligns and positions things the way I want them but I'm getting some strange behavior where shear and scale have some small fractional values (like 0.007) I'm can reset those after xform but it's not that neat.
i'm using pointOnSurface ($infoNode) to get normalized U V and N. it's part of a loop that instances pCube1.
float $p[] = `getAttr ($infoNode + ".position")`;
float $tu[] = `getAttr ($infoNode + ".ntu")`;
float $tv[] = `getAttr ($infoNode + ".ntv")`;
float $no[] = `getAttr ($infoNode + ".nn")`;
string $inst[] = `instance pCube1`;
float $m[] = {
$tu[0], $tu[1], $tu[2], 0,
$no[0], $no[1], $no[2], 0,
$tv[0], $tv[1], $tv[2], 0,
$p[0], $p[1], $p[2], 1};
xform matrix $m[0] $m[1] $m[2] $m[3] $m[4] $m[5] $m[6] $m[7] $m[8] $m[9] $m[10] $m[11] $m[12] $m[13] $m[14] $m[15] $inst[0];
ThE_JacO
06112009, 10:34 AM
lol, I wouldn't know what to do with that matrix once I made it :P
I'm still wrapping my head around 3D math again as I haven't had to use if for quite some time.
A 3x3 matrix represents rotation + scaling, and it's just three vectors determining the direction (and magnitude for scaling) of the three axis.
To get one from a poly face get the normal, get a vector from an edge (adding the vectors of the first and second point of that poly face) and that gives you a reliable upvector, except for very messed up shapes.
Once you have those you can run a cross product of your normal by your upvector, and that will give you a working binormal (the second of your axis). Another cross product between that and the normal will give you a third orthogornal axis, the third one. And you're good to go and can populate that matrix.
For the second vector it's also an option to project (and normalize, unit in maya) that upvector on the orthogonal plane of the normal, but it's slightly more involved than just running a cross product.
If you're not familiar with cross products, I suggest you look them up as part of an elementary study in linear algebra, there's only that far you can go without knowing at least the basics :)
CGTalk Moderation
06112009, 10:34 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 ©20002015, Jelsoft Enterprises Ltd.