public void ApplyEuler(Euler euler) { var quaternion = Quaternion.From(euler); Apply(quaternion); }
public static Quaternion From(Euler euler) { // http://www.mathworks.com/matlabcentral/fileexchange/ // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/ // content/SpinCalc.m float x, y, z, w; var c1 = Mathf.Cos(euler.x / 2); var c2 = Mathf.Cos(euler.y / 2); var c3 = Mathf.Cos(euler.z / 2); var s1 = Mathf.Sin(euler.x / 2); var s2 = Mathf.Sin(euler.y / 2); var s3 = Mathf.Sin(euler.z / 2); if (euler.order == Euler.OrderMode.XYZ) { x = s1 * c2 * c3 + c1 * s2 * s3; y = c1 * s2 * c3 - s1 * c2 * s3; z = c1 * c2 * s3 + s1 * s2 * c3; w = c1 * c2 * c3 - s1 * s2 * s3; } else if (euler.order == Euler.OrderMode.YXZ) { x = s1 * c2 * c3 + c1 * s2 * s3; y = c1 * s2 * c3 - s1 * c2 * s3; z = c1 * c2 * s3 - s1 * s2 * c3; w = c1 * c2 * c3 + s1 * s2 * s3; } else if (euler.order == Euler.OrderMode.ZXY) { x = s1 * c2 * c3 - c1 * s2 * s3; y = c1 * s2 * c3 + s1 * c2 * s3; z = c1 * c2 * s3 + s1 * s2 * c3; w = c1 * c2 * c3 - s1 * s2 * s3; } else if (euler.order == Euler.OrderMode.ZYX) { x = s1 * c2 * c3 - c1 * s2 * s3; y = c1 * s2 * c3 + s1 * c2 * s3; z = c1 * c2 * s3 - s1 * s2 * c3; w = c1 * c2 * c3 + s1 * s2 * s3; } else if (euler.order == Euler.OrderMode.YZX) { x = s1 * c2 * c3 + c1 * s2 * s3; y = c1 * s2 * c3 + s1 * c2 * s3; z = c1 * c2 * s3 - s1 * s2 * c3; w = c1 * c2 * c3 - s1 * s2 * s3; } else if (euler.order == Euler.OrderMode.XZY) { x = s1 * c2 * c3 - c1 * s2 * s3; y = c1 * s2 * c3 - s1 * c2 * s3; z = c1 * c2 * s3 + s1 * s2 * c3; w = c1 * c2 * c3 + s1 * s2 * s3; } else { throw new InvalidOperationException(); } //if (!update) this.onChangeCallback(); return(new Quaternion(x, y, z, w)); }
public static Matrix4 MakeRotationFromEuler(Euler euler) { var res = Matrix4.Identity; var te = res.elements; float x = euler.x, y = euler.y, z = euler.z; float a = Mathf.Cos(x), b = Mathf.Sin(x); float c = Mathf.Cos(y), d = Mathf.Sin(y); float e = Mathf.Cos(z), f = Mathf.Sin(z); if (euler.order == Euler.OrderMode.XYZ) { float ae = a * e, af = a * f, be = b * e, bf = b * f; te[0] = c * e; te[4] = -c * f; te[8] = d; te[1] = af + be * d; te[5] = ae - bf * d; te[9] = -b * c; te[2] = bf - ae * d; te[6] = be + af * d; te[10] = a * c; } else if (euler.order == Euler.OrderMode.YXZ) { float ce = c * e, cf = c * f, de = d * e, df = d * f; te[0] = ce + df * b; te[4] = de * b - cf; te[8] = a * d; te[1] = a * f; te[5] = a * e; te[9] = -b; te[2] = cf * b - de; te[6] = df + ce * b; te[10] = a * c; } else if (euler.order == Euler.OrderMode.ZXY) { float ce = c * e, cf = c * f, de = d * e, df = d * f; te[0] = ce - df * b; te[4] = -a * f; te[8] = de + cf * b; te[1] = cf + de * b; te[5] = a * e; te[9] = df - ce * b; te[2] = -a * d; te[6] = b; te[10] = a * c; } else if (euler.order == Euler.OrderMode.ZYX) { float ae = a * e, af = a * f, be = b * e, bf = b * f; te[0] = c * e; te[4] = be * d - af; te[8] = ae * d + bf; te[1] = c * f; te[5] = bf * d + ae; te[9] = af * d - be; te[2] = -d; te[6] = b * c; te[10] = a * c; } else if (euler.order == Euler.OrderMode.YZX) { float ac = a * c, ad = a * d, bc = b * c, bd = b * d; te[0] = c * e; te[4] = bd - ac * f; te[8] = bc * f + ad; te[1] = f; te[5] = a * e; te[9] = -b * e; te[2] = -d * e; te[6] = ad * f + bc; te[10] = ac - bd * f; } else if (euler.order == Euler.OrderMode.XZY) { float ac = a * c, ad = a * d, bc = b * c, bd = b * d; te[0] = c * e; te[4] = -f; te[8] = d * e; te[1] = ac * f + bd; te[5] = a * e; te[9] = ad * f - bc; te[2] = bc * f - ad; te[6] = b * e; te[10] = bd * f + ac; } // last column te[3] = 0; te[7] = 0; te[11] = 0; // bottom row te[12] = 0; te[13] = 0; te[14] = 0; te[15] = 1; return(res); }