Beispiel #1
0
        public Rope(Item item, XElement element)
            : base(item, element)
        {
            string spritePath = element.GetAttributeString("sprite", "");

            if (spritePath == "")
            {
                DebugConsole.ThrowError("Sprite " + spritePath + " in " + element + " not found!");
            }

            float length = ConvertUnits.ToSimUnits(element.GetAttributeFloat("length", 200.0f));

            pullForce = element.GetAttributeFloat("pullforce", 10.0f);

            projectileAnchor   = Vector2.Zero;
            projectileAnchor.X = element.GetAttributeFloat("projectileanchorx", 0.0f);
            projectileAnchor.Y = element.GetAttributeFloat("projectileanchory", 0.0f);
            projectileAnchor   = ConvertUnits.ToSimUnits(projectileAnchor);

            sprite        = new Sprite(spritePath, new Vector2(0.5f, 0.5f));
            sectionLength = ConvertUnits.ToSimUnits(sprite.size.X);

            Path ropePath = new Path();

            ropePath.Add(item.body.SimPosition);
            ropePath.Add(item.body.SimPosition + new Vector2(length, 0.0f));
            ropePath.Closed = false;

            Vertices     box   = PolygonTools.CreateRectangle(sectionLength, 0.05f);
            PolygonShape shape = new PolygonShape(box, 5);

            List <Body> ropeList = PathManager.EvenlyDistributeShapesAlongPath(GameMain.World, ropePath, shape, BodyType.Dynamic, (int)(length / sectionLength));

            ropeBodies = new PhysicsBody[ropeList.Count];
            for (int i = 0; i < ropeBodies.Length; i++)
            {
                ropeList[i].Mass    = 0.01f;
                ropeList[i].Enabled = false;
                //only collide with the map
                ropeList[i].CollisionCategories = Physics.CollisionItem;
                ropeList[i].CollidesWith        = Physics.CollisionWall;

                //ropeBodies[i] = new PhysicsBody(ropeList[i]);
            }

            List <RevoluteJoint> joints = PathManager.AttachBodiesWithRevoluteJoint(GameMain.World, ropeList,
                                                                                    new Vector2(-sectionLength / 2, 0.0f), new Vector2(sectionLength / 2, 0.0f), false, false);

            ropeJoints = new RevoluteJoint[joints.Count + 1];
            //ropeJoints[0] = JointFactory.CreateRevoluteJoint(Game1.world, item.body, ropeList[0], new Vector2(0f, -0.0f));
            for (int i = 0; i < joints.Count; i++)
            {
                var distanceJoint = JointFactory.CreateDistanceJoint(GameMain.World, ropeList[i], ropeList[i + 1]);

                distanceJoint.Length       = sectionLength;
                distanceJoint.DampingRatio = 1.0f;
                ropeJoints[i] = joints[i];
            }
        }
        public override void LoadContent()
        {
            base.LoadContent();

            World.Gravity = new Vector2(0, 9.82f);

            _border = new Border(World, Lines, Framework.GraphicsDevice);

            // Bridge
            // We make a path using 2 points.
            Path bridgePath = new Path();

            bridgePath.Add(new Vector2(-15, 5));
            bridgePath.Add(new Vector2(15, 5));
            bridgePath.Closed = false;

            Vertices     box   = PolygonTools.CreateRectangle(0.125f, 0.5f);
            PolygonShape shape = new PolygonShape(box, 20);

            _bridgeBodies = PathManager.EvenlyDistributeShapesAlongPath(World, bridgePath, shape, BodyType.Dynamic, 29);

            // Attach the first and last fixtures to the world
            Body anchor = new Body(World, Vector2.Zero);

            anchor.BodyType = BodyType.Static;
            World.AddJoint(new RevoluteJoint(_bridgeBodies[0], anchor, _bridgeBodies[0].Position - new Vector2(0.5f, 0f), true));
            World.AddJoint(new RevoluteJoint(_bridgeBodies[_bridgeBodies.Count - 1], anchor, _bridgeBodies[_bridgeBodies.Count - 1].Position + new Vector2(0.5f, 0f), true));

            PathManager.AttachBodiesWithRevoluteJoint(World, _bridgeBodies, new Vector2(0f, -0.5f), new Vector2(0f, 0.5f), false, true);

            // Soft body
            // We make a rectangular path.
            Path rectanglePath = new Path();

            rectanglePath.Add(new Vector2(-6, -11));
            rectanglePath.Add(new Vector2(-6, 1));
            rectanglePath.Add(new Vector2(6, 1));
            rectanglePath.Add(new Vector2(6, -11));
            rectanglePath.Closed = true;

            // Creating two shapes. A circle to form the circle and a rectangle to stabilize the soft body.
            Shape[] shapes = new Shape[2];
            shapes[0] = new PolygonShape(PolygonTools.CreateRectangle(0.5f, 0.5f, new Vector2(-0.1f, 0f), 0f), 1f);
            shapes[1] = new CircleShape(0.5f, 1f);

            // We distribute the shapes in the rectangular path.
            _softBodies = PathManager.EvenlyDistributeShapesAlongPath(World, rectanglePath, shapes, BodyType.Dynamic, 30);

            // Attach the bodies together with revolute joints. The rectangular form will converge to a circular form.
            PathManager.AttachBodiesWithRevoluteJoint(World, _softBodies, new Vector2(0f, -0.5f), new Vector2(0f, 0.5f), true, true);

            // GFX
            _bridgeBox           = new Sprite(ContentWrapper.TextureFromShape(shape, ContentWrapper.Orange, ContentWrapper.Brown));
            _softBodyBox         = new Sprite(ContentWrapper.TextureFromShape(shapes[0], ContentWrapper.Green, ContentWrapper.Black));
            _softBodyBox.Origin += new Vector2(ConvertUnits.ToDisplayUnits(0.1f), 0f);
            _softBodyCircle      = new Sprite(ContentWrapper.TextureFromShape(shapes[1], ContentWrapper.Lime, ContentWrapper.Grey));
        }
Beispiel #3
0
        public Chain(Vector2 pathBeginning, Vector2 pathEnd, Vector2 revolutePosition1, bool useSecondRevolute, Vector2 revolutePosition2, int numberOfChainlinks, Body revoluteBody, bool breakable)
        {
            chainTexture = ScreenManager.Content.Load <Texture2D>("Enemies/kette");
            origin       = new Vector2(chainTexture.Width / 2, chainTexture.Height / 2);

            //Chain start / end
            Path path = new Path();

            path.Add(pathBeginning);
            path.Add(pathEnd);

            //A single chainlink
            PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(0.125f, 0.6f), 20);

            //Use PathFactory to create all the chainlinks based on the chainlink created before.
            chainLinks = PathManager.EvenlyDistributeShapesAlongPath(PhysicsGameScreen.World, path, shape, BodyType.Dynamic, numberOfChainlinks);

            foreach (Body chainLink in chainLinks)
            {
                foreach (Fixture f in chainLink.FixtureList)
                {
                    f.Friction = 0.1f;
                }
            }

            //Fix the first chainlink to a Body
            FixedRevoluteJoint fixedJoint = new FixedRevoluteJoint(chainLinks[0], Vector2.Zero, revolutePosition1);

            PhysicsGameScreen.World.AddJoint(fixedJoint);


            RevoluteJoint fixedJoint2 = new RevoluteJoint(chainLinks[numberOfChainlinks -= 2], revoluteBody, new Vector2(chainLinks[numberOfChainlinks -= 2].LocalCenter.X - 0.125f,
                                                                                                                         chainLinks[numberOfChainlinks -= 2].LocalCenter.Y + 0.6f), revolutePosition2);

            PhysicsGameScreen.World.AddJoint(fixedJoint2);

            //Attach all the chainlinks together with a revolute joint
            List <RevoluteJoint> joints = PathManager.AttachBodiesWithRevoluteJoint(PhysicsGameScreen.World, chainLinks,
                                                                                    new Vector2(0, -0.6f),
                                                                                    new Vector2(0, 0.6f),
                                                                                    false, false);



            if (breakable == true)
            {
                //The chain is breakable
                for (int i = 0; i < joints.Count; i++)
                {
                    RevoluteJoint r = joints[i];

                    r.Breakpoint = 10000f;
                    //if (r.Broke lower bridge
                }
            }
        }
Beispiel #4
0
        public Barrier(Texture2D texture, Unit start, Unit end, World world, Game game, Color color)
            : base(texture, Vector2.Zero, DrawingHelper.DrawingLevel.Low, game, world)
        {
            Color           = color;
            EffectParticles = new ParticleEngine(30, texture, Vector2.Zero, color, Vector2.Zero, 1, 0, 10, DrawingHelper.DrawingLevel.Top, game);
            SyncedGameCollection.ComponentCollection.Add(EffectParticles);

            // Units to follow
            _start = start;
            _end   = end;

            // create two static hiddenBodies that mirror the position of the units
            hiddenBody1 = BodyFactory.CreateCircle(world, texture.Width / 2, 1, _start.RigidBody.Position); //_start.RigidBody;
            hiddenBody2 = BodyFactory.CreateCircle(world, texture.Width / 2, 1, _end.RigidBody.Position);   //_end.RigidBody;
            hiddenBody1.CollisionCategories = Category.None;
            hiddenBody2.CollisionCategories = Category.None;
            hiddenBody1.CollidesWith        = Category.None;
            hiddenBody2.CollidesWith        = Category.None;
            hiddenBody1.BodyType            = BodyType.Static;
            hiddenBody2.BodyType            = BodyType.Static;
            hiddenBody1.Position            = _start.RigidBody.Position;
            hiddenBody2.Position            = _end.RigidBody.Position;
            hiddenBody1.UserData            = hiddenBody2.UserData = "";

            // create path object and set it to the init position.
            Path barrierPath = new Path();

            barrierPath.Add(start.RigidBody.Position);
            barrierPath.Add(end.RigidBody.Position);
            barrierPath.Closed = false;

            // create barrier particle
            Vertices barrierParticle = PolygonTools.CreateCircle(ConvertUnits.ToSimUnits(texture.Width * 4), 8);
            Shape    shape           = new PolygonShape(barrierParticle, 0f);

            // distribute barrierParticle positions along the path between the two units.
            _barrierBodies = PathManager.EvenlyDistributeShapesAlongPath(world, barrierPath, shape, BodyType.Dynamic, 30);

            // fix the shapes together with the end and start point
            JointFactory.CreateRevoluteJoint(world, hiddenBody1, _barrierBodies[0], new Vector2(0f, 0f));
            JointFactory.CreateRevoluteJoint(world, hiddenBody2, _barrierBodies[_barrierBodies.Count - 1], new Vector2(0f, 0f));

            // fix all the barrierParticles together in the path
            PathManager.AttachBodiesWithRevoluteJoint(world, _barrierBodies, new Vector2(0f, 0.5f), new Vector2(0f, 0.5f), false, false);

            // Set up OnCollision fale safe.
            for (int i = 0; i < _barrierBodies.Count; i++)
            {
                _barrierBodies[i].CollisionCategories = Category.Cat10;
                _barrierBodies[i].CollidesWith        = Category.Cat5; /* should only collide with grabbables*/
                _barrierBodies[i].UserData            = "BARRIER";
                _barrierBodies[i].OnCollision        += OnCollision;
            }
            Deactivated();
        }
Beispiel #5
0
        private PathTest()
        {
            //Single body that moves around path
            _movingBody          = BodyFactory.CreateBody(World);
            _movingBody.Position = new Vector2(-25, 25);
            _movingBody.BodyType = BodyType.Dynamic;
            _movingBody.CreateFixture(new PolygonShape(PolygonTools.CreateRectangle(0.5f, 0.5f), 1));

            //Static shape made up of bodies
            _path = new Path();
            _path.Add(new Vector2(0, 20));
            _path.Add(new Vector2(5, 15));
            _path.Add(new Vector2(20, 18));
            _path.Add(new Vector2(15, 1));
            _path.Add(new Vector2(-5, 14));
            _path.Closed = true;

            CircleShape shape = new CircleShape(0.25f, 1);

            PathManager.EvenlyDistributeShapesAlongPath(World, _path, shape, BodyType.Static, 100);

            //Smaller shape that is movable. Created from small rectangles and circles.
            Vector2 xform = new Vector2(0.5f, 0.5f);

            _path.Scale(ref xform);
            xform = new Vector2(5, 5);
            _path.Translate(ref xform);

            List <Shape> shapes = new List <Shape>(2);

            shapes.Add(new PolygonShape(PolygonTools.CreateRectangle(0.5f, 0.5f, new Vector2(-0.1f, 0), 0), 1));
            shapes.Add(new CircleShape(0.5f, 1));

            List <Body> bodies = PathManager.EvenlyDistributeShapesAlongPath(World, _path, shapes, BodyType.Dynamic, 20);

            //Attach the bodies together with revolute joints
            PathManager.AttachBodiesWithRevoluteJoint(World, bodies, new Vector2(0, 0.5f), new Vector2(0, -0.5f), true,
                                                      true);

            xform = new Vector2(-25, 0);
            _path.Translate(ref xform);

            Body body = BodyFactory.CreateBody(World);

            body.BodyType = BodyType.Static;

            //Static shape made up of edges
            PathManager.ConvertPathToEdges(_path, body, 25);
            body.Position -= new Vector2(0, 10);

            xform = new Vector2(0, 15);
            _path.Translate(ref xform);

            PathManager.ConvertPathToPolygon(_path, body, 1, 50);
        }
Beispiel #6
0
        public Whip(Texture2D tex, Vector2 start, Vector2 end, World world)
        {
            this.tex   = tex;
            this.world = world;

            this.start = start / 64;
            this.end   = end / 64;

            Path path = new Path();



            path.Add(this.start);
            path.Add(this.end);

            //A single chainlink
            PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(0.125f, 0.125f), 20);

            // ChainLink shape = new ChainLink(100);

            //Use PathFactory to create all the chainlinks based on the chainlink created before.
            chainLinks = PathManager.EvenlyDistributeShapesAlongPath(world, path, shape, BodyType.Dynamic, 30);

            foreach (Body chainLink in chainLinks)
            {
                foreach (Fixture f in chainLink.FixtureList)
                {
                    f.Friction            = 0.2f;
                    f.CollisionCategories = Category.Cat3;
                    Category whipMask = Category.All ^ Category.Cat2;
                    f.CollidesWith = whipMask;
                    f.Body.BodyId  = 8;
                    f.OnCollision += new OnCollisionEventHandler(OnCollision);
                }
            }

            //Fix the first chainlink to the world
            //  FixedRevoluteJoint fixedJoint = new FixedRevoluteJoint(chainLinks[0], Vector2.Zero, chainLinks[0].Position);
//            Game1.world.AddJoint(fixedJoint);



            //Attach all the chainlinks together with a revolute joint. This is the spacing between links
            joints = PathManager.AttachBodiesWithRevoluteJoint(world, chainLinks,
                                                               new Vector2(0, -0.1f),
                                                               new Vector2(0, 0.1f),
                                                               false, false);

            //The chain is breakable
            for (int i = 0; i < joints.Count; i++)
            {
                RevoluteJoint r = joints[i];
                r.Breakpoint = 5000f;
            }
        }
Beispiel #7
0
        public override void LoadContent()
        {
            base.LoadContent();

            World.Gravity = new Vector2(0, -9.82f);

            _border = new Border(World, ScreenManager, Camera);

            /* Bridge */
            //We make a path using 2 points.
            Path bridgePath = new Path();

            bridgePath.Add(new Vector2(-15, -5));
            bridgePath.Add(new Vector2(15, -5));
            bridgePath.Closed = false;

            Vertices     box   = PolygonTools.CreateRectangle(0.25f / 2f, 1f / 2f);
            PolygonShape shape = new PolygonShape(box, 20);

            _bridgeBodies = PathManager.EvenlyDistributeShapesAlongPath(World, bridgePath, shape, BodyType.Dynamic, 29);
            _bridgeBox    = new Sprite(ScreenManager.Assets.TextureFromShape(shape, MaterialType.Dots, Color.SandyBrown, 1f));

            //Attach the first and last fixtures to the world
            JointFactory.CreateRevoluteJoint(World, HiddenBody, _bridgeBodies[0], new Vector2(0f, 0.5f));
            JointFactory.CreateRevoluteJoint(World, HiddenBody, _bridgeBodies[_bridgeBodies.Count - 1], new Vector2(0, -0.5f));

            PathManager.AttachBodiesWithRevoluteJoint(World, _bridgeBodies, new Vector2(0f, 0.5f), new Vector2(0f, -0.5f), false, true);

            /* Soft body */
            //We make a rectangular path.
            Path rectanglePath = new Path();

            rectanglePath.Add(new Vector2(-6, 11));
            rectanglePath.Add(new Vector2(-6, -1));
            rectanglePath.Add(new Vector2(6, -1));
            rectanglePath.Add(new Vector2(6, 11));
            rectanglePath.Closed = true;

            //Creating two shapes. A circle to form the circle and a rectangle to stabilize the soft body.
            List <Shape> shapes = new List <Shape>(2);

            shapes.Add(new PolygonShape(PolygonTools.CreateRectangle(1f / 2f, 1f / 2f, new Vector2(-0.1f, 0f), 0f), 1f));
            shapes.Add(new CircleShape(0.5f, 1f));

            //We distribute the shapes in the rectangular path.
            _softBodies = PathManager.EvenlyDistributeShapesAlongPath(World, rectanglePath, shapes,
                                                                      BodyType.Dynamic, 30);
            _softBodyBox         = new Sprite(ScreenManager.Assets.TextureFromShape(shapes[0], MaterialType.Blank, Color.Silver * 0.8f, 1f));
            _softBodyBox.Origin += new Vector2(2.4f, 0f);
            _softBodyCircle      = new Sprite(ScreenManager.Assets.TextureFromShape(shapes[1], MaterialType.Waves, Color.Silver, 1f));

            //Attach the bodies together with revolute joints. The rectangular form will converge to a circular form.
            PathManager.AttachBodiesWithRevoluteJoint(World, _softBodies, new Vector2(0f, 0.5f), new Vector2(0f, -0.5f),
                                                      true, true);
        }
        public override void Update(GameTime gameTime)
        {
            if (Path == null)
            {
                Path = new FarseerPhysics.Common.Path();
                Path.Add(position / Level.PixelPerMeter);
                Path.Add(new Vector2(position.X, position.Y + Length) / Level.PixelPerMeter);
                Path.Closed = false;

                float rectLength = (Length / Segments) / Level.PixelPerMeter;
                float rectWidth  = (Width) / Level.PixelPerMeter;

                List <Shape> shapes = new List <Shape>(1);
                //shapes.Add(new PolygonShape(PolygonTools.CreateRectangle(Width/Level.PixelPerMeter, (Length/Segments)/Level.PixelPerMeter, new Vector2(0, 0), 0f), 1));
                shapes.Add(new PolygonShape(PolygonTools.CreateRectangle(rectWidth, rectLength, new Vector2(rectWidth, rectLength), 0f), 1));
                Bodies = PathManager.EvenlyDistributeShapesAlongPath(Level.Physics, Path, shapes, BodyType.Dynamic, Segments);


                if (Object1 == null)
                {
                    Bodies.ElementAt(0).BodyType = BodyType.Static;
                }
                else
                {
                    WeldJoint joint = JointFactory.CreateWeldJoint(Level.Physics, Object1.fixture.Body, Bodies.ElementAt(0), Vector2.Zero, Vector2.Zero);
                    foreach (Body body in Bodies)
                    {
                        foreach (Fixture fix in body.FixtureList)
                        {
                            fix.IgnoreCollisionWith(Object1.fixture);
                        }
                    }
                }
                if (Object2 != null)
                {
                    WeldJoint joint = JointFactory.CreateWeldJoint(Level.Physics, Object2.fixture.Body, Bodies.ElementAt(Bodies.Count - 1), Vector2.Zero, Vector2.Zero);
                    foreach (Body body in Bodies)
                    {
                        foreach (Fixture fix in body.FixtureList)
                        {
                            fix.IgnoreCollisionWith(Object2.fixture);
                        }
                    }
                }

                PathManager.AttachBodiesWithRevoluteJoint(Level.Physics, Bodies, new Vector2(0, 0.25f), new Vector2(0, -0.25f), false, false);
            }


            Circle.position = position;
        }
Beispiel #9
0
        private void LoadObstacles()
        {
            Vertices     box   = PolygonTools.CreateRectangle(1f, 10f);
            PolygonShape shape = new PolygonShape(box, 30);

            _bridgeBox =
                new Sprite(ScreenManager.Assets.TextureFromShape(shape, MaterialType.Dots, Color.SandyBrown, 1f));

            Path bridgePathL = new Path();

            bridgePathL.Add(new Vector2(-400, -50));
            bridgePathL.Add(new Vector2(0, 0));
            bridgePathL.Closed = false;

            _bridgeBodiesL = PathManager.EvenlyDistributeShapesAlongPath(World, bridgePathL, shape,
                                                                         BodyType.Dynamic, 30);


            //Attach the first and last fixtures to the world
            JointFactory.CreateFixedRevoluteJoint(World, _bridgeBodiesL[0], new Vector2(0f, -0.5f),
                                                  _bridgeBodiesL[0].Position);
            JointFactory.CreateFixedRevoluteJoint(World, _bridgeBodiesL[_bridgeBodiesL.Count - 1], new Vector2(0, 0.5f),
                                                  _bridgeBodiesL[_bridgeBodiesL.Count - 1].Position);

            PathManager.AttachBodiesWithRevoluteJoint(World, _bridgeBodiesL, new Vector2(0f, -0.5f),
                                                      new Vector2(0f, 0.5f),
                                                      false, true);

            Path bridgePathR = new Path();

            bridgePathR.Add(new Vector2(350, -50));
            bridgePathR.Add(new Vector2(0, 0));
            bridgePathR.Closed = false;

            _bridgeBodiesR = PathManager.EvenlyDistributeShapesAlongPath(World, bridgePathR, shape,
                                                                         BodyType.Dynamic, 30);

            //Attach the first and last fixtures to the world
            JointFactory.CreateFixedRevoluteJoint(World, _bridgeBodiesR[0], new Vector2(0f, -0.5f),
                                                  _bridgeBodiesR[0].Position);
            JointFactory.CreateFixedRevoluteJoint(World, _bridgeBodiesR[_bridgeBodiesR.Count - 1], new Vector2(0, 0.5f),
                                                  _bridgeBodiesR[_bridgeBodiesR.Count - 1].Position);

            PathManager.AttachBodiesWithRevoluteJoint(World, _bridgeBodiesR, new Vector2(0f, -0.5f),
                                                      new Vector2(0f, 0.5f),
                                                      false, true);
        }
Beispiel #10
0
        public Rope(Vector2 RopeStart, Vector2 RopeEnd)
        {
            path.Add(RopeStart);
            path.Add(RopeEnd);


            ropeText = GameLoop.gameInstance.Content.Load <Texture2D>("Sprites/rope");
            shape    = new PolygonShape(PolygonTools.CreateRectangle(0.05f, 0.1f), 0.6f);

            chainLinks = PathManager.EvenlyDistributeShapesAlongPath(Level.Physics, path, shape, BodyType.Dynamic, 20);

            /*
             * MovingFixture = FixtureFactory.CreateRectangle(Level.Physics, 2, 2, 1);
             *
             * MovingFixture.Body.BodyType = BodyType.Static;
             * MovingFixture.Body.Position = RopeStart;
             *
             */



            foreach (Body chainLink in chainLinks)
            {
                chainLink.LinearDamping = 0.5f;
                foreach (Fixture f in chainLink.FixtureList)
                {
                    f.Friction     = 2.0f;
                    f.CollidesWith = CollisionCategory.None;
                }
            }
            lastBody = chainLinks[(chainLinks.Count - 1)];



            // weld = new WeldJoint(MovingFixture.Body, chainLinks[0], Vector2.Zero, Vector2.Zero);
            // Level.Physics.AddJoint(weld);
            RopeJoint rope = new RopeJoint(chainLinks[0], chainLinks[chainLinks.Count - 1], Vector2.Zero, Vector2.Zero);

            chainLinks[0].BodyType = BodyType.Static;

            MovingBody = chainLinks[0];


            PathManager.AttachBodiesWithRevoluteJoint(Level.Physics, chainLinks, new Vector2(0, -0.1f), new Vector2(0, 0.1f),
                                                      false, false);
        }
Beispiel #11
0
        private ChainTest()
        {
            //Ground
            BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            //Chain start / end
            Path path = new Path();

            path.Add(new Vector2(0, 25));
            path.Add(new Vector2(40, 25));

            //A single chainlink
            PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(0.125f, 0.6f), 20);

            //Use PathFactory to create all the chainlinks based on the chainlink created before.
            List <Body> chainLinks = PathManager.EvenlyDistributeShapesAlongPath(World, path, shape, BodyType.Dynamic, 30);

            foreach (Body chainLink in chainLinks)
            {
                foreach (Fixture f in chainLink.FixtureList)
                {
                    f.Friction = 0.2f;
                }
            }

            //Fix the first chainlink to the world
            FixedRevoluteJoint fixedJoint = new FixedRevoluteJoint(chainLinks[0], Vector2.Zero, chainLinks[0].Position);

            World.AddJoint(fixedJoint);

            //Attach all the chainlinks together with a revolute joint
            List <RevoluteJoint> joints = PathManager.AttachBodiesWithRevoluteJoint(World, chainLinks,
                                                                                    new Vector2(0, -0.6f),
                                                                                    new Vector2(0, 0.6f),
                                                                                    false, false);

            //The chain is breakable
            for (int i = 0; i < joints.Count; i++)
            {
                RevoluteJoint r = joints[i];
                r.Breakpoint = 10000f;
            }
        }
        /// <summary>
        /// Creates a chain.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="start">The start.</param>
        /// <param name="end">The end.</param>
        /// <param name="linkWidth">The width.</param>
        /// <param name="linkHeight">The height.</param>
        /// <param name="numberOfLinks">The number of links.</param>
        /// <param name="linkDensity">The link density.</param>
        /// <param name="attachRopeJoint">Creates a rope joint between start and end. This enforces the length of the rope. Said in another way: it makes the rope less bouncy.</param>
        /// <returns></returns>
        public static Path CreateChain(World world, Vector2 start, Vector2 end, float linkWidth, float linkHeight, int numberOfLinks, float linkDensity, bool attachRopeJoint)
        {
            Debug.Assert(numberOfLinks >= 2);

            //Chain start / end
            Path path = new Path();

            path.Add(start);
            path.Add(end);

            //A single chainlink
            PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(linkWidth, linkHeight), linkDensity);

            //Use PathManager to create all the chainlinks based on the chainlink created before.
            List <Body> chainLinks = PathManager.EvenlyDistributeShapesAlongPath(world, path, shape, BodyType.Dynamic, numberOfLinks);

            //TODO
            //if (fixStart)
            //{
            //    //Fix the first chainlink to the world
            //    JointFactory.CreateFixedRevoluteJoint(world, chainLinks[0], new Vector2(0, -(linkHeight / 2)),
            //                                          chainLinks[0].Position);
            //}

            //if (fixEnd)
            //{
            //    //Fix the last chainlink to the world
            //    JointFactory.CreateFixedRevoluteJoint(world, chainLinks[chainLinks.Count - 1],
            //                                          new Vector2(0, (linkHeight / 2)),
            //                                          chainLinks[chainLinks.Count - 1].Position);
            //}

            //Attach all the chainlinks together with a revolute joint
            PathManager.AttachBodiesWithRevoluteJoint(world, chainLinks, new Vector2(0, -linkHeight), new Vector2(0, linkHeight), false, false);

            if (attachRopeJoint)
            {
                JointFactory.CreateRopeJoint(world, chainLinks[0], chainLinks[chainLinks.Count - 1], Vector2.Zero, Vector2.Zero);
            }

            return(path);
        }
Beispiel #13
0
        protected override void SetupPhysics(World world)
        {
#if EDITOR
#else
            this._pathBodies = new List <Body>();
            float   width    = ConvertUnits.ToSimUnits(this._texture.Width * 0.5f);
            float   height   = ConvertUnits.ToSimUnits(this._texture.Height * 0.5f);
            Vector2 startPos = ConvertUnits.ToSimUnits(this._position);
            Vector2 endPos   = ConvertUnits.ToSimUnits(this._endPosition);

            Path _ropePath = new Path();
            _ropePath.Add(startPos);
            _ropePath.Add(endPos);

            PolygonShape rotationPointShape = new PolygonShape(PolygonTools.CreateCircle(height, 8), 25);
            PolygonShape shape       = new PolygonShape(PolygonTools.CreateRectangle(width, height), ConvertUnits.ToSimUnits(1.0f));
            PolygonShape sensorShape = new PolygonShape(PolygonTools.CreateCircle(height * 1.5f, 6), 1.0f);

            List <Shape> shapes = new List <Shape>(2);
            shapes.Add(new PolygonShape(PolygonTools.CreateRectangle(0.5f, 0.5f, new Vector2(-0.1f, 0f), 0f), 1f));
            shapes.Add(new CircleShape(0.5f, 1f));

            _pathBodies = PathManager.EvenlyDistributeShapesAlongPath(world, _ropePath, shapes,
                                                                      BodyType.Dynamic, _chainCount);

            JointFactory.CreateFixedRevoluteJoint(world, _pathBodies[0], Vector2.Zero, startPos);

            PathManager.AttachBodiesWithRevoluteJoint(world, _pathBodies, new Vector2(0f, -0.5f), new Vector2(0f, 0.5f),
                                                      false, true);

            for (int i = 1; i < _pathBodies.Count; i++)
            {
                _pathBodies[i].FixtureList[0].CollidesWith = Category.All & ~Category.Cat10 & ~Category.Cat12;
                _pathBodies[i].FixtureList[1].CollidesWith = Category.All & ~Category.Cat10 & ~Category.Cat12;

                Fixture fix = FixtureFactory.AttachCircle(height * 2, 0.0f, _pathBodies[i]);
                fix.IsSensor = true;

                fix.OnCollision  += Body_OnCollision;
                fix.OnSeparation += Body_OnSeparation;
            }

            //Body prevBody = new Body(world); ;
            //for (int i = 0; i < _chainCount; ++i)
            //{
            //    Body body = new Body(world);
            //    body.BodyType = BodyType.Dynamic;
            //    body.Position = startPos + new Vector2(0, height * i);

            //    if (i == 0)
            //    {
            //        Fixture fixture = body.CreateFixture(rotationPointShape);
            //        fixture.Friction = 0.2f;
            //        body.AngularDamping = 0.4f;

            //        FixedRevoluteJoint fixedJoint = JointFactory.CreateFixedRevoluteJoint(world, body, Vector2.Zero, startPos);
            //    }
            //    else
            //    {
            //        Fixture fixture = body.CreateFixture(shape);
            //        fixture.Friction = 0.2f;

            //        Fixture sensorFix = FixtureFactory.AttachCircle(height * 2, 0.0f, body);
            //        sensorFix.IsSensor = true;

            //        fixture.CollidesWith = Category.All & ~Category.Cat10 & ~Category.Cat12;

            //        RopeJoint rj = new RopeJoint(prevBody, body, new Vector2(0.0f, height), new Vector2(0.0f, -height * 0.5f));

            //        rj.CollideConnected = false;
            //        world.AddJoint(rj);

            //        body.FixtureList[1].Body.OnCollision += Body_OnCollision;
            //        body.FixtureList[1].Body.OnSeparation += Body_OnSeparation;
            //    }

            //    prevBody = body;
            //    _pathBodies.Add(body);
            //}
#endif
        }
Beispiel #14
0
        public PathEntity GetPathEntity(Vector2 scale)
        {
            float        scaleD     = (scale.X + scale.Y) / 2;
            PathEntity   pathEntity = new PathEntity();
            List <Shape> shapes     = new List <Shape>();
            Body         b          = Body.GetPhysicsBody(scale);

            foreach (var fix in b.FixtureList)
            {
                shapes.Add(fix.Shape);
            }


            Path p = new Path();

            foreach (Vector2 node in Path)
            {
                p.Add(UnitsConverter.ToSimUnits(node) * scale);
            }

            pathEntity.Bodies = PathManager.EvenlyDistributeShapesAlongPath(
                Platform.Instance.PhysicsWorld,
                p,
                shapes,
                Body.Static ? BodyType.Static : BodyType.Dynamic,
                BodyCount + 1
                );

            foreach (var link in pathEntity.Bodies)
            {
                link.CollisionGroup = b.FixtureList[0].CollisionGroup;
            }

            Platform.Instance.PhysicsWorld.RemoveBody(b);

            switch (JointType)
            {
            case JointType.Revolute:

                pathEntity.Joints = new List <Joint>(PathManager.AttachBodiesWithRevoluteJoint(
                                                         Platform.Instance.PhysicsWorld,
                                                         pathEntity.Bodies,
                                                         UnitsConverter.ToSimUnits(Anchor1) * scale,
                                                         UnitsConverter.ToSimUnits(Anchor2) * scale,
                                                         ConnectFirstAndLast,
                                                         CollideConnected
                                                         ));

                return(pathEntity);

            case JointType.Slider:

                pathEntity.Joints = new List <Joint>(PathManager.AttachBodiesWithSliderJoint(
                                                         Platform.Instance.PhysicsWorld,
                                                         pathEntity.Bodies,
                                                         UnitsConverter.ToSimUnits(Anchor1) * scale,
                                                         UnitsConverter.ToSimUnits(Anchor2) * scale,
                                                         ConnectFirstAndLast,
                                                         CollideConnected,
                                                         UnitsConverter.ToSimUnits(MaxLength) * scaleD,
                                                         UnitsConverter.ToSimUnits(MinLength) * scaleD
                                                         ));

                return(pathEntity);
            }

            return(new PathEntity());
        }
        public override void Construct()
        {
            Demo.Window.Title = "Path Demo";

            Demo.Camera3D.Position = new Vector3(16.36052f, 7.006462f, 1.119595f);
            Demo.Camera3D.Target   = new Vector3(15.43861f, 6.659885f, 0.9464932f);

            HiddenBody = Demo.World3D.World2D.CreateBody(Vector2.Zero);

            Path bridgePath = new Path();

            bridgePath.Add(new Vector2(-17, 5));
            bridgePath.Add(new Vector2(17, 5));

            bridgePath.Closed = false;

            Model plankModel = Demo.Content.Load <Model>("Models/bridge");

            PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(0.125f, 0.5f), 20f);

            List <Body> bodies =
                PathManager.EvenlyDistributeShapesAlongPath(Demo.World3D.World2D,
                                                            bridgePath, shape,
                                                            BodyType.Dynamic,
                                                            34);

            JointFactory.CreateRevoluteJoint(
                Demo.World3D.World2D,
                HiddenBody,
                bodies[0],
                new Vector2(0f, 0.5f));
            JointFactory.CreateRevoluteJoint(
                Demo.World3D.World2D,
                HiddenBody,
                bodies[bodies.Count - 1],
                new Vector2(0f, -0.5f));

            PathManager.AttachBodiesWithRevoluteJoint(
                Demo.World3D.World2D,
                bodies,
                new Vector2(0f, 0.5f),
                new Vector2(0f, -0.5f),
                false,
                true);

            foreach (Body b in bodies)
            {
                Demo.World3D.AddBody3D(new Body3D(plankModel, b));
            }

            Body body = new Body();

            body.CreateCircle(1f, 1f);
            body.SetRestitution(0.7f);

            Body3D body3D =
                Body3DFactory.CreateBody3D(
                    Demo.Content.Load <Model>("Models/Primitives/goldBall"),
                    Demo.World3D.World2D,
                    body,
                    new Vector2(8f, 8f));

            Demo.World3D.AddBody3D(body3D);
            body.ApplyAngularImpulse(5f);
        }