I don't see what offsetting the torque more would help matters, though?
Anyway, what I meant was:
Code:
ToAim = Barrel:toLocal(owner():aimPos()):normalized() #The direction from the barrel to the aim position, normalized and in the barrel's local coordinate system.
Error = vec(0,0,1):cross(ToAim) #Cross it with the up vector, which is [0,0,1] by definition of the local coordinate system. This is our error term - it's a vector normal to both the up and ToAim vectors and with a magnitude that approaches 0 as the two become approach parallell configuration, our goal.
Torque = Pgain * Error + Dgain * $Error
Barrel:applyTorque(Torque*Barrel:inertia())
In practice I guess it will perform much like Divran's quaternion code, but notably it will allow the barrel to spin freely about its up vector and uses a more reliable method for selecting which face of the barrel aims in the given direction. Adding 90 Pitch isn't always accurate.
Bookmarks