Advertisement

Is this Gimbal Lock at work or do I have a real problem?

Started by July 08, 2013 01:04 AM
11 comments, last by newObjekt 11 years, 7 months ago

Here's a quick video I recorded:

Currently I am using bullet physics to calculate my RigidBody physics actors. After each bullet step I basically run this code on my RigidBody game actors to get their position and rotation from bullet.

      //the variable "physics" is the bullet RigidBody object

        Vector3f c = physics.getCenterOfMassPosition(new Vector3f());
        setLocation(new Vertex(c.x, c.y, c.z + 10));

        Quat4f q = physics.getOrientation(new Quat4f());
        setRotation(MathUtil.convertQuat2Euler3(new Quat(q.x, q.y, q.z, q.w)));

Problem A:

I'm currently converting the Quaternion that is returned by the RigidBody to an Euler angle then applying that to the object and rendering.

I'm like 99% sure thats causing gimbal lock. I can rewrite my renderer to use Quat's instead of Euler angles and solve the problem if that is it. It's a lot of work though so I want to confirm that this is the problem.

Problem B:

I have to add 10 to the location.z value of the RigidBody for some odd reason. I have no idea why.

Thanks :)

You should be using MotionStates to receive bullet notifications about your objects changing positions.

If your engine needs to be fed a 3x3 matrix, you could use the btTransform that bullet sends you though the motion state (see previous link) and call .getBasis() on it to get a 3x3 matrix. That way you won't need to do quaternion<->matrix convertions and you may also benefit from bullet's interpolation extrapolation.

I'm not familiar with java, and don't know exacly what's wrong with your current code.

Advertisement

You should be using MotionStates to receive bullet notifications about your objects changing positions.

If your engine needs to be fed a 3x3 matrix, you could use the btTransform that bullet sends you though the motion state (see previous link) and call .getBasis() on it to get a 3x3 matrix. That way you won't need to do quaternion<->matrix convertions and you may also benefit from bullet's interpolation extrapolation.

I'm not familiar with java, and don't know exacly what's wrong with your current code.

Ah okay. Can MotionStates give me the rotation in Euler angles?

My engine currently requires that I get object rotations in Euler angles but I can convert the engine to use Quaternions if that will fix the problem I am having with the spastic rotations.

With the .getBasis() method I assume the matrix would be:

location.x rotation.x velocity.x

location.y rotation.y velocity.y

location.z rotation.z velocity.z

Or something similar?

Problem A:

I'm currently converting the Quaternion that is returned by the RigidBody to an Euler angle then applying that to the object and rendering.

I'm like 99% sure thats causing gimbal lock. I can rewrite my renderer to use Quat's instead of Euler angles and solve the problem if that is it. It's a lot of work though so I want to confirm that this is the problem.

Problem B:

I have to add 10 to the location.z value of the RigidBody for some odd reason. I have no idea why.

Thanks smile.png

This is unlikely gimbal lock and more than likely a simple math problem. The problem is likely 'related' to gimbal lock but not in the way you might be thinking. Converting from quat to euler can and should be a perfect operation, there is no orientation which can not be represented in euler, but the way you perform the conversion has to account for the order of rotations which Euler implies, as such as things near various axis' the order of rotations will cause changes in how the Euler angles are represented. Assuming a fairly normal Euler representation, most will be BPH ordered if I remember correctly. (Bank, pitch then heading.) If your engine uses a different order then the conversion of a quat to Euler will likely look correct in 90% of the cases and then do what I see in the video, all of a sudden change.

Overall, if you are using Euler for rendering, you have got to change it. It's just a bag of pissed off cats waiting to bite you. Euler angles are great for UI representations, pretty much everywhere else, avoid them like the plague.

Yeah, the use of Euler was a choice I made before I had much experience. Won't ever do it again in the future. As it stands just actor rotations use Euler angles. Animations and what not use Quaternions.

As for the ordering, I might have it wrong. I will swap them around tomorrow morning when I start working and see if it solves the problem.

Looking for an honest opinion, should I just go ahead and rewrite the actors to use quats and call it done? I believe I have a quat slerp fucntion already written for animations (not on linux atm so can't check) so I could pretty easily convert it. It'd take maybe an hour or two to do it.

Oh and here's a random question. How much of a speed difference is their between using floats and doubles for graphics and physics. I was considering switching to floats if it's a big enough difference.

Overall, I highly suggest switching to *anything* other than Euler, matrix or quaternion's, your choice. Of course, at that point you really need to know/understand the math involved as Eulers are fairly easy to visualize (ignoring gimbal lock) where matrix/quats and of course the resulting heavy use of vectors gets much more difficult to visualize as you work.

As to the floats versus doubles thing. CPU performance wise, it makes no difference. Anytime a x86/64 works on a float it internally converts it to 80bits for the actual work. On older CPU's you "could" get a bit of a performance benefit to the cycle times by setting the fpu to low precision mode but that is no longer true as I understand it. So, from the CPU side there is no benefit. But.. There is a large benefit overall to using 32bit floats in terms of cache and memory which are the slowest bits anymore. Using a double takes twice as much memory so you have all sorts of reasons the double will be slower: more cache lines in use, less per cache line, more likely to span multiple lines etc and of course more memory movement all over.

Memory is pretty much the primary performance hit for most programs. Reductions of memory read and memory write (cache line level) are the largest possible optimizations anymore in real work.

Alright. I'll make that my project for tomorrow. Out with Euler in with Quat. I understand Quat moderately well as I have worked with it extensively with skinned meshes and animations. It'll be nice to have the whole engine using the same rotation code heh.

I'll also make a branch and convert the engine from doubles to floats and see how much of a difference it makes since it sounds like it might have an impact on performance.

Thanks for the advice. Cheers.

I'll also make a branch and convert the engine from doubles to floats and see how much of a difference it makes since it sounds like it might have an impact on performance.

Keep in mind that this is an "overall" performance item. It won't likely show up notably unless you really beat on things. I.e. early engines won't show any notable difference, only when you have a fair sized simulation are you likely to notice the difference in major ways. Any initial game is likely not to see any benefit, only when your simulation times start eating up notable portions of your frame time does this really make a difference.

Yeah I very well understand that. When I want to stress test something I make the engine load the BSPs of 5 maps at once or spawn in 200 bouncing physics boxes. Etc etc...

Regardless you seem very knowledgeable about this so I'd take your word that it will make a difference either way. :P

Advertisement

You should be using MotionStates to receive bullet notifications about your objects changing positions.

If your engine needs to be fed a 3x3 matrix, you could use the btTransform that bullet sends you though the motion state (see previous link) and call .getBasis() on it to get a 3x3 matrix. That way you won't need to do quaternion<->matrix convertions and you may also benefit from bullet's interpolation extrapolation.

I'm not familiar with java, and don't know exacly what's wrong with your current code.

Ah okay. Can MotionStates give me the rotation in Euler angles?

My engine currently requires that I get object rotations in Euler angles but I can convert the engine to use Quaternions if that will fix the problem I am having with the spastic rotations.

With the .getBasis() method I assume the matrix would be:

location.x rotation.x velocity.x

location.y rotation.y velocity.y

location.z rotation.z velocity.z

Or something similar?

MotionStates will not give you rotation in Euler angles. It will only give you a btTransform which is composed of a btMatrix3x3 (rotation matrix) and a btVector3 (object's translation/position).

The 3x3 matrix is a rotation matrix, it will only describe the rotation of the object. You should study how they work, it's pretty interesting and will probably be useful to you in the long term. Simply put they describe how each axis' coordinate is affected (by the rotation).

Made the changes and problem is solved. Thanks again AllEightUp.

Made the changes and problem is solved. Thanks again AllEightUp.

There's a glitch at 0:36

Made the changes and problem is solved. Thanks again AllEightUp.

There's a glitch at 0:36

That's most likely just encoding. When I record my desktop with ffmpeg it tends to skip a few frames every once and a while.

This topic is closed to new replies.

Advertisement