CGTalk Rotation from a vector
 10-24-2008, 11:01 PM #1 Norb Frequenter   ... Technical Artist Electronic Arts Canada Canada   Join Date: Jul 2002 Posts: 266 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.. share quote
 10-27-2008, 09:37 AM #2 sirpalee death from above   portfolio Pal Mezei Software Engineer Luma Pictures Melbourne, Australia   Join Date: Feb 2004 Posts: 88 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) share quote
 10-27-2008, 11:06 PM #3 Norb Frequenter   ... Technical Artist Electronic Arts Canada Canada   Join Date: Jul 2002 Posts: 266 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) Code: ``` angleBetween -er -v1 0 0 1 -v2 \$v1 0 \$v3``` share quote
 10-28-2008, 06:26 PM #4 GennadiyKorol Know-it-Not   portfolio Gennadiy Korol Senior Coffee Maker Animation Lab Israel   Join Date: Aug 2003 Posts: 661 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 __________________ Diffusion Curves Wip share quote
 10-28-2008, 09:23 PM #5 Norb Frequenter   ... Technical Artist Electronic Arts Canada Canada   Join Date: Jul 2002 Posts: 266 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. share quote
 06-10-2009, 01:49 AM #6 vsPiotr The portfolio Piotr Stopniak 3D Artist Toby And Pete Sydney, Australia   Join Date: May 2006 Posts: 180 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. __________________ http://www.tobyandpete.com http://piratorbay.tumblr.com share quote
 06-10-2009, 06:14 PM #7 sirpalee death from above   portfolio Pal Mezei Software Engineer Luma Pictures Melbourne, Australia   Join Date: Feb 2004 Posts: 88 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. Last edited by sirpalee : 06-10-2009 at 06:18 PM. share quote
 06-10-2009, 09:40 PM #8 vsPiotr The portfolio Piotr Stopniak 3D Artist Toby And Pete Sydney, Australia   Join Date: May 2006 Posts: 180 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. __________________ http://www.tobyandpete.com http://piratorbay.tumblr.com Last edited by vsPiotr : 06-10-2009 at 10:10 PM. share quote
 06-11-2009, 08:34 AM #9 vsPiotr The portfolio Piotr Stopniak 3D Artist Toby And Pete Sydney, Australia   Join Date: May 2006 Posts: 180 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 re-set 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. Code: ``` 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];``` __________________ http://www.tobyandpete.com http://piratorbay.tumblr.com share quote
06-11-2009, 09:34 AM   #10
ThE_JacO
MOBerator-X

portfolio
Raffaele Fragapane
That Creature Dude
Animal Logic
Sydney, Australia

Join Date: Jul 2002
Posts: 10,952
Quote:
 Originally Posted by Norb 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 up-vector, and that will give you a working bi-normal (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
__________________
"As an online CG discussion grows longer, the probability of the topic being shifted to subsidies approaches 1"

Free Maya Nodes

Last edited by ThE_JacO : 06-11-2009 at 09:36 AM.

 06-11-2009, 09:34 AM #11 CGTalk Moderation Lord of the posts   Join Date: Sep 2003 Posts: 1,066,481 Thread automatically closed 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. __________________ CGTalk Policy/Legalities Note that as CGTalk Members, you agree to the terms and conditions of using this website. share quote