PDA

View Full Version : Look at object


Ojah
04-24-2009, 09:02 AM
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)

Ojah
04-24-2009, 10:08 AM
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.

Alex Morris
04-24-2009, 03:43 PM
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)

decon
04-29-2009, 09:41 AM
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

CGTalk Moderation
04-29-2009, 09:41 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.