- Home /
Limit local rotation around x, y, and z axes?
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:
var minlimit=Vector3(-20,-20,-20);
var maxlimit=Vector3(30,30,30);
var negconvx=newQuat.eulerAngles.x;
var negconvy=newQuat.eulerAngles.y;
var negconvz=newQuat.eulerAngles.z;
if(newQuat.eulerAngles.x>=180){
negconvx=newQuat.eulerAngles.x-360;
}
if(newQuat.eulerAngles.y>=180){
negconvy=newQuat.eulerAngles.y-360;
}
if(newQuat.eulerAngles.z>=180){
negconvz=newQuat.eulerAngles.z-360;
}
if(negconvx<=minlimit.x){
x=minlimit.x;
}
if(negconvy<=minlimit.y){
y=minlimit.y;
}
if(negconvz<=minlimit.z){
z=minlimit.x;
if(newQuat.eulerAngles.x>maxlimit.x){
x=maxlimit.x;
}
if(newQuat.eulerAngles.y>maxlimit.y){
y=maxlimit.y;
}
if(newQuat.eulerAngles.z>maxlimit.z){
z=maxlimit.z;
-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:
var trend=currentAngles[boneindex]-previousAngles[boneindex];
var perp: Vector3 = Vector3.Cross(transform.forward, trend);
var rotdirection: float = Vector3.Dot(perp, transform.up);
if (rotdirection > 0.0) {
//turning clockwise
var dirNum=1.0;
} else if (rotdirection< 0.0) {
//turning counterclockwise
dirNum=-1.0;
} else {
dirNum=0.0;
}
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.
Your answer
Follow this Question
Related Questions
Reverse rotations problems... 2 Answers
Avoiding Gimbal Lock 1 Answer
Quaternion rotation flips axises when upside down 0 Answers
unwanted rotation on the y and z axes 1 Answer
IMU Sensor and Quaternion 2 Answers