View Full Version : Rotation from a vector

10 October 2008, 11:01 PM's friday and my brain has stopped

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..

10 October 2008, 09: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)

10 October 2008, 11:06 PM
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

10 October 2008, 06: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 :)

10 October 2008, 09:23 PM
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.

06 June 2009, 01: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.

06 June 2009, 06:14 PM
Hi, you should read the corresponding wiki section.

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.

06 June 2009, 09: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.

06 June 2009, 08:34 AM
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.

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];

06 June 2009, 09: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 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 :)

CGTalk Moderation
06 June 2009, 09: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.