How can I avoid Gimbal Lock with Rotations?

I’ve unlocked the Character Capsule’s Pitch and Roll rotations. I’ve setup a MakeRotZX from the Characters location in Z, and the characters Forward Vector in X, which produces the characters Up Vector Always pointing towards the Worlds Center (0,0,0), Leaving only the Yaw available for mouse input to rotate the character along it’s Yaw. This works up until the character capsule has tilted around 90 degrees, at that point the Rotation starts to Gimbal lock and fall over onto it’s side (it’s up vector no longer pointer towards the desired coordinate and Yawing the character no longer rotate along it’s yaw only).

  1. Is there a way I can fix this inside of BluePrint? It was suggested that I try to only rotate the Pitch and Roll first (zeroing out the Yaw) and then Rotate this Rotation Vector by the desired Yaw Input Rotation and then set the character to that result. However I either this doesn’t work, or I wasn’t correctly setting it up.

  2. So, If this is a possibility does anyone know the correct way to do this in BluePrints?

  3. If it’s not possible to avoid Gimbal Lock in blueprints (without compromising the functionality I’m looking for described above) how can I do this in Code using Quaternions?

Thanks in advance!

Use Combine Rotator node, Combine the rotators of the turning rotation, from the input axis turn float with the MakeRotZX, z = actor location * -1, x = actor forward vector. This stopped the camera from wobbling into Gimbal lock near the poles. It also stopped the control from completely flipping on the opposite hemisphere.

Hi, any chance you could put up a screenshot of this solution? I’m having similar issues and am slightly puzzled at how you managed to solve it. Many thanks

Keep in mind that you have to set a true value to to these settings in the character.cpp

bUseControllerRotationPitch = true;
bUseControllerRotationRoll = true;
bUseControllerRotationYaw = true;

otherwise they’ll forbid those movements from occurring to the character capsule.

Thanks for that. I’m modifying an Actor’s transformation directly so hopefully this should behave ok. I’m getting odd flipping around the poles (gimbal) so hopefully this can help with that.
cheers

I have been using Add Actor Local Rotation and letting the player rotate on all 3 axes like in Descent. It doesnt seem to gimbal lock when i roll, pitch and yaw anywhere i want, but i have only tested it for a few minutes and maybe didnt hit the sweet spot that would lock it. I guess if it happens I’ll rewrite that part using combinerotators.

This actually didn´t work in 4.19.1. I got weird flipping while manipulating the local rotation for looking up. I´m working on a network based spaceship movement according to adjust the spaceship to the mouse position on screen. It always ends up in gimal lock. So now i´m out of ideas how to fix this. The node “combine rotators” was a good hint but i actually don´t know how to get gimbal lock away.
this is my solution now with gimbal lock.

Try this, I fixed gimbal lock on the flying pawn template

Here’s my 6 dof, used from various tuts etc. Imgur: The magic of the Internet. Attach your camera to a socket. I don’t use camera’s for any movement. It’s silly.

So, does anybody know the answer to this in blueprints? i am at wits end, cause nothing seems to work… I have tried what is marked as Answers, and it doesn’t work. I specify that my camera is fixed and that i am using the mouse to rotate the character ( top-down mouse oriented ) . Issue happens during the turn in place, if i move the cursor behind the character ie. opposed to the vector’s forward direction.

You can make a solution in BP, anyone suggesting otherwise is wrong. Here’s a solution I’ve made to add rotations into a rotation. I suggest making this into a BP function that you can you in your project.

Get current forward vector (the vector the object is facing right now, probably x axis or “forward vector”). Get the vector you want to be facing. Make sure both are normalized. Get the cross product of both Top slot is the original vector, bottom is the new facing vector. Then get the dot of them, again top slot is the original vector, the bottom is the new facing vector. Acos to degrees, then make rotator from axis and angle. This will yield a new rotation that you can use to ADD into an existing rotation. Key word here is add. You are adding this into an Existing Rotation.

Here’s an image of my function.

2 Likes

I fixed it with quaternions and posted a link to the tutorial. It involves adding C++ to your project but I literally walk you through it and all you do is copy/paste code. Not sure if you checked it out and it doesn’t work for your set up or not but figured I would at least let you know about it since it is not marked as an “accepted answer” on this question.

It can´t be fixed in BPs right now because the only way to fix this 3D problem is to use the mathematically operation with so called quaternions.
So, i decided to buy this one form the marketplace for little money and use it.
It costs only 20€ and it uses the mentioned quaternions like i said in C++ code.

Thanks a bunch man, this helped !

I’m curious if you could elaborate on how you ‘get the vector you want to be facing’. Can I get a screenshot of that? I’d like to try this out, but can’t figure out how you get that second vector.

Great idea! Would like to see this in the player blueprint with generic input lookup and look right with the AddRelativeRotation. Not forsure abou the From and to in your pic.

Thank you .

Thumper, could you show your code in a BP picture of how you would make it work with the generic camera rotation. Once you go to the top it gimbal locks.

@JohnADaniels I’ve solved my gimbal lock issue with AddRelativeRotation by simply parenting a scene root to it & rotating the scene root instead.

2 Likes

Thank you, worked like a charm!


(I calculated teleportation rotation) Rotator → Converted to quaternions, and then subtracting (quaternion * inversed quaternion) to receive the correct rotation.
In case you have to add rotation, convert both rotations to quat-> and “quat1 + quat2”.
Hope this helps
Stay save and creative