Hello everyone!
I use Euler angles to calculate a boolean value:
public static bool GetIsFlipDirection(Rotation rotation)
{
Quaternion rotationValue = rotation.Value;
var anglesZ = rotationValue.eulerAngles.z;
bool flipY = anglesZ > 90 && anglesZ < 270;
return flipY;
}
Is it a good way to use Quaternion in DOTS? Is it compatible with Burst? I really couldn’t find a way to use Unity.Mathematics to calculate Euler angles.
mr-gmg
March 3, 2020, 1:50pm
2
I used this
public static float3 ToEulerAngles(quaternion q) {
float3 angles;
// roll (x-axis rotation)
double sinr_cosp = 2 * (q.value.w * q.value.x + q.value.y * q.value.z);
double cosr_cosp = 1 - 2 * (q.value.x * q.value.x + q.value.y * q.value.y);
angles.x = (float)math.atan2(sinr_cosp, cosr_cosp);
// pitch (y-axis rotation)
double sinp = 2 * (q.value.w * q.value.y - q.value.z * q.value.x);
if (math.abs(sinp) >= 1)
angles.y = (float)CopySign(math.PI / 2, sinp); // use 90 degrees if out of range
else
angles.y = (float)math.asin(sinp);
// yaw (z-axis rotation)
double siny_cosp = 2 * (q.value.w * q.value.z + q.value.x * q.value.y);
double cosy_cosp = 1 - 2 * (q.value.y * q.value.y + q.value.z * q.value.z);
angles.z = (float)math.atan2(siny_cosp, cosy_cosp);
return angles;
}
private static double CopySign(double a, double b) {
return math.abs(a) * math.sign(b);
}
5 Likes
@mr-gmg Thank you very much.
I checked the performance of your solution.
[BurstCompile]
iterations: 10000000
Quaternion.eulerAngles => ~670ms
ToEulerAngles() => ~260ms
6 Likes