Esempio n. 1
0
    /// <summary>
    /// Creates a matrix for rotating points around the Y-axis.
    /// </summary>
    /// <param name="radians">The amount, in radians, by which to rotate around the Y-axis.</param>
    /// <returns>The rotation matrix.</returns>
    public static FixMatrix4x4 RotateY(Fix64 radians)
    {
        FixMatrix4x4 result;

        Fix64 c = FixMath.Cos(radians);
        Fix64 s = FixMath.Sin(radians);

        // [  c  0 -s  0 ]
        // [  0  1  0  0 ]
        // [  s  0  c  0 ]
        // [  0  0  0  1 ]
        result.M11 = c;
        result.M12 = Fix64.Zero;
        result.M13 = -s;
        result.M14 = Fix64.Zero;
        result.M21 = Fix64.Zero;
        result.M22 = Fix64.One;
        result.M23 = Fix64.Zero;
        result.M24 = Fix64.Zero;
        result.M31 = s;
        result.M32 = Fix64.Zero;
        result.M33 = c;
        result.M34 = Fix64.Zero;
        result.M41 = Fix64.Zero;
        result.M42 = Fix64.Zero;
        result.M43 = Fix64.Zero;
        result.M44 = Fix64.One;

        return(result);
    }
Esempio n. 2
0
    /// <summary>
    /// Creates a matrix for rotating points around the Y-axis, from a center point.
    /// </summary>
    /// <param name="radians">The amount, in radians, by which to rotate around the Y-axis.</param>
    /// <param name="centerPoint">The center point.</param>
    /// <returns>The rotation matrix.</returns>
    public static FixMatrix4x4 RotateY(Fix64 radians, FixVector3 centerPoint)
    {
        FixMatrix4x4 result;

        Fix64 c = FixMath.Cos(radians);
        Fix64 s = FixMath.Sin(radians);

        Fix64 x = centerPoint.x * (Fix64.One - c) - centerPoint.z * s;
        Fix64 z = centerPoint.x * (Fix64.One - c) + centerPoint.x * s;

        // [  c  0 -s  0 ]
        // [  0  1  0  0 ]
        // [  s  0  c  0 ]
        // [  x  0  z  1 ]
        result.M11 = c;
        result.M12 = Fix64.Zero;
        result.M13 = -s;
        result.M14 = Fix64.Zero;
        result.M21 = Fix64.Zero;
        result.M22 = Fix64.One;
        result.M23 = Fix64.Zero;
        result.M24 = Fix64.Zero;
        result.M31 = s;
        result.M32 = Fix64.Zero;
        result.M33 = c;
        result.M34 = Fix64.Zero;
        result.M41 = x;
        result.M42 = Fix64.Zero;
        result.M43 = z;
        result.M44 = Fix64.One;

        return(result);
    }
Esempio n. 3
0
        public FixVec2 Rotate(Fix degree)
        {
            var cos = FixMath.Cos(degree);
            var sin = FixMath.Sin(degree);

            return(new FixVec2(X * cos - Y * sin, X * sin + Y * cos));
        }
Esempio n. 4
0
    /// <summary>
    /// Creates a matrix for rotating points around the X-axis, from a center point.
    /// </summary>
    /// <param name="radians">The amount, in radians, by which to rotate around the X-axis.</param>
    /// <param name="centerPoint">The center point.</param>
    /// <returns>The rotation matrix.</returns>
    public static FixMatrix4x4 RotateX(Fix64 radians, FixVector3 centerPoint)
    {
        FixMatrix4x4 result;

        Fix64 c = FixMath.Cos(radians);
        Fix64 s = FixMath.Sin(radians);

        Fix64 y = centerPoint.y * (Fix64.One - c) + centerPoint.z * s;
        Fix64 z = centerPoint.z * (Fix64.One - c) - centerPoint.y * s;

        // [  1  0  0  0 ]
        // [  0  c  s  0 ]
        // [  0 -s  c  0 ]
        // [  0  y  z  1 ]
        result.M11 = Fix64.One;
        result.M12 = Fix64.Zero;
        result.M13 = Fix64.Zero;
        result.M14 = Fix64.Zero;
        result.M21 = Fix64.Zero;
        result.M22 = c;
        result.M23 = s;
        result.M24 = Fix64.Zero;
        result.M31 = Fix64.Zero;
        result.M32 = -s;
        result.M33 = c;
        result.M34 = Fix64.Zero;
        result.M41 = Fix64.Zero;
        result.M42 = y;
        result.M43 = z;
        result.M44 = Fix64.One;

        return(result);
    }
Esempio n. 5
0
    /// <summary>
    /// Creates a matrix for rotating points around the Z-axis, from a center point.
    /// </summary>
    /// <param name="radians">The amount, in radians, by which to rotate around the Z-axis.</param>
    /// <param name="centerPoint">The center point.</param>
    /// <returns>The rotation matrix.</returns>
    public static FixMatrix4x4 RotateZ(Fix64 radians, FixVector3 centerPoint)
    {
        FixMatrix4x4 result;

        Fix64 c = FixMath.Cos(radians);
        Fix64 s = FixMath.Sin(radians);

        Fix64 x = centerPoint.x * (1 - c) + centerPoint.y * s;
        Fix64 y = centerPoint.y * (1 - c) - centerPoint.x * s;

        // [  c  s  0  0 ]
        // [ -s  c  0  0 ]
        // [  0  0  1  0 ]
        // [  x  y  0  1 ]
        result.M11 = c;
        result.M12 = s;
        result.M13 = Fix64.Zero;
        result.M14 = Fix64.Zero;
        result.M21 = -s;
        result.M22 = c;
        result.M23 = Fix64.Zero;
        result.M24 = Fix64.Zero;
        result.M31 = Fix64.Zero;
        result.M32 = Fix64.Zero;
        result.M33 = Fix64.One;
        result.M34 = Fix64.Zero;
        result.M41 = Fix64.Zero;
        result.M42 = Fix64.Zero;
        result.M43 = Fix64.Zero;
        result.M44 = Fix64.One;

        return(result);
    }
Esempio n. 6
0
        public void CosWorks(double value)
        {
            var expected = Math.Cos(value * Math.PI / 180.0);

            var result = (double)FixMath.Cos((Fix)value);

            Assert.AreEqual(expected, result, (double)Fix.Epsilon);
        }
        public static FixTrans2 MakeRotation(Fix radians)
        {
            var cos = FixMath.Cos(radians);
            var sin = FixMath.Sin(radians);

            return(new FixTrans2(
                       cos, -sin, 0,
                       sin, cos, 0
                       ));
        }
Esempio n. 8
0
        public void Set(Fix degrees)
        {
            Fix c = FixMath.Cos(degrees);
            Fix s = FixMath.Sin(degrees);

            m00 = c;
            m01 = -s;
            m10 = s;
            m11 = c;
        }
Esempio n. 9
0
        public static FixTrans3 MakeRotationX(Fix degrees)
        {
            var cos = FixMath.Cos(degrees);
            var sin = FixMath.Sin(degrees);

            return(new FixTrans3(
                       1, 0, 0, 0,
                       0, cos, -sin, 0,
                       0, sin, cos, 0
                       ));
        }
Esempio n. 10
0
 /// <summary>
 /// Constructs a quaternion from a vector3.
 /// https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
 /// </summary>
 /// <param name="vector">The vector3 being converted.</param>
 public FixQuaternion(FixVec3 vector)
 {
     w = FixMath.Cos(vector.z / 2) * FixMath.Cos(vector.y / 2) * FixMath.Cos(vector.x / 2)
         + FixMath.Sin(vector.z / 2) * FixMath.Sin(vector.y / 2) * FixMath.Sin(vector.x / 2);
     x = FixMath.Cos(vector.z / 2) * FixMath.Cos(vector.y / 2) * FixMath.Sin(vector.x / 2)
         - FixMath.Sin(vector.z / 2) * FixMath.Sin(vector.y / 2) * FixMath.Cos(vector.x / 2);
     y = FixMath.Sin(vector.z / 2) * FixMath.Cos(vector.y / 2) * FixMath.Sin(vector.x / 2)
         + FixMath.Cos(vector.z / 2) * FixMath.Sin(vector.y / 2) * FixMath.Cos(vector.x / 2);
     z = FixMath.Sin(vector.z / 2) * FixMath.Cos(vector.y / 2) * FixMath.Cos(vector.x / 2)
         - FixMath.Cos(vector.z / 2) * FixMath.Sin(vector.y / 2) * FixMath.Sin(vector.x / 2);
 }
        public FixTrans2(FixVec2 position, FixVec2 scale, Fix rotation)
        {
            var cos = FixMath.Cos(rotation);
            var sin = FixMath.Sin(rotation);

            _m11 = cos * scale.X;
            _m12 = -sin * scale.X;
            _m13 = position.X;
            _m21 = sin * scale.Y;
            _m22 = cos * scale.Y;
            _m23 = position.Y;
        }
Esempio n. 12
0
    /// <summary>
    /// Creates a matrix which rotates around the given axis by the given angle.
    /// </summary>
    /// <param name="axis">The axis.</param>
    /// <param name="angle">The angle.</param>
    /// <param name="result">The resulting rotation matrix</param>
    public static void AxisAngle(ref FixVector3 axis, Fix64 angle, out FixMatrix4x4 result)
    {
        // a: angle
        // x, y, z: unit vector for axis.
        //
        // Rotation matrix M can compute by using below equation.
        //
        //        T               T
        //  M = uu + (cos a)( I-uu ) + (sin a)S
        //
        // Where:
        //
        //  u = ( x, y, z )
        //
        //      [  0 -z  y ]
        //  S = [  z  0 -x ]
        //      [ -y  x  0 ]
        //
        //      [ 1 0 0 ]
        //  I = [ 0 1 0 ]
        //      [ 0 0 1 ]
        //
        //
        //     [  xx+cosa*(1-xx)   yx-cosa*yx-sina*z zx-cosa*xz+sina*y ]
        // M = [ xy-cosa*yx+sina*z    yy+cosa(1-yy)  yz-cosa*yz-sina*x ]
        //     [ zx-cosa*zx-sina*y zy-cosa*zy+sina*x   zz+cosa*(1-zz)  ]
        //
        Fix64 x = axis.x, y = axis.y, z = axis.z;
        Fix64 sa = FixMath.Sin(angle), ca = FixMath.Cos(angle);
        Fix64 xx = x * x, yy = y * y, zz = z * z;
        Fix64 xy = x * y, xz = x * z, yz = y * z;

        result.M11 = xx + ca * (Fix64.One - xx);
        result.M12 = xy - ca * xy + sa * z;
        result.M13 = xz - ca * xz - sa * y;
        result.M14 = Fix64.Zero;
        result.M21 = xy - ca * xy - sa * z;
        result.M22 = yy + ca * (Fix64.One - yy);
        result.M23 = yz - ca * yz + sa * x;
        result.M24 = Fix64.Zero;
        result.M31 = xz - ca * xz + sa * y;
        result.M32 = yz - ca * yz - sa * x;
        result.M33 = zz + ca * (Fix64.One - zz);
        result.M34 = Fix64.Zero;
        result.M41 = Fix64.Zero;
        result.M42 = Fix64.Zero;
        result.M43 = Fix64.Zero;
        result.M44 = Fix64.One;
    }
Esempio n. 13
0
        public void CosAverageDeviationTest()
        {
            var totalDiff = 0.0;
            var maxDiff   = double.MinValue;

            for (var i = 0; i < 3600; i++)
            {
                var fixAngle    = FixMath.Pi * i / 1800;
                var doubleAngle = Math.PI * i / 1800.0;

                var diff = (double)FixMath.Cos(fixAngle) - Math.Cos(doubleAngle);

                totalDiff += diff;
                maxDiff    = Math.Max(maxDiff, Math.Abs(diff));
            }
            // Max & Average deviation
            FixAssert.AssertEquals(maxDiff, 0, 0.019f);
            FixAssert.AssertEquals(totalDiff / 360, 0, 0.007f);
        }
Esempio n. 14
0
 public static DynValue cos(ScriptExecutionContext executionContext, CallbackArguments args)
 {
     return(exec1(args, "cos", (x) => FixMath.Cos(x * 360)));
 }