# Rotating towards a moving object

Oh man, I hope you guys can clear the fog up. My brain has moved to Quaternion hell !

I have an object “A” that must point at a moving target “B”. It must rotate at a constant speed. As “B” moves, “A” locks on to it.

So I create a temp node, and use “lookAt(B)”. I’ve tried using “slerp” in the update method with tpf as the amount, but this isn’t right. As “A” gets closer to "B"s rotation, the rotation process slows down…

This seems so simple, but I’m throwing around "mult"s and “fromAngles” etc, and now I don’t know whether I’m coming or going!

use lookAt every frame, should work fine

@richard_hawkes said:
So I create a temp node, and use "lookAt(B)". I've tried using "slerp" in the update method with tpf as the amount, but this isn't right.

Slerp works from 0-1 0=QuaterionA rotation and 1=QuaternionB rotation, anything in between is anything in between the two ^^

Thanks for the super-fast response guys, I haven’t explained myself very well then.

Firstly, calling lookAt every frame would mean my object “A” would initially jump to point to object “B”. I’m after a smooth rotation towards it.

In terms of using slerp, the problem I have is what to set the value to. If I’m updating object "A"s local rotation every update, what do I “slerp” with in the next frame (bearing in mind that object “B” is still moving) !? I could set a value that starts at zero and progresses towards 1 at a given location. But of course, object “B” would have moved by then !

Hope this is making some sense! I should be able to knock some sample code up shortly!

Well if you want to turn in e.g. One second then have a value that you augment with tpf

slerpValue+=tpf;

1 Like

Each frame slerp towards the current location of the moving object…then you adjust for the movement as you go and “catch up” with it.

Thanks Normen and Zarch.

Zarch I think your approach is the best. I have to update the target rotation to consistently point at “B” and slerp towards that. In my mind that means it will speed up or slow down the rotation at the end, should “B” drastically change direction, rather than be consistently rotating. But that’s OK, I think for my purposes it’s going to be fine as the rotation is pretty quick.

Thanks again… Might wiki this little conundrum! I notice the importance of cloning my rotations here as well for each update.

• Richard