6

What's the correct/best way of constraining a 3D rotation (using Euler angles and/or quaternions)?

It seems like there's something wrong with my way of doing it. I'm applying the rotations to bones in a skeletal hierarchy for animation, and the bones sometimes visibly "jump" into the wrong orientation, and the individual Euler components are wrapping around to the opposite end of their ranges.

I'm using Euler angles to represent the current orientation, converting to quaternions to do rotations, and clamping each Euler angle axis independently. Here's C++ pseudo-code showing basically what I'm doing:

Euler min = ...;
Euler max = ...;

Quat rotation = ...;
Euler eCurrent = ...;

// do rotation
Quat qCurrent = eCurrent.toQuat();
qCurrent = qCurrent * rotation;
eCurrent = qCurrent.toEuler();

// constrain
for (unsigned int i = 0; i < 3; i++)
    eCurrent[i] = clamp(eCurrent[i], min[i], max[i]);
3
  • In general using quaternions is preferable, because they do not suffer from the limitations of Euler angles (like gimbal lock, which is possibly what you are experiencing). Use quaternions whenever you require complex rotations (for example on the 3 axis). Euler angles are just fine on simple ones.
    – user703016
    May 19, 2012 at 6:56
  • The reason I'm using Euler angles is so I can constrain them. I don't know how to constrain quaternions.
    – KTC
    May 19, 2012 at 16:57

2 Answers 2

1

One problem with Euler angles is that there are multiple ways to represent the same rotation, so you can easily create a sequence of rotations that are smooth, but the angles representing that rotation may jump around. If the angles jump in and out of the constrained range, then you will see effects like you are describing.

Imagine that only the X rotation was involved, and you had constrained the X rotation to be between 0 and 180 degrees. Also imagine that your function that converts the quaternion to Euler angles gave angles from -180 to 180 degrees.

You then have this sequence of rotations:

True rotation    After conversion    After constraint
179              179                 179
180              180                 180
181             -179                 0

You can see that even though the rotation is changing smoothly, the result will suddenly jump from one side to the other because the conversion function forces the result to be represented in a certain range.

When you are converting the quaternion to Euler angles, find the angles that are closest to the previous result. For example:

eCurrent = closestAngles(qCurrent.toEuler(),eCurrent);
eConstrained = clampAngles(eCurrent,min,max);

remember the eCurrent values for next time, and apply the eConstrained rotations to your skeleton.

4
  • I don't understand. It just seems like you've rewritten the problem in terms of "closestAngles".
    – KTC
    May 20, 2012 at 21:48
  • The point is when you are converting to Euler angles, you have to consider the previous angles as well, so that your Euler representation of the rotations is smooth. May 21, 2012 at 3:32
  • Alright, I see what you mean. Are you sure it's valid to do that for each axis independently though? I tried it and couldn't ever get it to work. It did fix the "jumping", but the bones still ended up in invalid orientations. I think converting back and forth between Euler and quaternions has other problems besides the angles wrapping around.
    – KTC
    May 21, 2012 at 4:32
  • @KTC: It depends on what the euler conversion does. If the middle rotation of the sequence is not guaranteed to be between -90 and +90 degrees, then you can run into another set of equivalent angles. May 21, 2012 at 5:11
0

The problem here is that the constraints you are applying have no relation to the rotation be applied. From a conceptual point of view this is what you are trying to achieve:

  • assume a bone is in an unconstrained state.
  • apply rotation
  • has bone exceeded constraints? If yes, rotate it to where it is not constrained any more

Your code that clamps the Euler rotations is the part where you rotating the bone back. However this code ignores the original bone rotation so you will see odd behavior, such as the snapping you are seeing.

A simple way to work with this is to do this instead:

  • assume a bone is in an unconstrained state
  • apply rotation
  • test if bone exceeded constraints
  • if yes, we need to find where the constraint stops movement.
    1. halve the rotation, apply it in reverse
    2. is the bone exceeding constraints? If yes go to 1
    3. If no, halve the rotation, apply it in the forward direction. Goto 2
  • keep doing that until you are within some tolerance of your constraining angles

Now this will work, but because your rotation quarternions is being applied on all angles, the rotation will stop when any one of those constraints are net, even if there is freedom some where else.

If instead you apply rotations independently of each other, then you will be able to reliable use your clamping or the above technique to honor constraints, and also rotate as closely to your target as you can. - -

7
  • Converting between quaternions and Euler angles seems to break this solution. I'm not sure how to apply rotations independently while still using quaternions.
    – KTC
    May 20, 2012 at 21:49
  • The short answer is you don't. If you need to independently apply rotations, your internal data structures ought to keep the Euler rotations separate as well as the order those rotations ought to be applies. May 20, 2012 at 22:01
  • I won't be able to do that. I'd like to use quaternions to interpolate between keyframes.
    – KTC
    May 20, 2012 at 22:39
  • Could you elaborate on the problems you saw when trying to find the point where the constraint was hit then please? May 21, 2012 at 0:26
  • 1
    I have a lot of joints that I want to constrain to just rotation around the X and Y axes, and not roll around the Z axis. But a quaternion representation of the rotation always seems to introduce some roll when I convert it back to Euler, so the rotation is always outside of its constraints.
    – KTC
    May 21, 2012 at 1:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.