Example #1
0
        /////////////////////////////////////////

        Vector2[] GenerateConvex()
        {
            var radius     = Radius.Value;
            var height     = Height.Value;
            var pointsSide = Math.Max((Edges - 2) / 2, 1) + 1;

            var result = new Vector2[pointsSide * 2];

            for (int n = 0; n < pointsSide; n++)
            {
                double factor = (double)n / (double)(pointsSide - 1);
                var    angle  = factor * Math.PI;

                if (Axis.Value == 0)
                {
                    result[n] = new Vector2(height / 2, 0) + new Vector2(MathEx.Sin(angle), MathEx.Cos(angle)) * radius;
                    result[pointsSide + n] = new Vector2(-height / 2, 0) - new Vector2(MathEx.Sin(angle), MathEx.Cos(angle)) * radius;
                }
                else
                {
                    result[n] = new Vector2(0, height / 2) + new Vector2(MathEx.Cos(angle), MathEx.Sin(angle)) * radius;
                    result[pointsSide + n] = new Vector2(0, -height / 2) - new Vector2(MathEx.Cos(angle), MathEx.Sin(angle)) * radius;
                }
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// Determines whether the given sphere intersects the current instance of <see cref="ConeF"/>.
        /// </summary>
        /// <param name="sphere">The sphere to check.</param>
        /// <returns>True if the given sphere intersects the current instance of <see cref="ConeF"/>; False otherwise.</returns>
        public bool Intersects(SphereF sphere)
        {
            float sinAngle = MathEx.Sin(Angle);
            float cosAngle = MathEx.Cos(Angle);
            float invSin   = 1.0f / sinAngle;
            float cosSqr   = cosAngle * cosAngle;

            Vector3F cmv       = sphere.Origin - Origin;
            Vector3F d         = cmv + (sphere.Radius * invSin) * Axis;
            float    lengthSqr = d.LengthSquared();
            float    e         = Vector3F.Dot(d, Axis);

            if (e > 0 && e * e >= lengthSqr * cosSqr)
            {
                float sinSqr = sinAngle * sinAngle;
                lengthSqr = cmv.LengthSquared();
                e         = -Vector3F.Dot(cmv, Axis);
                if (e > 0.0f && e * e >= lengthSqr * sinSqr)
                {
                    float rSqr = sphere.Radius * sphere.Radius;
                    return(lengthSqr <= rSqr);
                }
                return(true);
            }
            return(false);
        }
        protected override void OnGetLocalPosition(Random random, out Vector3 result)
        {
            var a = random.Next(MathEx.PI * 2);
            var r = MathEx.Sqrt(random.NextFloat()) * Radius;

            result.X = (float)(random.NextFloat() * Height.Value - Height.Value / 2);
            result.Y = r * MathEx.Cos(a);
            result.Z = r * MathEx.Sin(a);
        }
Example #4
0
        /////////////////////////////////////////

        protected internal override IList <Fixture> CreateShape(Body body, Transform shapeTransform, List <Vector2> rigidBodyLocalPoints)
        {
            if (shapeTransform.IsPositionZero && shapeTransform.IsRotationIdentity)
            {
                var radius = Dimensions.Value * shapeTransform.Scale.ToVector2() * 0.5;

                rigidBodyLocalPoints.Add(new Vector2(-radius.X, -radius.Y));
                rigidBodyLocalPoints.Add(new Vector2(radius.X, -radius.Y));
                rigidBodyLocalPoints.Add(new Vector2(-radius.X, radius.Y));
                rigidBodyLocalPoints.Add(new Vector2(radius.X, radius.Y));

                if (Math.Abs(radius.X - radius.Y) < 0.0001 && UseSmoothCircleWhenPossible)
                {
                    return new Fixture[] { body.CreateCircle((float)radius.X, 0, new Microsoft.Xna.Framework.Vector2(0, 0)) }
                }
                ;
                else
                {
                    return new Fixture[] { body.CreateEllipse((float)radius.X, (float)radius.Y, Edges, 0) }
                };
            }
            else
            {
                var radius = Dimensions.Value * 0.5;
                int edges  = Edges.Value;

                var points = new Vector2[edges];
                for (int n = 0; n < edges; n++)
                {
                    float angle = (float)n / (float)edges * MathEx.PI * 2;
                    points[n] = (shapeTransform * new Vector3(MathEx.Cos(angle) * radius.X, MathEx.Sin(angle) * radius.Y, 0)).ToVector2();
                }

                var r = Rectangle.Cleared;
                foreach (var p in points)
                {
                    r.Add(p);
                }
                rigidBodyLocalPoints.Add(r.LeftTop);
                rigidBodyLocalPoints.Add(r.RightTop);
                rigidBodyLocalPoints.Add(r.LeftBottom);
                rigidBodyLocalPoints.Add(r.RightBottom);

                var vertices = new Vertices(points.Length);
                foreach (var p in points)
                {
                    vertices.Add(Physics2DUtility.Convert(p));
                }

                return(new Fixture[] { body.CreatePolygon(vertices, 0) });
            }
        }
Example #5
0
        /// <summary>
        /// Similar to Mat3F.LookAt( direction, Vec3F.ZAxis ) with fix for vertical direction.
        /// </summary>
        /// <param name="direction"></param>
        /// <returns></returns>
        public static void FromDirectionZAxisUp(ref Vector3F direction, out QuaternionF result)
        {
            //SphereDir sphereDir = SphereDir.FromVector( direction );
            //rotation = new Angles( 0, 0, MathFunctions.RadToDeg(
            //   -sphereDir.Horizontal ) ).ToQuat();
            //rotation *= new Angles( 0,
            //   MathFunctions.RadToDeg( sphereDir.Vertical ), 0 ).ToQuat();

            float horizontal;
            float vertical;
            {
                horizontal = MathEx.Atan2(direction.Y, direction.X);
                float dir2Length = MathEx.Sqrt(direction.X * direction.X + direction.Y * direction.Y);
                vertical = MathEx.Atan2(direction.Z, dir2Length);
            }

            //Quat horizRotation;
            float horizRotationZ;
            float horizRotationW;
            {
                float a = -horizontal * .5f;
                horizRotationZ = -MathEx.Sin(a);
                horizRotationW = MathEx.Cos(a);
                //float sz = MathFunctions.Sin( a );
                //float cz = MathFunctions.Cos( a );
                //horizRotation = new Quat( 0, 0, -sz, cz );
            }

            //Quat vertRotation;
            float vertRotationY;
            float vertRotationW;

            {
                float a = vertical * .5f;
                vertRotationY = -MathEx.Sin(a);
                vertRotationW = MathEx.Cos(a);
                //float sy = MathFunctions.Sin( a );
                //float cy = MathFunctions.Cos( a );
                //vertRotation = new Quat( 0, -sy, 0, cy );
            }

            result = new QuaternionF(
                -horizRotationZ * vertRotationY, horizRotationW * vertRotationY,
                horizRotationZ * vertRotationW, horizRotationW * vertRotationW);
            //return horizRotation * vertRotation;
        }
        protected override void OnGetLocalPosition(Random random, out Vector3 result)
        {
            var u        = random.NextFloat();
            var v        = random.NextFloat();
            var theta    = u * 2.0f * MathEx.PI;
            var phi      = MathEx.Acos(2.0f * v - 1.0f);
            var r        = MathEx.Pow(random.NextFloat(), 1.0f / 3.0f);       //var r = Math.Cbrt( random.NextFloat() );
            var sinTheta = MathEx.Sin(theta);
            var cosTheta = MathEx.Cos(theta);
            var sinPhi   = MathEx.Sin(phi);
            var cosPhi   = MathEx.Cos(phi);
            var x        = r * sinPhi * cosTheta;
            var y        = r * sinPhi * sinTheta;
            var z        = r * cosPhi;

            result = new Vector3F(x, y, z) * Radius;
        }
Example #7
0
        /// <summary>
        /// Converts the current instance of <see cref="AnglesF"/> into the equivalent <see cref="QuaternionF"/> structure.
        /// </summary>
        /// <returns>The equivalent <see cref="QuaternionF"/> structure.</returns>
        public QuaternionF ToQuaternion()
        {
            float a;

            a = MathEx.DegreeToRadian(Yaw) * .5f;
            float sz = MathEx.Sin(a);
            float cz = MathEx.Cos(a);

            a = MathEx.DegreeToRadian(Pitch) * .5f;
            float sy = MathEx.Sin(a);
            float cy = MathEx.Cos(a);

            a = MathEx.DegreeToRadian(Roll) * .5f;
            float sx = MathEx.Sin(a);
            float cx = MathEx.Cos(a);

            float sxcy = sx * cy;
            float cxcy = cx * cy;
            float sxsy = sx * sy;
            float cxsy = cx * sy;

            return(new QuaternionF(cxsy * sz - sxcy * cz, -cxsy * cz - sxcy * sz, sxsy * cz - cxcy * sz, cxcy * cz + sxsy * sz));
        }
Example #8
0
 /// <summary>
 /// Calculates the cosine of each component of the specified vector.
 /// </summary>
 /// <param name="v">The specified vector.</param>
 /// <returns>The vector which contains the cosines of the corresponding components in the specified vector.</returns>
 public static Vector2F Cos(Vector2F v)
 {
     return(new Vector2F(MathEx.Cos(v.X), MathEx.Cos(v.Y)));
 }
Example #9
0
 /// <summary>
 /// Calculates the cosine of each component of the specified vector.
 /// </summary>
 /// <param name="v">The specified vector.</param>
 /// <returns>The vector which contains the cosines of the corresponding components in the specified vector.</returns>
 public static Vector4F Cos(Vector4F v)
 {
     return(new Vector4F(MathEx.Cos(v.X), MathEx.Cos(v.Y), MathEx.Cos(v.Z), MathEx.Cos(v.W)));
 }
Example #10
0
        void RenderBackRectangle(ViewportRenderingContext context, Rectangle rectangle, ColorValue color)
        {
            var renderer = context.Owner.CanvasRenderer;

            switch (BackStyle.Value)
            {
            case BackStyleEnum.Rectangle:
                renderer.AddQuad(rectangle, color);
                break;

            case BackStyleEnum.RoundedRectangle:
            {
                var rect2     = rectangle.ToRectangleF();
                var fontSize  = GetFontSizeScreen(context);
                var roundSize = new Vector2F(renderer.AspectRatioInv, 1) * (float)fontSize * 0.4f;

                int steps = 16;

                var list = new List <Vector2F>(steps * 4);

                for (int n = 0; n < steps; n++)
                {
                    var v     = (float)n / (float)(steps - 1);
                    var angle = v * MathEx.PI / 2;
                    list.Add(rect2.LeftTop + new Vector2F(1.0f - MathEx.Cos(angle), 1.0f - MathEx.Sin(angle)) * roundSize);
                }

                for (int n = steps - 1; n >= 0; n--)
                {
                    var v     = (float)n / (float)(steps - 1);
                    var angle = v * MathEx.PI / 2;
                    list.Add(rect2.RightTop + new Vector2F(MathEx.Cos(angle) - 1.0f, 1.0f - MathEx.Sin(angle)) * roundSize);
                }

                for (int n = 0; n < steps; n++)
                {
                    var v     = (float)n / (float)(steps - 1);
                    var angle = v * MathEx.PI / 2;
                    list.Add(rect2.RightBottom + new Vector2F(MathEx.Cos(angle) - 1.0f, MathEx.Sin(angle) - 1.0f) * roundSize);
                }

                for (int n = steps - 1; n >= 0; n--)
                {
                    var v     = (float)n / (float)(steps - 1);
                    var angle = v * MathEx.PI / 2;
                    list.Add(rect2.LeftBottom + new Vector2F(1.0f - MathEx.Cos(angle), MathEx.Sin(angle) - 1.0f) * roundSize);
                }

                var vertices = new List <CanvasRenderer.TriangleVertex>(1 + list.Count);
                var indices  = new List <int>(list.Count * 3);

                vertices.Add(new CanvasRenderer.TriangleVertex(rect2.GetCenter(), color));
                foreach (var v in list)
                {
                    vertices.Add(new CanvasRenderer.TriangleVertex(v, color));
                }

                for (int n = 0; n < list.Count; n++)
                {
                    indices.Add(0);
                    indices.Add(1 + n);
                    indices.Add(1 + (n + 1) % list.Count);
                }

                renderer.AddTriangles(vertices, indices);
            }
            break;
            }
        }
Example #11
0
 /// <summary>
 /// Calculates the cosine of each component of the specified vector.
 /// </summary>
 /// <param name="v">The specified vector.</param>
 /// <returns>The vector which contains the cosines of the corresponding components in the specified vector.</returns>
 public static Vector3F Cos(Vector3F v)
 {
     return(new Vector3F(MathEx.Cos(v.X), MathEx.Cos(v.Y), MathEx.Cos(v.Z)));
 }
Example #12
0
 public void GetVector(out Vector3F result)
 {
     result.X = MathEx.Cos(Vertical) * MathEx.Cos(Horizontal);
     result.Y = MathEx.Cos(Vertical) * MathEx.Sin(Horizontal);
     result.Z = MathEx.Sin(Vertical);
 }