1 hour ago, alvaro said:The way I would do it is:
- Compute the quaternion that would produce the "most natural rotation" that would align your missile with the target.
- Look at its real coordinate to see if the rotation is larger than the limit. The real part should be at least cos(limit_angle/2). If needed, modify the quaternion so its real part is exactly cos(limit_angle/2), scaling the imaginary parts to keep the quaternion having unit length.
- Apply the quaternion to rotate the missile.
You can find code to compute the "most natural rotation" in this thread.
... which is exactly what i've done in my first reply. Your 'most natural rotation' is just the shortest rotation between two directions. You calculate it by converting axis and angle to quaternion, while i kept working with axis and angle because i think it's more intuitive for somebody asking a question like this.
Only mentioning this to avoid confusion or wrong expections, correct me if i'm wrong.
The problem is, a turret mounted on ground or a vehicle can not take any most natural rotation because it is usually constructed by two hinges. The up vector fixes this in a simple way, but this does not help with joint limits. So if this is necessary we either need to model the two hinges, or (more easily) convert the target direction to spherical coords in the turret reference orientation and clip them.