Quaternion reaching Gimbal Lock

I have a problem with the rotation of the objects via sleitnick’s Quaternion Module

Problem: When any axis (X, Y, Z) is equal to 90 degrees, then the other two axes have the same direction of rotation.
If you don’t understand what I’m talking about, then look at this: Gimbal lock - Wikipedia

Video of problem:

Script:

local quatModule = require(game.ReplicatedStorage.Quaternion)

local cube = workspace.CUBE
local cubePos = cube:GetPivot().Position
local axis = {X = 0, Y = 0, Z = 0}

local quat = quatModule.euler(math.rad(axis.X), math.rad(axis.Y), math.rad(axis.Z)) -- Creating Quaternion from Euler Angles
local newCFrame = CFrame.new(cubePos.X, cubePos.Y, cubePos.Z, quat.X, quat.Y, quat.Z, quat.W)

cube:PivotTo(newCFrame)

I’ve already tried:

  1. Change the rotation order (XYZ, ZYX, YXZ, etc.)
  2. Use other Quaternion modules
  3. Converting Quaternion to CFrame (Quaternion:ToCFrame(position))
  4. The solutions I found on the forum did not help me

I am grateful for any suggestion to solve this problem and i use a translator, so my text may be incorrect

2 Likes

As long as you are using euler angles manually adding the angles you will get gimbal lock.

local newCFrame = CFrame.fromOrientation(x+10,0,0) --likely to result in gimbal lock

It is better to go for CFrame multiplication or quaternion multiplication instead.

You can still reconvert the result into euler angles if you need to.

local block = CFrame.fromOrientation(0,addedRotation,0) * cube.CFrame
local x, y, z = block:ToOrientation()
1 Like