x


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.

more ▼

asked Mar 13 '12 at 04:25 AM

unitynewbie1 gravatar image

unitynewbie1
18 2 4 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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)

more ▼

answered Mar 21 '12 at 06:37 PM

Dakwamine gravatar image

Dakwamine
534 4 7 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2173
x443
x134
x61
x9

asked: Mar 13 '12 at 04:25 AM

Seen: 830 times

Last Updated: Mar 21 '12 at 06:37 PM