Get the right rotation

Hello,
I’ve been struggling with this issues now for ages and can’t find a solution.
I have an object pointing in the y-axis… when I rotate it around the X axis I want to return an angle between 0 and 360.
Therefore I got this script:

def update(self):
rotX = math.degrees(self.object.localOrientation.to_euler().x)
if not self.object[“flipped”]:
if rotX < 0:
rotX += 360
else:
pass

    print(rotX)

Now if I turn the object around its Z Axis 180° the script doesnt work anymore.
I get completely wrong values.

Can someone help me solving this issue?
I’m attaching an example blend in upbge 0.25.

rotationProblem.blend (512.9 KB)

When you rotate the object with W-Key you get values from 0-360°
All good.
Then, when you rotate the object with SPACE 180° on the Z, the returned values jump from 90 to 270 and are off.

I’m unable to find a fix for that.
I went the dot product route but that didn’t help either.

Hope someone can help me figuring it out.

You mean the solution from discord?
I repost here for others:
"

Here’s a solution where I calculate the angle between the local_z axis to world Z-axis but it is only correct if you don’t have an additional rotation around the y-axis.

"

        vecz = Vector([0,0,1])
        local_z = self.object.worldOrientation.col[2]
        self.object["angle"] = math.degrees(local_z.angle(vecz))
        dot = self.object.worldOrientation.col[1].dot(vecz)
        if dot<0: self.object["angle"] = 360-self.object["angle"]

File for UPBGE 0.2.5:
025_rotationProblem.blend (513.5 KB)

So people can try find other solutions.

yeah, thanks musikai. Your solution almost works but there were still some jumping. So for now, I just lerped to conceal the problem, which is OK.
Also had to use a different formula for when the player is rotated normal.
self.stance is 0 Z rotation and not self.stance is 180° Z rotation

vecz = Vector([0, 0, 1])
local_z = self.physicsObject.worldOrientation.col[2]
self.physicsObject[“angle”] = math.degrees(local_z.angle(vecz))
dot = self.physicsObject.worldOrientation.col[1].dot(vecz)
if self.stance:
self.airBackflipFrame = math.degrees(self.physicsObject.localOrientation.to_euler().x) % 360
else:
if dot > 0: self.physicsObject[“angle”] = Database.lerp(self.airBackflipFrame, 360 - self.physicsObject[“angle”], .1)
self.airBackflipFrame = self.physicsObject[“angle”]

By the way… how do you format the text as code?