View Full Version : Gimbal Lock Problem

Aramil

11-11-2006, 09:12 AM

Hello again...okay, my next project is a 3D space combat almost-sim.

The ship should be able to pitch and roll depending on the joystick positions. Unfortunately, I'm encountering a problem that I believe is called gimbal lock. I've heard that this can be corrected with quaternion math, but my math education isn't advanced enough yet for me to understand this. Can anyone direct me to any useful libraries/engines/code snippets or techniques that wouldn't be too complex? Thanks.

Sv3nni

11-11-2006, 04:52 PM

The Nebula device has a good math-lib.

http://nebuladevice.cubik.org/

You can copy from there.

Gimbal lock is not solved with quaternions. Gimbal lock is an effect of the rotation representation you use, not how you realize those rotations. So it's just as easy to run into it with quaternions as with matrix rotations, and you can avoid it as easily with matrix rotations as with quaternions.

The Gimbal lock is an effect of Euler angle rotations, and switching to quaternions, but preserving the underlaying Euler angle rotation system, will not eliminate the Gimbal lock problem. If you solve it with quaternions, you have solved it with matrix rotations also.

What you have to do to avoid Gimbal lock is to not use Euler angles. Look into axis/angle rotation systems instead, in which case quaternions are useful.

Also, but I'm not entirely sure, I don't think you can ever run into the Gimbal lock problem by only using two euler angles. You need three angles to even have a chance. Rotate first angle by a certain amount so the two remaining rotation axes are the same and you lose one degree or freedom. With two rotation angles, you still have the remaining degree of freedom left for use.

Aramil

11-12-2006, 06:46 AM

Okay...maybe my problem isn't gimbal lock. I'll describe it to you.

So, you know that I want my ship to pitch and roll. Whenever my pitch is at zero degrees, my roll works perfectly. However, when I pitch down 90, my roll becomes yaw. I think I described my problem wrong...how can I fix this?

yooyo

11-12-2006, 10:49 AM

Your ship have current orientation. Make additional rotation based on *delta* values from input device and compose it. Example:

float oldJoyX, oldJoyY;

mat4 CurrentShipMatrix;

...

float x,y;

ReadJoystick(&x,&y); // absolute values from -1 to 1

float dx = x- oldJoyX;

float dy = y- oldJoyY;

oldJoyX = x;

oldJoyY = y;

mat4 deltarot = BuildRotationMatrix(dx, dy);

CurrentShipMatrix *= deltarot;You may need additional code to adjust rotation speed.

Even more.. you can use absolute values from joystick as delta values.. when player pull or push joystic more, ship will rotate faster.

Aramil

11-13-2006, 07:07 AM

Hmmm...what type of variable is "mat4", and what's "BuildRotationMatrix"?

I understand what you're getting at with how the rotation will increase with the joystick angle...I'm not sure how this can solve my problem; I don't understand those types mentioned above. Could you please explain them, or where you got them?

yooyo

11-13-2006, 08:49 AM

pfff.. It's a pseudocode. I suppose you already have your own math library. In short...

mat4 is 4x4 matrix

BuildRotationMatrix(roll, pitch) should build 4x4 matrix based on rool and pitch value.

a *= b is equal to a = a* b

Aramil

11-13-2006, 10:24 AM

Alright, got that far.

a *= b is equal to a = a* b Lol, was that sarcasm?

I'm assuming that "BuildRotationMatrix" is simply creating a 4x4 matrix and just inserting those roll and pitch values into the corresponding slots?

Once I've build that new one, I just multiply the old matrix with the rotation one.

Originally posted by Aramil:

So, you know that I want my ship to pitch and roll. Whenever my pitch is at zero degrees, my roll works perfectly. However, when I pitch down 90, my roll becomes yaw. I think I described my problem wrong...how can I fix this? What you describe here sounds a lot like you apply pitch and roll in the wrong order.

yooyo

11-13-2006, 11:54 AM

@aramil:

When you fly a space ship... your controls should be applyed relative from ship orientation. You can't apply absolute rotations.

Sarcasm... not.. Since you didn't know what is mat4 I was think it is better to explain multiplication too. Maybe im not precise enough... It was matrix multiplication, ie. matrix composition.

Keep up good work!

Aramil

11-14-2006, 08:12 AM

I can work with that stuff. Thanks!

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.