[Closed] Look at object
Hi there, I’m having some trouble with a piece of code where an object has to look at another object by turning only on the z-axis. Whenever I apply the rotation it seems as if the object can turn both sides, not being predictable.
I hope anyone can help me
heres the code I’m using for testpurposes atm.
lookAtTarget = $sphere01
transformTarget = $teapot01
-- lookAt code
A=transformTarget.pos
in coordsys transformTarget transformTarget.pos += [1000,0,0]
B=transformTarget.pos -- getting the directionvector
in coordsys transformTarget transformTarget.pos += [-1000,0,0]
C=[lookAtTarget.pos.x,lookAtTarget.pos.y,transformTarget.pos.z]
v1=normalize(B-A)
print v1 as string
v2=normalize(C-A)
theAngle=acos(dot v1 v2)
print "blaat" + theAngle as string
rotate transformTarget (eulerangles 0 0 theAngle)
I seem to get it to work in a testfile, with “transformTarget.dir = v2” instead of using rotate. But, when I put this in a function and try to save the rotation in a transformmatrix it will move the objects when I recall it later.
normally you’d do this with a dummy linked between, and then disable the transform inheritance from the dummy to the object you want to rotate in one axis only (if that makes sense):
lookAt target -> Dummy(with lookAt constraint) -> object that rotates (linked to dummy and inheritance of z-rotation enabled/disabled in the motion panel)
or simply :
myObj = if $Box01 != undefined then $Box01 else Box pos:[100,100,0] height:50 length:2
myTarg = if $Sphere01 != undefined then $Sphere01 else Sphere()
theY = myTarg.pos - myObj.pos
theY.z = 0.0
theY = normalize theY
theX = cross theY [0,0,1]
TM = matrix3 1
TM.row1 = theX
TM.row2 = theY
TM.row4 = myObj.pos
myObj.transform = TM