Пример #1
0
        private Body CreateUfoBody()
        {
            // Creates a body but with no shape
            // Set it to ignore gravity (this is a UFO after all!). Uses AngularDamping to avoid fast rotation
            var body = new Body(World, bodyType: BodyType.Dynamic)
            {
                IgnoreGravity = true, AngularDamping = 20
            };

            // The body of the ufo consists of an ellipsis and a circle
            var ellipseVertices = PolygonTools.CreateEllipse(ConvertUnits.ToSimUnits(29), ConvertUnits.ToSimUnits(14), 16);

            ellipseVertices.Translate(ConvertUnits.ToSimUnits(0, 9));
            var ellipseShape = new PolygonShape(ellipseVertices, 5f);

            body.CreateFixture(ellipseShape);

            var circleVertices = PolygonTools.CreateCircle(ConvertUnits.ToSimUnits(15), 16);

            circleVertices.Translate(ConvertUnits.ToSimUnits(0, -5));
            var circleShape = new PolygonShape(circleVertices, 5f);

            body.CreateFixture(circleShape);

            return(body);
        }
Пример #2
0
        public DPhysicsBody(Vector2 size, ShapeType shapeType = ShapeType.Unknown, Vertices vertices = null) : base(DScene.current.physicsWorld)
        {
            switch (shapeType)
            {
            case ShapeType.Unknown:
                vertices = PolygonTools.CreateRectangle(size.X / 2, size.Y / 2);
                break;

            case ShapeType.Circle:
                vertices = PolygonTools.CreateEllipse(size.X / 2, size.Y / 2, Settings.MaxPolygonVertices);
                break;

            case ShapeType.Edge:
                throw new NotImplementedException();

            case ShapeType.Polygon:
                throw new NotImplementedException();

            case ShapeType.Chain:
                vertices = PolygonTools.CreateRectangle(size.X / 2, size.Y / 2);
                break;

            case ShapeType.TypeCount:
                throw new NotImplementedException();
            }

            load(vertices, shapeType);
        }
Пример #3
0
 public FSCollisionEllipse SetRadii(float xRadius, float yRadius)
 {
     _xRadius = xRadius;
     _yRadius = yRadius;
     _verts   = PolygonTools.CreateEllipse(_xRadius * FSConvert.DisplayToSim, _yRadius * FSConvert.DisplayToSim, _edgeCount);
     RecreateFixture();
     return(this);
 }
Пример #4
0
        public FSCollisionEllipse SetEdgeCount(int edgeCount)
        {
            Insist.IsFalse(edgeCount > Settings.MaxPolygonVertices, "edgeCount must be less than Settings.maxPolygonVertices");

            _edgeCount = edgeCount;
            _verts     = PolygonTools.CreateEllipse(_xRadius * FSConvert.DisplayToSim, _yRadius * FSConvert.DisplayToSim, _edgeCount);
            RecreateFixture();
            return(this);
        }
Пример #5
0
        public FSCollisionEllipse(float xRadius, float yRadius, int edgeCount)
        {
            Insist.IsFalse(edgeCount > Settings.MaxPolygonVertices, "edgeCount must be less than Settings.maxPolygonVertices");

            _xRadius   = xRadius;
            _yRadius   = yRadius;
            _edgeCount = edgeCount;
            _verts     = PolygonTools.CreateEllipse(_xRadius * FSConvert.DisplayToSim, _yRadius * FSConvert.DisplayToSim, _edgeCount);
        }
Пример #6
0
        public Fixture CreateEllipse(float xRadius, float yRadius, int edges, float density)
        {
            if (xRadius <= 0)
            {
                throw new ArgumentOutOfRangeException("xRadius", "X-radius must be more than 0");
            }

            if (yRadius <= 0)
            {
                throw new ArgumentOutOfRangeException("yRadius", "Y-radius must be more than 0");
            }

            Vertices     ellipseVertices = PolygonTools.CreateEllipse(xRadius, yRadius, edges);
            PolygonShape polygonShape    = new PolygonShape(ellipseVertices, density);

            return(CreateFixture(polygonShape));
        }
Пример #7
0
        public static Fixture AttachEllipse(float xRadius, float yRadius, int edges, float density, Body body, object userData)
        {
            if (xRadius <= 0)
            {
                throw new ArgumentOutOfRangeException("xRadius", "X-radius must be more than 0");
            }

            if (yRadius <= 0)
            {
                throw new ArgumentOutOfRangeException("yRadius", "Y-radius must be more than 0");
            }

            Vertices     ellipseVertices = PolygonTools.CreateEllipse(xRadius, yRadius, edges);
            PolygonShape polygonShape    = new PolygonShape(ellipseVertices, density);

            return(body.CreateFixture(polygonShape, userData));
        }
Пример #8
0
        protected override Body CreatePhysics()
        {
            var newBody = BodyFactory.CreateBody(World, new Vector2());

            newBody.BodyType      = BodyType.Dynamic;
            newBody.FixedRotation = true;
            var rectangleVertices = PolygonTools.CreateEllipse(width / 2, height / 2, 20);
            //var rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2);
            var rectangleShape = new PolygonShape(rectangleVertices, density);
            var fixture        = newBody.CreateFixture(rectangleShape);

            newBody.FixedRotation = true;
            fixture.Friction      = friction;
            fixture.Restitution   = restitution;

            return(newBody);
        }
Пример #9
0
        protected override Body CreatePhysics()
        {
            var newBody = BodyFactory.CreateBody(World, new Vector2());

            if (density != 0)
            {
                newBody.BodyType = BodyType.Dynamic;
            }
            var rectangleVertices = PolygonTools.CreateEllipse(width / 2, height / 2, 20);
            //var rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2);
            var rectangleShape = new PolygonShape(rectangleVertices, density);
            var fixture        = newBody.CreateFixture(rectangleShape);

            newBody.FixedRotation = true;
            fixture.Friction      = friction;
            fixture.Restitution   = restitution;

            SensorFixture          = FixtureFactory.AttachRectangle(sensorWidth, 0.05f, 1f, sensorCenter, newBody, sensorName);
            SensorFixture.IsSensor = true;

            return(newBody);
        }
        public static Fixture CreateEllipse(World world, float xRadius, float yRadius, int edges, float density,
                                            Vector2 position)
        {
            if (xRadius <= 0)
            {
                throw new ArgumentOutOfRangeException("xRadius", "X-radius must be more than 0");
            }

            if (yRadius <= 0)
            {
                throw new ArgumentOutOfRangeException("yRadius", "Y-radius must be more than 0");
            }

            if (density <= 0)
            {
                throw new ArgumentOutOfRangeException("density", "Density must be more than 0");
            }

            Body         body            = BodyFactory.CreateBody(world, position);
            Vertices     ellipseVertices = PolygonTools.CreateEllipse(xRadius, yRadius, edges);
            PolygonShape polygonShape    = new PolygonShape(ellipseVertices);

            return(body.CreateFixture(polygonShape, density));
        }
Пример #11
0
    void Start()
    {
        // Create Vector2 vertices
        Vector2[] vertices2D = null;

        switch (tipo)
        {
        case Tipo.CAPSULE:
            vertices2D = USVG.PolygonTools.CreateCapsule(Height, Radius, Edges);
            break;

        case Tipo.CIRCLE:
            vertices2D = PolygonTools.CreateCircle(Radius, Edges);
            break;

        case Tipo.ELIPSE:
            vertices2D = PolygonTools.CreateEllipse(Radius, yRadius, Edges);
            break;

        case Tipo.GEAR:
            vertices2D = PolygonTools.CreateGear(Radius, NumberOfTheeth, TipPercentage, ToothHeight);
            break;

        case Tipo.ROUNDED_RECTANGLE:
            vertices2D = PolygonTools.CreateRoundedRectangle(Height, Width, Radius, yRadius, Edges);
            break;

        default:
        case Tipo.RECTANGLE:
            vertices2D = PolygonTools.CreateRectangle(Height, Width);
            break;
        }


        // Use the triangulator to get indices for creating triangles
        Triangulator tr = new Triangulator(vertices2D);

        int[] indices = tr.Triangulate();

        // Create the Vector3 vertices
        Vector3[] vertices = new Vector3[vertices2D.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
        }

        // Create the mesh
        Mesh msh = new Mesh();

        msh.vertices  = vertices;
        msh.triangles = indices;
        msh.RecalculateNormals();
        msh.RecalculateBounds();

        // Set up game object with mesh;
        gameObject.AddComponent(typeof(MeshRenderer));
        MeshFilter filter = gameObject.GetComponent <MeshFilter>();

        if (filter == null)
        {
            filter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter;
        }

        filter.mesh = msh;
    }
Пример #12
0
        private void TextureFromDictionary(Dictionary <string, object> shapeParam, out Texture2D texture, out Vertices shapeVertices)
        {
            shapeVertices = null;
            texture       = null;
            ObjectType objectType = (ObjectType)shapeParam[ShapeParametersKeys.ObjectType];

            switch (objectType)
            {
            case ObjectType.Arc:
                shapeVertices = PolygonTools.CreatePreTransformedArt(MathHelper.ToRadians((float)shapeParam[ShapeParametersKeys.ArcDegrees]), (int)shapeParam[ShapeParametersKeys.ArcSides], (float)shapeParam[ShapeParametersKeys.ArcRadius]);
                texture       = ContentService.GetContentService().AssetCreator.TextureFromVertices(shapeVertices, (string)shapeParam[ShapeParametersKeys.Material], (Color)shapeParam[ShapeParametersKeys.Color], (float)shapeParam[ShapeParametersKeys.MaterialScale]);

                //shapeVertices.GetCollisionBox().;
                //Vector2 arcOffset = Vector2.One;
                //shapeVertices.Translate(ref arcOffset);
                break;

            case ObjectType.Capsule:
                shapeVertices = PolygonTools.CreateCapsule((float)shapeParam[ShapeParametersKeys.CapsuleHeight], (float)shapeParam[ShapeParametersKeys.CapsuleBottomRadius], (int)shapeParam[ShapeParametersKeys.CapsuleBottomEdges], (float)shapeParam[ShapeParametersKeys.CapsuleTopRadius], (int)shapeParam[ShapeParametersKeys.CapsuleTopEdges]);
                texture       = ContentService.GetContentService().AssetCreator.TextureFromVertices(shapeVertices, (string)shapeParam[ShapeParametersKeys.Material], (Color)shapeParam[ShapeParametersKeys.Color], (float)shapeParam[ShapeParametersKeys.MaterialScale]);
                break;

            case ObjectType.Gear:
                shapeVertices = PolygonTools.CreateGear((float)shapeParam[ShapeParametersKeys.GearTipPercentage], (int)shapeParam[ShapeParametersKeys.GearNumberOfTeeth], (float)shapeParam[ShapeParametersKeys.GearTipPercentage], (float)shapeParam[ShapeParametersKeys.GearToothHeigt]);
                texture       = ContentService.GetContentService().AssetCreator.TextureFromVertices(shapeVertices, (string)shapeParam[ShapeParametersKeys.Material], (Color)shapeParam[ShapeParametersKeys.Color], (float)shapeParam[ShapeParametersKeys.MaterialScale]);
                break;

            case ObjectType.Rectangle:
                shapeVertices = PolygonTools.CreateRectangle((float)shapeParam[ShapeParametersKeys.RectangleWidth], (float)shapeParam[ShapeParametersKeys.RectangleHeight]);
                texture       = ContentService.GetContentService().AssetCreator.TextureFromVertices(shapeVertices, (string)shapeParam[ShapeParametersKeys.Material], (Color)shapeParam[ShapeParametersKeys.Color], (float)shapeParam[ShapeParametersKeys.MaterialScale]);
                break;

            case ObjectType.RoundedRectangle:
                shapeVertices = PolygonTools.CreateRoundedRectangle((float)shapeParam[ShapeParametersKeys.RoundedRectangleWidth], (float)shapeParam[ShapeParametersKeys.RoundedRectangleHeight], (float)shapeParam[ShapeParametersKeys.RoundedRectangleXRadius], (float)shapeParam[ShapeParametersKeys.RoundedRectangleYRadius], (int)shapeParam[ShapeParametersKeys.RoundedRectangleSegments]);
                texture       = ContentService.GetContentService().AssetCreator.TextureFromVertices(shapeVertices, (string)shapeParam[ShapeParametersKeys.Material], (Color)shapeParam[ShapeParametersKeys.Color], (float)shapeParam[ShapeParametersKeys.MaterialScale]);
                break;

            case ObjectType.Ellipse:
                shapeVertices = PolygonTools.CreateEllipse((float)shapeParam[ShapeParametersKeys.EllipseXRadius], (float)shapeParam[ShapeParametersKeys.EllipseYRadius], (int)shapeParam[ShapeParametersKeys.EllipseNumberOfEdges]);
                texture       = ContentService.GetContentService().AssetCreator.EllipseTexture((float)shapeParam[ShapeParametersKeys.EllipseXRadius], (float)shapeParam[ShapeParametersKeys.EllipseYRadius], (string)shapeParam[ShapeParametersKeys.Material], (Color)shapeParam[ShapeParametersKeys.Color], (float)shapeParam[ShapeParametersKeys.MaterialScale]);
                break;

            case ObjectType.Circle:
                shapeVertices = PolygonTools.CreateCircle((float)shapeParam[ShapeParametersKeys.CircleRadius], (int)shapeParam[ShapeParametersKeys.CircleSegments]);
                texture       = ContentService.GetContentService().AssetCreator.CircleTexture((float)shapeParam[ShapeParametersKeys.CircleRadius], (string)shapeParam[ShapeParametersKeys.Material], (Color)shapeParam[ShapeParametersKeys.Color], (float)shapeParam[ShapeParametersKeys.MaterialScale]);
                break;

            case ObjectType.CustomShape:
                if ((bool)shapeParam[ShapeParametersKeys.CustomObjectUseOriginalTexture])
                {
                    ContentService.GetContentService().AssetCreator.ShapeFromTexture((string)shapeParam[ShapeParametersKeys.CustomObjectShape], (float)shapeParam[ShapeParametersKeys.CustomObjectScale], (Color)shapeParam[ShapeParametersKeys.Color], out texture, out shapeVertices);
                }
                else
                {
                    ContentService.GetContentService().AssetCreator.ShapeFromTexture((string)shapeParam[ShapeParametersKeys.CustomObjectShape], (float)shapeParam[ShapeParametersKeys.CustomObjectScale], (string)shapeParam[ShapeParametersKeys.Material], (Color)shapeParam[ShapeParametersKeys.Color], (float)shapeParam[ShapeParametersKeys.MaterialScale], out texture, out shapeVertices);
                }
                break;

            default:
                throw new Exception("Unknown Shape");
            }
        }