Пример #1
0
        public static TheQuaternion Slerp(TheQuaternion a, TheQuaternion b, float value)
        {
            TheQuaternion result;
            float         dot = DotProduct(a, b);

            if (dot < 0)
            {
                dot    = -dot;
                result = -b;
            }
            else
            {
                result = b;
            }

            if (dot < 0.95f)
            {
                float angle = Math.Math.Acos(dot);
                return((a * Math.Math.Sin(angle * (1 - value)) + result * Math.Math.Sin(angle * value)) / Math.Math.Sin(angle));
            }
            else
            {
                return(Lerp(a, result, value));
            }
        }
Пример #2
0
        public static TheQuaternion FromAngleAxis(float angle, TheVector3 axis)
        {
            TheVector3    tmp = axis * Math.Math.Sin(angle / 2);
            TheQuaternion ret = new TheQuaternion
            {
                x = tmp.x,
                y = tmp.y,
                z = tmp.z,
                w = Math.Math.Cos(angle / 2)
            };

            return(ret);
        }
Пример #3
0
        public override bool Equals(object other)
        {
            bool ret;

            if (other == null || !(other is TheQuaternion))
            {
                ret = false;
            }
            else
            {
                TheQuaternion quaternion = (TheQuaternion)other;
                ret = (x == quaternion.x && y == quaternion.y && z == quaternion.z && w == quaternion.w);
            }
            return(ret);
        }
Пример #4
0
        public static TheQuaternion Normalize(TheQuaternion quaternion)
        {
            TheQuaternion ret;
            float         magnitude = Magnitude(quaternion);

            if (magnitude > 1E-05f)
            {
                ret = quaternion / magnitude;
            }
            else
            {
                ret = Identity;
            }
            return(ret);
        }
Пример #5
0
        public static void ToAngleAxis(TheQuaternion a, out float angle, out TheVector3 axis)
        {
            angle = Math.Math.Acos(a.w);

            float sin_theta_inv = 1.0f / Math.Math.Sin(angle);

            axis = new TheVector3
            {
                x = a.x * sin_theta_inv,
                y = a.y * sin_theta_inv,
                z = a.z * sin_theta_inv
            };

            angle *= 2;
        }
Пример #6
0
        public static TheQuaternion RotateTowards(TheQuaternion from, TheQuaternion to, float step)
        {
            float         angle = AngleBetween(from, to);
            TheQuaternion result;

            if (angle == 0f)
            {
                result = to;
            }
            else
            {
                float value = Math.Math.Min(1f, step / angle);
                result = Slerp(from, to, value);
            }
            return(result);
        }
Пример #7
0
        public static TheQuaternion FromEulerAngles(TheVector3 angles)
        {
            float cos_z_2 = Math.Math.Cos(0.5f * angles.z);
            float cos_y_2 = Math.Math.Cos(0.5f * angles.y);
            float cos_x_2 = Math.Math.Cos(0.5f * angles.x);

            float sin_z_2 = Math.Math.Sin(0.5f * angles.z);
            float sin_y_2 = Math.Math.Sin(0.5f * angles.y);
            float sin_x_2 = Math.Math.Sin(0.5f * angles.x);

            TheQuaternion ret = new TheQuaternion
            {
                x = cos_z_2 * cos_y_2 * sin_x_2 - sin_z_2 * sin_y_2 * cos_x_2,
                y = cos_z_2 * sin_y_2 * cos_x_2 + sin_z_2 * cos_y_2 * sin_x_2,
                z = sin_z_2 * cos_y_2 * cos_x_2 - cos_z_2 * sin_y_2 * sin_x_2,
                w = cos_z_2 * cos_y_2 * cos_x_2 + sin_z_2 * sin_y_2 * sin_x_2
            };

            return(ret);
        }
Пример #8
0
 public static TheQuaternion Lerp(TheQuaternion a, TheQuaternion b, float value)
 {
     return((a * (1 - value) + b * value).Normalized);
 }
Пример #9
0
        public static float AngleBetween(TheQuaternion a, TheQuaternion b)
        {
            float dot = DotProduct(a, b);

            return(Math.Math.Acos(Math.Math.Min(Math.Math.Abs(dot), 1f)) * 2f * Math.Math.RadToDeg);
        }
Пример #10
0
 public static float DotProduct(TheQuaternion a, TheQuaternion b)
 {
     return(a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w);
 }
Пример #11
0
 public static float Magnitude(TheQuaternion quaternion)
 {
     return(Math.Math.Sqrt(quaternion.x * quaternion.x + quaternion.y * quaternion.y + quaternion.z * quaternion.z + quaternion.w * quaternion.w));
 }