Reverse engineer quaternions (SOLVED)

I’m using Defold Gyro - The gyroscope extension to get the angle of the Phone, but it is in a quaternion.

I use this script to turn it in to degrees, but I want to just have two cordinates. The Up and side
rotations. Right now the z roation is the side rotation (like a compass), but I can’t combine the y and x to get the hight (How much you look up) in any way plz help. (I use http://quaternions.online/)

local quat = vmath.quat(quatx, quaty, quatz, quatw)
local rot = quat	
self.yrot = math.atan(rot.y/rot.w)*2
self.xrot = math.atan(rot.x/rot.w)*2

self.zrot = math.atan(rot.z/rot.w)*2

-- math.deg(self.zrot)    For degrees
if gyro then
	gyro.start(function(self, quatx, quaty, quatz, quatw)
	local quat = vmath.quat(quatx, quaty, quatz, quatw)
	
	local rot = quat
	
	self.yrot = math.atan(rot.y/rot.w)*2
	self.xrot = math.atan(rot.x/rot.w)*2
	self.zrot = math.atan(rot.z/rot.w)*2
	
	local cosO = math.cos(self.xrot)
	local cos0 = math.cos(self.yrot)
	local cosY = math.cos(self.zrot)
	
	local sinO = math.sin(self.xrot)
	local sin0 = math.sin(self.yrot)
	local sinY = math.sin(self.zrot)
	
	local v1 = cos0*cosY
	local v2 = sinO*sin0*cosY + cosO*sinY
	local v3 = -cosO*sin0*cosY + sinO*sinY
	
	self.wrot = math.atan2(v3, v2) -- The Rotation I was looking for
end)
2 Likes

Excellent that you got it working yourself! Quaternions are hard to understand at first but they are a powerful way of expressing rotations in 3D space.

2 Likes