Page 1 of 1

Closest equivalent to Unity's "LookAt" rotation in

Posted: Mon May 27, 2013 6:07 am
by dvochin
Hello all, i'm trying to rotate some vertices in a mesh about a 3D cursor while preserving the up vector.

If I were in Unity I'd use the "LookAt()" function by specifying what I want the object to look at and also specifying the 'worldUp' vector to make the rotation robust for all rotations:

---
from http://docs.unity3d.com/Documentation/S ... ookAt.html

"function LookAt (worldPosition : Vector3, worldUp : Vector3 = Vector3.up) :
Rotates the transform so the forward vector points at worldPosition. Then it rotates the transform to point its up direction vector in the direction hinted at by the worldUp vector. If you leave out the worldUp parameter, the function will use the world y axis. worldUp is only a hint vector. The up vector of the rotation will only match the worldUp vector if the forward direction is perpendicular to worldUp. "
---

What would be the closest Blender equivalent to doing "LookAt" rotation?

Thanks for any help! Lost on this one!!

Dan,

Posted: Mon May 27, 2013 3:01 pm
by CoDEmanX
isn't that a rotation about the up-axis? you can simply calculate the angle difference between the target vector (looks at target) and the forward vector, then rotate around z-axis by that angle. If the rotation axis isn't at the same location as the up-axis, then do as follows:

Code: Select all

mat = center * rotation_mat * center.inverted()
me.transform(mat)
me.update()
center needs to be a 4x4 translation matrix. You can use the location of the object, the median point, the center of mass or whatever you want - depends on what you desire.

You can construct a translation matrix like
Matrix.Translation(vector)

and a rotation matrix like
Matrix.Rotation(radians(num_of_degrees), 4, axis)

axis can either be a char ('X', 'Y' or 'Z') or a vector.

Posted: Tue May 28, 2013 2:20 am
by dvochin
Hi CoDEmanX, thanks for taking the time and the trouble...

Intersting approach... didn't know you could multiply quaternions like that...

Will research this and try it out! Thanks again!

Dan,

Posted: Tue May 28, 2013 11:05 am
by CoDEmanX
it's matrices, not quaternions. Not sure how to do the same with quats, but it would probably work as well.