How to fix Gimbal Lock

Hello,

I have a 2.5D sidescroller and an enemy that has a back mounted cannon and said cannon faces the player’s location. Gimbal lock is preventing the cannon from rotating properly. I found a video by @Nebula_Games_Inc and it was great, but it didn’t fix the issue I’m facing. Here’s the cannon code (Called from BeginPlay):

Does anyone have any suggestions? Thanks so much.

1 Like
  1. Why the delay loop? :frowning:
  2. Why not just use the look at node and set the rotation?
  3. The look at rotation node you are using is returning the rotation in world space and you are passing to the rinterp a relative rotation.

Thanks for the quick reply.

So using world rotation or relative rotation didn’t yield different results, so I just left relative rotation in the screen since it was the last thing I tried. haha And the delay is because I call from Begin Play and it gives me an infinite loop error if I don’t add the delay.

I tried every set rotation node I could find and nothing changed the Gimbal Lock.

IMO Gimbal Lock has nothing to do with your issue.

Suggest you simply use Tick for this since you need it to update every frame.

I’ve tried calling from Tick as well and the same thing happens. Here’s a quick video of the behavior.
rotate issue.mkv (1.7 MB)

That should not happend with what I shared above. Did you try this?

Since your example is utilizing all axes it flips the model.
flip.mkv (1.5 MB)

Look at rotation only returns Yaw and Pitch and keeps Up vector always pointing Z+.

If you want you actors to not “flip”, then I suggest you dedicate some time to research into quaternions.

1 Like

I was hoping the part tracking the player wasn’t the issue, but you’re thinking that is the case then?

I’ll see if there is an alternative to this and work on that part. I had a similar issue with something else that I had to figure out a way to get it working from 3D to 2D. Lots of things I’m doing were fine in 3D, but a lot of those rely on certain axes pointing certain world directions. Thanks @pezzott1

In the meanwhile, if anyone has a solution please let me know?

So even in C++, using Quaternions, it gives me the gimbal lock/model flip thing. The internet keeps saying that you have to use FIndLookAtRotation to have something look at an object, but that uses Rotators which are Eulers. I can use Quaternions to simply rotate the object with no issue at all on any or all axes, but as soon as I need it to look at something all hell breaks loose.

No one on the internet, for a decades old game engine, has any idea or solution to a common game mechanic? No one?

It’s just a math problem. :innocent:

As per the C++ you deleted: You can’t simply turn a FRotator to FQuat and expect it to show different results. Quaternions are complicated to say the least, probably the reason why it’s so hard to find info about them.

Think this is what it would look like if the axis don’t flip:


VectorFromAngleDist

If you could be more specific about the range of motion that you need, I’m sure it can be done without complicating things further. You only want the actor to pitch; no roll nor yaw?

3 Likes

I had a similar problem, i found a trick, maybe work for u.
Attach the object to the player, keep world or snap whatever you want. Attaching to player make the rotation works fine, maybe in that case you need attach to camera ?

The problem is the movement will work with player not enemy…

My silly opinion, this isn’t caused by gimbal lock, it is caused because -180 to 180 degree rotation is a thing. When you get the find look at rotation it doesn’t understand the real difference between -180 degrees and 20 degrees. It thinks it is a difference of 200 degrees, which it is if you go the opposite way, but the other way is only 20.

You want to get the inverse rotation of the look at rotation and feed that into whichever shananigans you want to accomplish. Here is an example of working code that utilizes inverse rotation to bypass the issue.

Edit; A better explanation as to why it fixes it…

What you are doing now is asking… “My rotation is at 50 degrees currently, what should I rotate to if I want to look at the object?”

What you need to ask is, “Let’s pretend that my rotation is zero, what should I rotate to?” This way the answer is never to try and rotate from -160 degrees to 20 degrees.

Thanks for the reply, but that didn’t work either. I think I’m going to move to a different mechanic and do a bunch of C++ tutorials so I can learn the keywords for Unreal’s script. I know C++ and I know how to do this in Unity’s C#, so I just need to learn more Unreal’s C++ and more about how Quaternions get implemented into Unreal specifically and should be able to get this figured out. Using rotations in Blueprints just isn’t as efficient and clear as using it in C++ thus far, so that’s probably my best bet.

My apologies for getting frustrated. Learning Unreal and knowing how to do this in Unity, but having to go about it differently is frustrating, so my apologies for being rude.

1 Like

Oh my god, you beautiful human being. Works beautifully.

I still need to get more acquainted with quaternions regardless and using these things in C++. I figure working in Blueprints will be easier to get familiar with keywords since Visual Studio isn’t too friendly with Unreal and intelisense. haha

Again, I apologize for the previous rude comment. Learning curve is steep and I’m getting old I think. haha 10+ years with Unity and now starting over again is a humbling but frustrating experience.

Thanks again for the help.

1 Like