Hi !
I know it’s already an older post, but since Google took me here, here’s my solution to fix the weird values.
It’s not perfect, but there is only a couple of specific angles where the Y value is wrong, and it is only a few angles off.
float3x3 WtoL = LWCToFloat(GetPrimitiveData(Parameters.PrimitiveId).WorldToLocal);
float sy = sqrt(WtoL[0][0] * WtoL[0][0] + WtoL[1][0] * WtoL[1][0]);
bool singular = sy < 1e-6; // If true, gimbal lock is likely
float x, y, z;
if (!singular) {
x = -atan2(WtoL[2][1], WtoL[2][2]);
y = -atan2(-WtoL[2][0], sy);
z = atan2(WtoL[1][0], WtoL[0][0]);
} else {
x = -atan2(-WtoL[1][2], WtoL[1][1]);
y = -atan2(-WtoL[2][0], sy);
z = 0;
}
float3 R = float3(x, y, z);
return R < 0 ? (2 * pi - abs(R)) : R;
There is probably a better way to fix this, as I am only barely cognisant when it comes to angular mathematics…