# Turning a node towards another

I need some way to have a node turn some number of degrees towards another, like:

``obj.turnTo(obj2, 30)``

I’ve made a few of my own versions, but they all suffer from weird gimbal problems or crazy behavior. There must be ‘proper’ way to do it (builtin, involving quats, whatever) that I’ve not heard of.

Anybody got any tips?

``````obj.headsUp(obj2)
obj.setH(obj.getH()+30)``````

Does that work?

no that would not work.

``````
def turn(start,stop,degree):
while start < 0 : start += 360
while stop  < 0 : stop += 360
while start >= 360 : start -= 360
while stop  >= 360 : stop -= 360
delta = abs(start-stop)
delta = min(delta,360-delta)
if delta < degree: degree = delta
diff = stop-start
if (diff > 0 and diff <= 180) or (diff < -180):
return start+degree
else:
return start-degree

def turnHpr(start,stop,degree):
return Vec3(turn(start,stop,degree),
turn(start,stop,degree),
turn(start,stop,degree))

def turnNodeTo(node,pos,degree):
hpr = node.getHpr()
node.lookAt(Point3(pos))
wantHpr = node.getHpr()
hpr = turnHpr(hpr,wantHpr,degree)
node.setHpr(hpr)
``````

I was looking for this the other day myself, as I wanted to extract a quaternion from lookAt. I found an older thread where Treeform and drwr had posted very similar code.

Personally I would rather see a version of lookAt which returns a transform directly instead of having it effect the nodepath directly, but I can see why it was done that way.

Still puzzling through the vector/math operations that Panda provides here too.

Got that too:

``````m = Mat4()
lookAt(m, forward)
print m
q = Quat``````

David

Oh! Awesome! But where is that coming from? It’s a global function I am guessing?

Yes:

``from pandac.PandaModules import lookAt``