i've been asked to recreate the to_track_quat function of mathutils to create a quaternion that looks from point a to point b, but so far i've been un successful.
I'm using in blender two objects, being their positions:
object.location = mathutils.Vector((0,0,3))
target.location = mathutils.Vector((2,4,4))
angle = look_at(object, target)
Here is my first function:
def look_at(u, v): u.normalize() v.normalize() dot = u.dot(v) cross = u.cross(v) q = mathutils.Quaternion() q.x = cross.x q.y = cross.y q.z = cross.z q.w = math.sqrt(u.length_squared*v.length_squared) + dot q.normalize() print(q) return q
Returns (w=0.9129, x=-0.3651, y=0.1826, z=0.0000)
The result is something like this:
I believe i'm missing an operation, but i'm not certainly sure what. Can someone guide me a bit on how to do it properly?
Another attempt that has been suggested:
def look_at(u, v): w = (v - u)/math.sqrt((v.x - u.x)**2 + (v.y - u.y)**2 + (v.z - u.z)**2) up = mathutils.Vector((0,-1,0)) q = mathutils.Quaternion() cross1 = w.cross(up) dot1 = w.dot(up) q.x = cross1.x q.y = cross1.y q.z = cross1.z q.w = math.sqrt(w.length_squared*up.length_squared) + dot1 q.normalize() return q