Beispiel #1
0
        public static SBSQuaternion AngleAxis(float angle, SBSVector3 axis)
        {
            angle *= (SBSMath.ToRadians * 0.5f);
            SBSVector3 a = axis.normalized;
            float      s = SBSMath.Sin(angle),
                       c = SBSMath.Cos(angle);

            return(new SBSQuaternion(a.x * s, a.y * s, a.z * s, c));
        }
Beispiel #2
0
        public void SetAngleAxis(float angle, SBSVector3 axis)
        {
            angle *= (SBSMath.ToRadians * 0.5f);
            SBSVector3 a = axis.normalized;
            float      s = SBSMath.Sin(angle),
                       c = SBSMath.Cos(angle);

            x = a.x * s;
            y = a.y * s;
            z = a.z * s;
            w = c;
        }
Beispiel #3
0
        public static SBSQuaternion Slerp(SBSQuaternion q0, SBSQuaternion q1, float t)
        {
            float w1 = q0.w, x1 = q0.x, y1 = q0.y, z1 = q0.z,
                  w2 = q1.w, x2 = q1.x, y2 = q1.y, z2 = q1.z,
                  dot = w1 * w2 + x1 * x2 + y1 * y2 + z1 * z2;

            if (dot < 0.0f)
            {
                dot = -dot;
                w2  = -w2;
                x2  = -x2;
                y2  = -y2;
                z2  = -z2;
            }

            if (dot < 0.95f)
            {
                float angle = SBSMath.Acos(dot),
                      s     = 1.0f / SBSMath.Sin(angle),
                      s1    = SBSMath.Sin(angle * (1.0f - t)) * s,
                      s2    = SBSMath.Sin(angle * t) * s;

                w1 = w1 * s1 + w2 * s2;
                x1 = x1 * s1 + x2 * s2;
                y1 = y1 * s1 + y2 * s2;
                z1 = z1 * s1 + z2 * s2;
            }
            else
            {
                w1 += t * (w2 - w1);
                x1 += t * (x2 - x1);
                y1 += t * (y2 - y1);
                z1 += t * (z2 - z1);

                float oom = x1 * x1 + y1 * y1 + z1 * z1 + w1 * w1;
                if (oom < SBSMath.SqrEpsilon)
                {
                    return(new SBSQuaternion(0.0f, 0.0f, 0.0f, 1.0f));
                }

                oom = 1.0f / SBSMath.Sqrt(oom);

                x1 *= oom;
                y1 *= oom;
                z1 *= oom;
                w1 *= oom;
            }

            return(new SBSQuaternion(x1, y1, z1, w1));
        }
Beispiel #4
0
        public static void Slerp(SBSQuaternion q0, SBSQuaternion q1, float t, out SBSQuaternion o)
#endif
        {
            float w1 = q0.w, x1 = q0.x, y1 = q0.y, z1 = q0.z,
                  w2 = q1.w, x2 = q1.x, y2 = q1.y, z2 = q1.z,
                  dot = w1 * w2 + x1 * x2 + y1 * y2 + z1 * z2;

            if (dot < 0.0f)
            {
                dot = -dot;
                w2  = -w2;
                x2  = -x2;
                y2  = -y2;
                z2  = -z2;
            }

            if (dot < 0.95f)
            {
                float angle = SBSMath.Acos(dot),
                      s     = 1.0f / SBSMath.Sin(angle),
                      s1    = SBSMath.Sin(angle * (1.0f - t)) * s,
                      s2    = SBSMath.Sin(angle * t) * s;

                o.w = w1 * s1 + w2 * s2;
                o.x = x1 * s1 + x2 * s2;
                o.y = y1 * s1 + y2 * s2;
                o.z = z1 * s1 + z2 * s2;
            }
            else
            {
                o.w = w1 + t * (w2 - w1);
                o.x = x1 + t * (x2 - x1);
                o.y = y1 + t * (y2 - y1);
                o.z = z1 + t * (z2 - z1);

                o.Normalize();
            }
        }