Example #1
0
        /// <summary>
        /// Scale a quaternion
        /// </summary>
        /// <param name="quaternion1">The quaternion to scale.</param>
        /// <param name="scaleFactor">Scale factor.</param>
        /// <returns>The scaled quaternion.</returns>
        #region public static JQuaternion Multiply(JQuaternion quaternion1, FP scaleFactor)
        public static FPQuaternion Multiply(FPQuaternion quaternion1, FP scaleFactor)
        {
            FPQuaternion result;

            FPQuaternion.Multiply(ref quaternion1, scaleFactor, out result);
            return(result);
        }
Example #2
0
        /// <summary>
        /// Multiply two quaternions.
        /// </summary>
        /// <param name="quaternion1">The first quaternion.</param>
        /// <param name="quaternion2">The second quaternion.</param>
        /// <returns>The product of both quaternions.</returns>
        #region public static JQuaternion Multiply(JQuaternion quaternion1, JQuaternion quaternion2)
        public static FPQuaternion Multiply(FPQuaternion quaternion1, FPQuaternion quaternion2)
        {
            FPQuaternion result;

            FPQuaternion.Multiply(ref quaternion1, ref quaternion2, out result);
            return(result);
        }
Example #3
0
        /// <summary>
        /// Creates a quaternion from a matrix.
        /// </summary>
        /// <param name="matrix">A matrix representing an orientation.</param>
        /// <returns>JQuaternion representing an orientation.</returns>
        #region public static JQuaternion CreateFromMatrix(JMatrix matrix)
        public static FPQuaternion CreateFromMatrix(FPMatrix matrix)
        {
            FPQuaternion result;

            FPQuaternion.CreateFromMatrix(ref matrix, out result);
            return(result);
        }
Example #4
0
 /// <summary>
 /// Scale a quaternion
 /// </summary>
 /// <param name="quaternion1">The quaternion to scale.</param>
 /// <param name="scaleFactor">Scale factor.</param>
 /// <param name="result">The scaled quaternion.</param>
 public static void Multiply(ref FPQuaternion quaternion1, FP scaleFactor, out FPQuaternion result)
 {
     result.x = quaternion1.x * scaleFactor;
     result.y = quaternion1.y * scaleFactor;
     result.z = quaternion1.z * scaleFactor;
     result.w = quaternion1.w * scaleFactor;
 }
Example #5
0
        /// <summary>
        /// Multiply two quaternions.
        /// </summary>
        /// <param name="value1">The first quaternion.</param>
        /// <param name="value2">The second quaternion.</param>
        /// <returns>The product of both quaternions.</returns>
        #region public static FP operator *(JQuaternion value1, JQuaternion value2)
        public static FPQuaternion operator *(FPQuaternion value1, FPQuaternion value2)
        {
            FPQuaternion result;

            FPQuaternion.Multiply(ref value1, ref value2, out result);
            return(result);
        }
Example #6
0
        /// <summary>
        /// Quaternions are subtracted.
        /// </summary>
        /// <param name="quaternion1">The first quaternion.</param>
        /// <param name="quaternion2">The second quaternion.</param>
        /// <returns>The difference of both quaternions.</returns>
        #region public static JQuaternion Subtract(JQuaternion quaternion1, JQuaternion quaternion2)
        public static FPQuaternion Subtract(FPQuaternion quaternion1, FPQuaternion quaternion2)
        {
            FPQuaternion result;

            FPQuaternion.Subtract(ref quaternion1, ref quaternion2, out result);
            return(result);
        }
Example #7
0
        /// <summary>
        /// Subtract two quaternions.
        /// </summary>
        /// <param name="value1">The first quaternion.</param>
        /// <param name="value2">The second quaternion.</param>
        /// <returns>The difference of both quaternions.</returns>
        #region public static FP operator -(JQuaternion value1, JQuaternion value2)
        public static FPQuaternion operator -(FPQuaternion value1, FPQuaternion value2)
        {
            FPQuaternion result;

            FPQuaternion.Subtract(ref value1, ref value2, out result);
            return(result);
        }
Example #8
0
        /// <summary>
        /// Add two quaternions.
        /// </summary>
        /// <param name="value1">The first quaternion.</param>
        /// <param name="value2">The second quaternion.</param>
        /// <returns>The sum of both quaternions.</returns>
        #region public static FP operator +(JQuaternion value1, JQuaternion value2)
        public static FPQuaternion operator +(FPQuaternion value1, FPQuaternion value2)
        {
            FPQuaternion result;

            FPQuaternion.Add(ref value1, ref value2, out result);
            return(result);
        }
Example #9
0
 /// <summary>
 /// Quaternions are subtracted.
 /// </summary>
 /// <param name="quaternion1">The first quaternion.</param>
 /// <param name="quaternion2">The second quaternion.</param>
 /// <param name="result">The difference of both quaternions.</param>
 public static void Subtract(ref FPQuaternion quaternion1, ref FPQuaternion quaternion2, out FPQuaternion result)
 {
     result.x = quaternion1.x - quaternion2.x;
     result.y = quaternion1.y - quaternion2.y;
     result.z = quaternion1.z - quaternion2.z;
     result.w = quaternion1.w - quaternion2.w;
 }
 public void SetRotation(FPQuaternion fpRotation)
 {
     this.rotation = fpRotation;
     //  Debug.Log("vvvvvvvvvvvvv:::" + "transform.Rotation::" + transform.rotation + ", newRotation::" + fpRotation.ToQuaternion());
     transform.rotation = fpRotation.ToQuaternion();
     //   Debug.Log("vvvvvvvvvvvvv1111111111111111:::" + "transform.Rotation::" + transform.rotation + ", newRotation::" + fpRotation.ToQuaternion());
 }
Example #11
0
        // Creates a rotation matrix. Note: Assumes unit quaternion
        public static FPMatrix4x4 Rotate(FPQuaternion q)
        {
            // Precalculate coordinate products
            FP x  = q.x * 2.0F;
            FP y  = q.y * 2.0F;
            FP z  = q.z * 2.0F;
            FP xx = q.x * x;
            FP yy = q.y * y;
            FP zz = q.z * z;
            FP xy = q.x * y;
            FP xz = q.x * z;
            FP yz = q.y * z;
            FP wx = q.w * x;
            FP wy = q.w * y;
            FP wz = q.w * z;

            // Calculate 3x3 matrix from orthonormal basis
            FPMatrix4x4 m;

            m.m00 = 1.0f - (yy + zz); m.m10 = xy + wz; m.m20 = xz - wy; m.m30 = 0.0F;
            m.m01 = xy - wz; m.m11 = 1.0f - (xx + zz); m.m21 = yz + wx; m.m31 = 0.0F;
            m.m02 = xz + wy; m.m12 = yz - wx; m.m22 = 1.0f - (xx + yy); m.m32 = 0.0F;
            m.m03 = 0.0F; m.m13 = 0.0F; m.m23 = 0.0F; m.m33 = 1.0F;
            return(m);
        }
Example #12
0
 /// <summary>
 /// Quaternions are added.
 /// </summary>
 /// <param name="quaternion1">The first quaternion.</param>
 /// <param name="quaternion2">The second quaternion.</param>
 /// <param name="result">The sum of both quaternions.</param>
 public static void Add(ref FPQuaternion quaternion1, ref FPQuaternion quaternion2, out FPQuaternion result)
 {
     result.x = quaternion1.x + quaternion2.x;
     result.y = quaternion1.y + quaternion2.y;
     result.z = quaternion1.z + quaternion2.z;
     result.w = quaternion1.w + quaternion2.w;
 }
Example #13
0
        /// <summary>
        /// Quaternions are added.
        /// </summary>
        /// <param name="quaternion1">The first quaternion.</param>
        /// <param name="quaternion2">The second quaternion.</param>
        /// <returns>The sum of both quaternions.</returns>
        #region public static JQuaternion Add(JQuaternion quaternion1, JQuaternion quaternion2)
        public static FPQuaternion Add(FPQuaternion quaternion1, FPQuaternion quaternion2)
        {
            FPQuaternion result;

            FPQuaternion.Add(ref quaternion1, ref quaternion2, out result);
            return(result);
        }
Example #14
0
        private static void InitializeGameObject(GameObject go, FPVector position, FPQuaternion rotation)
        {
            ICollider[] tsColliders = go.GetComponentsInChildren <ICollider>();
            if (tsColliders != null)
            {
                for (int index = 0, length = tsColliders.Length; index < length; index++)
                {
                    PhysicsManager.instance.AddBody(tsColliders[index]);
                }
            }

            FPTransform rootFPTransform = go.GetComponent <FPTransform>();

            if (rootFPTransform != null)
            {
                rootFPTransform.Initialize();

                rootFPTransform.position = position;
                rootFPTransform.rotation = rotation;
            }

            FPTransform[] FPTransforms = go.GetComponentsInChildren <FPTransform>();
            if (FPTransforms != null)
            {
                for (int index = 0, length = FPTransforms.Length; index < length; index++)
                {
                    FPTransform FPTransform = FPTransforms[index];

                    if (FPTransform != rootFPTransform)
                    {
                        FPTransform.Initialize();
                    }
                }
            }

            FPTransform2D rootFPTransform2D = go.GetComponent <FPTransform2D>();

            if (rootFPTransform2D != null)
            {
                rootFPTransform2D.Initialize();

                rootFPTransform2D.position = new FPVector2(position.x, position.y);
                rootFPTransform2D.rotation = rotation.ToQuaternion().eulerAngles.z;
            }

            FPTransform2D[] FPTransforms2D = go.GetComponentsInChildren <FPTransform2D>();
            if (FPTransforms2D != null)
            {
                for (int index = 0, length = FPTransforms2D.Length; index < length; index++)
                {
                    FPTransform2D FPTransform2D = FPTransforms2D[index];

                    if (FPTransform2D != rootFPTransform2D)
                    {
                        FPTransform2D.Initialize();
                    }
                }
            }
        }
 public void ChangeForward(FPVector toForward)
 {
     //  Debug.Log("this.rotation____before____before____before:::" + this.rotation.ToQuaternion() + ","+ this.forward.ToVector() +  " ,::"+ toForward.ToVector());
     this.rotation *= FPQuaternion.FromToRotation(this.forward, toForward);
     //   Debug.Log("this.rotation____late____late___late:::" + this.rotation.ToQuaternion());
     this.UpdateRotation();
     //  this.UpdateForward();
 }
Example #16
0
        public static FPQuaternion LerpUnclamped(FPQuaternion a, FPQuaternion b, FP t)
        {
            FPQuaternion result = FPQuaternion.Multiply(a, (1 - t)) + FPQuaternion.Multiply(b, t);

            result.Normalize();

            return(result);
        }
Example #17
0
        public static FPQuaternion Conjugate(FPQuaternion value)
        {
            FPQuaternion quaternion;

            quaternion.x = -value.x;
            quaternion.y = -value.y;
            quaternion.z = -value.z;
            quaternion.w = value.w;
            return(quaternion);
        }
Example #18
0
        public static FPQuaternion FromToRotation(FPVector fromVector, FPVector toVector)
        {
            FPVector     w = FPVector.Cross(fromVector, toVector);
            FPQuaternion q = new FPQuaternion(w.x, w.y, w.z, FPVector.Dot(fromVector, toVector));

            q.w += FP.Sqrt(fromVector.sqrMagnitude * toVector.sqrMagnitude);
            q.Normalize();

            return(q);
        }
        private void UpdateEditMode()
        {
            if (transform.hasChanged)
            {
                _position = transform.position.ToFPVector();
                _rotation = transform.rotation.ToFPQuaternion();
                _scale    = transform.localScale.ToFPVector();

                _serialized = true;
            }
        }
Example #20
0
        public static FPQuaternion Euler(FP x, FP y, FP z)
        {
            x *= FP.Deg2Rad;
            y *= FP.Deg2Rad;
            z *= FP.Deg2Rad;

            FPQuaternion rotation;

            FPQuaternion.CreateFromYawPitchRoll(y, x, z, out rotation);

            return(rotation);
        }
Example #21
0
        public static FP Angle(FPQuaternion a, FPQuaternion b)
        {
            FPQuaternion aInv = FPQuaternion.Inverse(a);
            FPQuaternion f    = b * aInv;

            FP angle = FP.Acos(f.w) * 2 * FP.Rad2Deg;

            if (angle > 180)
            {
                angle = 360 - angle;
            }

            return(angle);
        }
        /**
         *  @brief Rotates game object based on provided axis, angle of rotation and relative space.
         *
         *  If relative space is SELF then the game object will rotate based on its forward vector.
         **/
        public void Rotate(FPVector axis, FP angle, Space relativeTo)
        {
            FPQuaternion result = FPQuaternion.identity;

            if (relativeTo == Space.Self)
            {
                result = this.rotation * FPQuaternion.AngleAxis(angle, axis);
            }
            else
            {
                result = FPQuaternion.AngleAxis(angle, axis) * this.rotation;
            }

            result.Normalize();
            this.rotation = result;
        }
        /**
         *  @brief Rotates game object based on provided axis angles and relative space.
         *
         *  If relative space is SELF then the game object will rotate based on its forward vector.
         **/
        public void Rotate(FPVector eulerAngles, Space relativeTo)
        {
            FPQuaternion result = FPQuaternion.identity;

            if (relativeTo == Space.Self)
            {
                result = this.rotation * FPQuaternion.Euler(eulerAngles);
            }
            else
            {
                result = FPQuaternion.Euler(eulerAngles) * this.rotation;
            }

            result.Normalize();
            this.rotation = result;
        }
Example #24
0
        public static FPQuaternion Slerp(FPQuaternion from, FPQuaternion to, FP t)
        {
            t = FPMath.Clamp(t, 0, 1);

            FP dot = Dot(from, to);

            if (dot < 0.0f)
            {
                to  = Multiply(to, -1);
                dot = -dot;
            }

            FP halfTheta = FP.Acos(dot);

            return(Multiply(Multiply(from, FP.Sin((1 - t) * halfTheta)) + Multiply(to, FP.Sin(t * halfTheta)), 1 / FP.Sin(halfTheta)));
        }
Example #25
0
        public static void CreateFromYawPitchRoll(FP yaw, FP pitch, FP roll, out FPQuaternion result)
        {
            FP num9 = roll * FP.Half;
            FP num6 = FP.Sin(num9);
            FP num5 = FP.Cos(num9);
            FP num8 = pitch * FP.Half;
            FP num4 = FP.Sin(num8);
            FP num3 = FP.Cos(num8);
            FP num7 = yaw * FP.Half;
            FP num2 = FP.Sin(num7);
            FP num  = FP.Cos(num7);

            result.x = ((num * num4) * num5) + ((num2 * num3) * num6);
            result.y = ((num2 * num3) * num5) - ((num * num4) * num6);
            result.z = ((num * num3) * num6) - ((num2 * num4) * num5);
            result.w = ((num * num3) * num5) + ((num2 * num4) * num6);
        }
Example #26
0
        /// <summary>
        /// Multiply two quaternions.
        /// </summary>
        /// <param name="quaternion1">The first quaternion.</param>
        /// <param name="quaternion2">The second quaternion.</param>
        /// <param name="result">The product of both quaternions.</param>
        public static void Multiply(ref FPQuaternion quaternion1, ref FPQuaternion quaternion2, out FPQuaternion result)
        {
            FP x     = quaternion1.x;
            FP y     = quaternion1.y;
            FP z     = quaternion1.z;
            FP w     = quaternion1.w;
            FP num4  = quaternion2.x;
            FP num3  = quaternion2.y;
            FP num2  = quaternion2.z;
            FP num   = quaternion2.w;
            FP num12 = (y * num2) - (z * num3);
            FP num11 = (z * num4) - (x * num2);
            FP num10 = (x * num3) - (y * num4);
            FP num9  = ((x * num4) + (y * num3)) + (z * num2);

            result.x = ((x * num) + (num4 * w)) + num12;
            result.y = ((y * num) + (num3 * w)) + num11;
            result.z = ((z * num) + (num2 * w)) + num10;
            result.w = (w * num) - num9;
        }
Example #27
0
        /// <summary>
        /// Creates a quaternion from a matrix.
        /// </summary>
        /// <param name="matrix">A matrix representing an orientation.</param>
        /// <param name="result">JQuaternion representing an orientation.</param>
        public static void CreateFromMatrix(ref FPMatrix matrix, out FPQuaternion result)
        {
            FP num8 = (matrix.M11 + matrix.M22) + matrix.M33;

            if (num8 > FP.Zero)
            {
                FP num = FP.Sqrt((num8 + FP.One));
                result.w = num * FP.Half;
                num      = FP.Half / num;
                result.x = (matrix.M23 - matrix.M32) * num;
                result.y = (matrix.M31 - matrix.M13) * num;
                result.z = (matrix.M12 - matrix.M21) * num;
            }
            else if ((matrix.M11 >= matrix.M22) && (matrix.M11 >= matrix.M33))
            {
                FP num7 = FP.Sqrt((((FP.One + matrix.M11) - matrix.M22) - matrix.M33));
                FP num4 = FP.Half / num7;
                result.x = FP.Half * num7;
                result.y = (matrix.M12 + matrix.M21) * num4;
                result.z = (matrix.M13 + matrix.M31) * num4;
                result.w = (matrix.M23 - matrix.M32) * num4;
            }
            else if (matrix.M22 > matrix.M33)
            {
                FP num6 = FP.Sqrt((((FP.One + matrix.M22) - matrix.M11) - matrix.M33));
                FP num3 = FP.Half / num6;
                result.x = (matrix.M21 + matrix.M12) * num3;
                result.y = FP.Half * num6;
                result.z = (matrix.M32 + matrix.M23) * num3;
                result.w = (matrix.M31 - matrix.M13) * num3;
            }
            else
            {
                FP num5 = FP.Sqrt((((FP.One + matrix.M33) - matrix.M11) - matrix.M22));
                FP num2 = FP.Half / num5;
                result.x = (matrix.M31 + matrix.M13) * num2;
                result.y = (matrix.M32 + matrix.M23) * num2;
                result.z = FP.Half * num5;
                result.w = (matrix.M12 - matrix.M21) * num2;
            }
        }
        public static FPQuaternion LerpUnclamped(FPQuaternion a, FPQuaternion b, FP t)
        {
            FPQuaternion tmpQuat;

            // if (dot < 0), q1 and q2 are more than 360 deg apart.
            // The problem is that quaternions are 720deg of freedom.
            // so we - all components when lerping
            if (Dot(a, b) < 0.0F)
            {
                tmpQuat = FPQuaternion.Multiply(b, -1);
            }
            else
            {
                tmpQuat = b;
            }

            FPQuaternion result = FPQuaternion.Multiply(a, (1 - t)) + FPQuaternion.Multiply(tmpQuat, t);

            result.Normalize();

            return(result);
        }
Example #29
0
        public static FPQuaternion RotateTowards(FPQuaternion from, FPQuaternion to, FP maxDegreesDelta)
        {
            FP dot = Dot(from, to);

            if (dot < 0.0f)
            {
                to  = Multiply(to, -1);
                dot = -dot;
            }

            FP halfTheta = FP.Acos(dot);
            FP theta     = halfTheta * 2;

            maxDegreesDelta *= FP.Deg2Rad;

            if (maxDegreesDelta >= theta)
            {
                return(to);
            }

            maxDegreesDelta /= theta;

            return(Multiply(Multiply(from, FP.Sin((1 - maxDegreesDelta) * halfTheta)) + Multiply(to, FP.Sin(maxDegreesDelta * halfTheta)), 1 / FP.Sin(halfTheta)));
        }
Example #30
0
        public void SetFromToRotation(FPVector fromDirection, FPVector toDirection)
        {
            FPQuaternion targetRotation = FPQuaternion.FromToRotation(fromDirection, toDirection);

            this.Set(targetRotation.x, targetRotation.y, targetRotation.z, targetRotation.w);
        }