Beispiel #1
0
        private void CreateBorder(float width, float height, float borderWidth)
        {
            width  = Math.Abs(width);
            height = Math.Abs(height);

            _anchor = new Body(_world);
            List <Vertices> borders = new List <Vertices>(4);

            //Bottom
            borders.Add(PolygonTools.CreateRectangle(width, borderWidth, new Vector2(0, height), 0));

            //Left
            borders.Add(PolygonTools.CreateRectangle(borderWidth, height, new Vector2(-width, 0), 0));

            //Top
            borders.Add(PolygonTools.CreateRectangle(width, borderWidth, new Vector2(0, -height), 0));

            //Right
            borders.Add(PolygonTools.CreateRectangle(borderWidth, height, new Vector2(width, 0), 0));

            DebugMaterial material = new DebugMaterial(MaterialType.Pavement)
            {
                Color = Color.LightGray,
                Scale = 8f
            };
            List <Fixture> fixtures = FixtureFactory.CreateCompoundPolygon(borders, 1, _anchor, material);

            foreach (Fixture t in fixtures)
            {
                t.CollisionFilter.CollisionCategories = Category.All;
                t.CollisionFilter.CollidesWith        = Category.All;
            }
        }
Beispiel #2
0
        public Fixture(Body body, Shape shape, DebugMaterial userData)
        {
            if (Settings.UseFPECollisionCategories)
            {
                _collisionCategories = Category.All;
            }
            else
            {
                _collisionCategories = Category.Cat1;
            }

            _collidesWith   = Category.All;
            _collisionGroup = 0;

            //Fixture defaults
            Friction    = 0.2f;
            Restitution = 0;

            IsSensor = false;

            Body     = body;
            UserData = userData;

            if (Settings.ConserveMemory)
            {
                Shape = shape;
            }
            else
            {
                Shape = shape.Clone();
            }

            RegisterFixture();
        }
Beispiel #3
0
        public static Body CreateBody(World world, Vector2 position, DebugMaterial userData)
        {
            Body body = CreateBody(world, userData);

            body.Position = position;
            return(body);
        }
Beispiel #4
0
        public override void LoadContent()
        {
            World = new World(Vector2.Zero);
            base.LoadContent();

            Vertices rect1 = PolygonTools.CreateRectangle(2, 2);
            Vertices rect2 = PolygonTools.CreateRectangle(2, 2);

            Vector2 trans = new Vector2(-2, 0);

            rect1.Translate(ref trans);
            trans = new Vector2(2, 0);
            rect2.Translate(ref trans);

            List <Vertices> vertices = new List <Vertices>(2);

            vertices.Add(rect1);
            vertices.Add(rect2);

            DebugMaterial material = new DebugMaterial(MaterialType.Circles)
            {
                Color = Color.Gold,
                Scale = 2.5f
            };

            _rectangles = FixtureFactory.CreateCompoundPolygon(World, vertices, 1, material);
            _rectangles[0].Body.BodyType = BodyType.Dynamic;
        }
        private void Decompose()
        {
            //Unsubsribe from the PostSolve delegate
            _world.ContactManager.PostSolve -= PostSolve;

            for (int i = 0; i < Parts.Count; i++)
            {
                Fixture fixture = Parts[i];

                Shape shape = fixture.Shape.Clone();

                DebugMaterial userdata = fixture.UserData;
                MainBody.DestroyFixture(fixture);

                Body body = BodyFactory.CreateBody(_world);
                body.BodyType = BodyType.Dynamic;
                body.Position = MainBody.Position;
                body.Rotation = MainBody.Rotation;
                body.UserData = MainBody.UserData;

                body.CreateFixture(shape, userdata);

                body.AngularVelocity = _angularVelocitiesCache[i];
                body.LinearVelocity  = _velocitiesCache[i];
            }

            _world.RemoveBody(MainBody);
            _world.RemoveBreakableBody(this);
        }
Beispiel #6
0
 public static Body CreateRoundedRectangle(World world, float width, float height, float xRadius,
                                           float yRadius,
                                           int segments, float density, DebugMaterial userData)
 {
     return(CreateRoundedRectangle(world, width, height, xRadius, yRadius, segments, density, Vector2.Zero,
                                   userData));
 }
        public static void Init()
        {
            editingTexture = new DebugMaterial(MaterialType.Stars)
            {
                Color = Microsoft.Xna.Framework.Color.Red,
                Scale = 8f
            };

            selectTexture = new DebugMaterial(MaterialType.Stars)
            {
                Color = Microsoft.Xna.Framework.Color.Yellow,
                Scale = 8f
            };

            objectiveTexture = new DebugMaterial(MaterialType.Stars)
            {
                Color = Color.OrangeRed,
                Scale = 4f
            };

            completedObjectiveTexture = new DebugMaterial(MaterialType.Stars)
            {
                Color = Color.Gray,
                Scale = 4f
            };
            powerupTexture = new DebugMaterial(MaterialType.Stars)
            {
                Color = new Color(.8f, 0, .8f),
                Scale = 4f
            };
        }
Beispiel #8
0
        public Turret(Vector2 farseerLoc, World w, RagdollManager r, Fixture f)
        {
            DebugMaterial gray = new DebugMaterial(MaterialType.Blank)
            {
                Color = Color.DarkGray
            };

            body  = new Body(w);
            pivot = FixtureFactory.AttachCircle(.9f, 1, body, gray);
            FixtureFactory.AttachRectangle(barrelLength, .5f, 1, new Vector2(barrelLength / 2, 0), body, gray);
            body.Position = farseerLoc;
            body.BodyType = BodyType.Dynamic;
            //b.CollidesWith = Category.None;

            if (f == null)
            {
                motor = JointFactory.CreateFixedRevoluteJoint(w, body, Vector2.Zero, farseerLoc);
            }
            else
            {
                motor = new RevoluteJoint(body, f.Body, Vector2.Zero, f.Body.GetLocalPoint(farseerLoc));
                w.AddJoint(motor);
            }

            motor.MotorEnabled   = true;
            motor.MaxMotorTorque = 5000;

            Init(w, r);
        }
Beispiel #9
0
        public static Body CreateEdge(World world, Vector2 start, Vector2 end, DebugMaterial userData)
        {
            Body body = CreateBody(world);

            FixtureFactory.AttachEdge(start, end, body, userData);
            return(body);
        }
Beispiel #10
0
        public override void LoadContent()
        {
            World = new World(new Vector2(0, -20));
            base.LoadContent();

            DebugMaterial material = new DebugMaterial(MaterialType.Waves)
            {
                Color = Color.OliveDrab,
                Scale = 4f
            };

            Fixture _temp;

            _temp = FixtureFactory.CreateEdge(World, new Vector2(-20f, 17f), new Vector2(10f, 8f));
            _temp = FixtureFactory.CreateEdge(World, new Vector2(13.5f, 11f), new Vector2(13.5f, 7f));

            _temp = FixtureFactory.CreateEdge(World, new Vector2(-10f, -4f), new Vector2(20f, 4f));
            _temp = FixtureFactory.CreateEdge(World, new Vector2(-13.5f, -1f), new Vector2(-13.5f, -5f));

            _temp = FixtureFactory.CreateEdge(World, new Vector2(-20f, -8f), new Vector2(10f, -17f));

            float[] friction = new[] { 0.75f, 0.5f, 0.35f, 0.1f, 0.0f };
            for (int i = 0; i < 5; ++i)
            {
                _temp = FixtureFactory.CreateRectangle(World, 2.5f, 2.5f, 1f, material);
                _temp.Body.BodyType = BodyType.Dynamic;
                _temp.Body.Position = new Vector2(-18f + 5.2f * i, 20.0f);
                _temp.Friction      = friction[i];
            }
        }
Beispiel #11
0
        private void Bubble()
        {
            if (bubbled)
            {
                return;
            }

            //foreach (Body b in ragdoll.AllBodies) {
            //    b.AngularVelocity = 0;
            //    b.LinearVelocity = Vector2.Zero;
            //}

            DebugMaterial mat = new DebugMaterial(MaterialType.Blank);

            mat.Color = Color.Transparent;

            bubble = new Body(world, mat);
            FixtureFactory.AttachCircle(6, .5f, bubble, mat);
            bubble.CollidesWith        = Category.Cat2;
            bubble.CollisionCategories = Category.Cat3;
            bubble.Position            = ragdoll.Body.Position;
            bubble.BodyType            = BodyType.Dynamic;
            bubble.IgnoreGravity       = true;

            joint = new RevoluteJoint(ragdoll.Body, bubble, Vector2.Zero, Vector2.Zero);
            world.AddJoint(joint);
            bubbled = true;
        }
Beispiel #12
0
        private void LoadObstacles()
        {
            DebugMaterial material = new DebugMaterial(MaterialType.Dots)
            {
                Color = Color.SandyBrown,
                Scale = 8f
            };

            for (int i = 0; i < 5; i++)
            {
                _obstacles[i] = FixtureFactory.CreateRectangle(World, 8, 1.5f, 1, material);
                _obstacles[i].Body.IsStatic = true;

                if (i == 0)
                {
                    _obstacles[i].Restitution = .2f;
                    _obstacles[i].Friction    = .2f;
                }
            }

            _obstacles[0].Body.Position = new Vector2(-5, -15);
            _obstacles[1].Body.Position = new Vector2(15, -10);
            _obstacles[2].Body.Position = new Vector2(10, 5);
            _obstacles[3].Body.Position = new Vector2(-10, 15);
            _obstacles[4].Body.Position = new Vector2(-17, 0);
        }
Beispiel #13
0
        public static Body CreateLoopShape(World world, Vertices vertices, Vector2 position,
                                           DebugMaterial userData)
        {
            Body body = CreateBody(world, position);

            FixtureFactory.AttachLoopShape(vertices, body, userData);
            return(body);
        }
Beispiel #14
0
        public static Body CreatePolygon(World world, Vertices vertices, float density, Vector2 position,
                                         DebugMaterial userData)
        {
            Body body = CreateBody(world, position);

            FixtureFactory.AttachPolygon(vertices, density, body, userData);
            return(body);
        }
Beispiel #15
0
        public static Body CreateEllipse(World world, float xRadius, float yRadius, int edges, float density,
                                         Vector2 position, DebugMaterial userData)
        {
            Body body = CreateBody(world, position);

            FixtureFactory.AttachEllipse(xRadius, yRadius, edges, density, body, userData);
            return(body);
        }
        private static void SetTemporaryTexture(Fixture f, DebugMaterial texture)
        {
            if (!materialBank.ContainsKey(f))
            {
                materialBank.Add(f, f.UserData);
            }

            f.UserData = texture;
        }
Beispiel #17
0
        public static Body CreateCompoundPolygon(World world, List <Vertices> list, float density,
                                                 Vector2 position, DebugMaterial userData)
        {
            //We create a single body
            Body polygonBody = CreateBody(world, position);

            FixtureFactory.AttachCompoundPolygon(list, density, polygonBody, userData);
            return(polygonBody);
        }
Beispiel #18
0
        /// <summary>
        /// Duplicates the given Body along the given path for approximatly the given copies.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="path">The path.</param>
        /// <param name="shape">The shape.</param>
        /// <param name="type">The type.</param>
        /// <param name="copies">The copies.</param>
        /// <param name="userData">The user data.</param>
        /// <returns></returns>
        public static List <Body> EvenlyDistributeShapesAlongPath(World world, Path path, Shape shape, BodyType type,
                                                                  int copies, DebugMaterial userData)
        {
            List <Shape> shapes = new List <Shape>(1);

            shapes.Add(shape);

            return(EvenlyDistributeShapesAlongPath(world, path, shapes, type, copies, userData));
        }
        public static Fixture AttachRectangle(float width, float height, float density, Vector2 offset, Body body,
                                              DebugMaterial userData)
        {
            Vertices rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2);

            rectangleVertices.Translate(ref offset);
            PolygonShape rectangleShape = new PolygonShape(rectangleVertices, density);

            return(body.CreateFixture(rectangleShape, userData));
        }
        public static Fixture AttachCircle(float radius, float density, Body body, DebugMaterial userData)
        {
            if (radius <= 0)
            {
                throw new ArgumentOutOfRangeException("radius", "Radius must be more than 0 meters");
            }

            CircleShape circleShape = new CircleShape(radius, density);

            return(body.CreateFixture(circleShape, userData));
        }
Beispiel #21
0
        /// <summary>
        /// Creates a breakable body. You would want to remove collinear points before using this.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="vertices">The vertices.</param>
        /// <param name="density">The density.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, Vector2 position,
                                                        DebugMaterial userData)
        {
            List <Vertices> triangles = EarclipDecomposer.ConvexPartition(vertices);

            BreakableBody breakableBody = new BreakableBody(triangles, world, density, userData);

            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);

            return(breakableBody);
        }
Beispiel #22
0
        public override void LoadContent()
        {
            World = new World(Vector2.Zero);
            base.LoadContent();

            DebugMaterial material = new DebugMaterial(MaterialType.Circles)
            {
                Color = Color.Gold,
                Scale = 2.5f
            };

            _rectangle = FixtureFactory.CreateRectangle(World, 5, 5, 1, material);
            _rectangle.Body.BodyType = BodyType.Dynamic;
        }
Beispiel #23
0
        public static Body CreateGear(World world, float radius, int numberOfTeeth, float tipPercentage,
                                      float toothHeight, float density, DebugMaterial userData)
        {
            Vertices gearPolygon = PolygonTools.CreateGear(radius, numberOfTeeth, tipPercentage, toothHeight);

            //Gears can in some cases be convex
            if (!gearPolygon.IsConvex())
            {
                //Decompose the gear:
                List <Vertices> list = EarclipDecomposer.ConvexPartition(gearPolygon);

                return(CreateCompoundPolygon(world, list, density, userData));
            }

            return(CreatePolygon(world, gearPolygon, density, userData));
        }
Beispiel #24
0
        /// <summary>
        /// Creates a rounded rectangle.
        /// Note: Automatically decomposes the capsule if it contains too many vertices (controlled by Settings.MaxPolygonVertices)
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="xRadius">The x radius.</param>
        /// <param name="yRadius">The y radius.</param>
        /// <param name="segments">The segments.</param>
        /// <param name="density">The density.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static Body CreateRoundedRectangle(World world, float width, float height, float xRadius,
                                                  float yRadius,
                                                  int segments, float density, Vector2 position,
                                                  DebugMaterial userData)
        {
            Vertices verts = PolygonTools.CreateRoundedRectangle(width, height, xRadius, yRadius, segments);

            //There are too many vertices in the capsule. We decompose it.
            if (verts.Count >= Settings.MaxPolygonVertices)
            {
                List <Vertices> vertList = EarclipDecomposer.ConvexPartition(verts);
                Body            body     = CreateCompoundPolygon(world, vertList, density, userData);
                body.Position = position;
                return(body);
            }

            return(CreatePolygon(world, verts, density));
        }
        public static Fixture AttachEllipse(float xRadius, float yRadius, int edges, float density, Body body,
                                            DebugMaterial 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));
        }
Beispiel #26
0
        private void CreateObstacles()
        {
            DebugMaterial material = new DebugMaterial(MaterialType.Dots)
            {
                Color = Color.SandyBrown,
                Scale = 8f
            };

            Fixture[] rect = new Fixture[4];

            for (int i = 0; i < 4; i++)
            {
                rect[i] = FixtureFactory.CreateRectangle(World, 6, 1.5f, 1, material);
            }
            rect[0].Body.Position = new Vector2(-9, -5);
            rect[1].Body.Position = new Vector2(-8, 7);
            rect[2].Body.Position = new Vector2(9, -7);
            rect[3].Body.Position = new Vector2(7, 5);
        }
Beispiel #27
0
        public WallPopper(Vector2 farseerLoc, World w, RagdollManager r)
        {
            DebugMaterial gray = new DebugMaterial(MaterialType.Blank)
            {
                Color = Color.DarkGray
            };

            body          = new Body(w);
            body.Rotation = -(float)Math.PI / 2;
            Vertices popperShape = new Vertices(new Vector2[] { new Vector2(-1.2f, .8f), new Vector2(-1.2f, -.8f), new Vector2(1.2f, -.4f), new Vector2(1.2f, .4f) });

            FixtureFactory.AttachPolygon(popperShape, 1, body, gray);
            body.Position = farseerLoc;
            body.BodyType = BodyType.Dynamic;
            body.IsBullet = true;
            //b.CollidesWith = Category.None;

            Init(w, r);
        }
Beispiel #28
0
        public Body(World world, DebugMaterial userData)
        {
            JointList   = new List <Joint>();
            FixtureList = new List <Fixture>(32);
            BodyId      = _bodyIdCounter++;

            World    = world;
            UserData = userData;

            FixedRotation   = false;
            IsBullet        = false;
            SleepingAllowed = true;
            Awake           = true;
            BodyType        = BodyType.Static;
            Enabled         = true;

            Xf.R.Set(0);

            world.AddBody(this);
        }
Beispiel #29
0
        public static Body CreateRectangle(World world, float width, float height, float density, Vector2 position,
                                           DebugMaterial userData)
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width", "Width must be more than 0 meters");
            }

            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", "Height must be more than 0 meters");
            }

            Body         newBody           = CreateBody(world, position);
            Vertices     rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2);
            PolygonShape rectangleShape    = new PolygonShape(rectangleVertices, density);

            newBody.CreateFixture(rectangleShape, userData);

            return(newBody);
        }
Beispiel #30
0
        public Agent(World world, Vector2 position)
        {
            _collidesWith        = Category.All;
            _collisionCategories = Category.All;

            _agentBody          = BodyFactory.CreateBody(world, position);
            _agentBody.BodyType = BodyType.Dynamic;

            DebugMaterial matBody = new DebugMaterial(MaterialType.Blank)
            {
                Color = Color.LightGray
            };
            DebugMaterial matHands = new DebugMaterial(MaterialType.Squares)
            {
                Color = Color.DarkOrange,
                Scale = 8f,
                Depth = 0.0f
            };

            //Center
            FixtureFactory.CreateCircle(1, 1, _agentBody, matBody);

            //Left arm
            FixtureFactory.CreateRectangle(3, 0.8f, 1, new Vector2(-2, 0), _agentBody, matBody);
            FixtureFactory.CreateCircle(1, 1, _agentBody, new Vector2(-4, 0), matHands);

            //Right arm
            FixtureFactory.CreateRectangle(3, 0.8f, 1, new Vector2(2, 0), _agentBody, matBody);
            FixtureFactory.CreateCircle(1, 1, _agentBody, new Vector2(4, 0), matHands);

            //Top arm
            FixtureFactory.CreateRectangle(0.8f, 3, 1, new Vector2(0, 2), _agentBody, matBody);
            FixtureFactory.CreateCircle(1, 1, _agentBody, new Vector2(0, 4), matHands);

            //Bottom arm
            FixtureFactory.CreateRectangle(0.8f, 3, 1, new Vector2(0, -2), _agentBody, matBody);
            FixtureFactory.CreateCircle(1, 1, _agentBody, new Vector2(0, -4), matHands);
        }