I’m rotating a character's arm toward a target object. I have a maximum and a minimum rotation limit (for each of the x, y, and z directions) that I want the arm to stay within the range of.
I have tried a number of things that have all caused the arm to do a strange flipping behavior, which I think is resulting from gimbal lock. I have encountered this problem of gimbal lock when I tried:
-Setting the angle to the max limit when the attempted rotation exceeded that range. And if the angle of attempted rotation was between 180 and 360, I converted the number to a negative, and then set it to the minlimit if it was less than that:
-using Mathf.Clamp to clamp the rotations between a maximum and a minimum for x, y, and z as listed in the second answer for this limitrotation post.
-converting the range of rotation as suggested in the answer to this limitlocalrotation post. As mentioned in the comments section to that answer, the solution only solved for clamping after a small rotation, but not for clamping an arbitrary angle to the limits
I was thinking that one way I could get around the strange flipping behavior that I’m noticing is to be able to know when the arm is rotating clockwise or counterclockwise. If the arm is moving clockwise and is rotated beyond the maxlimit, I would set it to maxlimit. And if the arm is moving counterclockwise and is rotated beyond its minlimit, I would set it to minlimit.
I tried the code that's listed here to determine clockwise and counterclockwise direction with using the dot product and then the cross product, but it didn't reliably give me 1 for clockwise and -1 for counterclockwise for each axis of rotation. This is what I tried based on their code, but didn't work:
How can I determine if the rotation of an object toward a target is going in the counterclockwise or clockwise direction around the x, y, and z axes?
If there's a better way to limit the rotation without gauging counterclockwise or clockwise, please do share.
Any help would be very much appreciated, as I've been trying to figure out this problem for quite some time, scouring the forum posts and doing little experiments. I'm sort of new to this, so if I'm doing anything wrong, I'm open to constructive feedback.
If your rotation angles were more limited (max angle at 170° instead of 180° for example), can this be considered a solution?
If not, can we have a visual explanation of the behaviour you are looking for? (screenshot, drawing)
answered Mar 21 '12 at 06:37 PM