Beispiel #1
0
        private void Explode()
        {
            Vector2 min = _mousePos - new Vector2(Radius);
            Vector2 max = _mousePos + new Vector2(Radius);

            AABB affected = new AABB(ref min, ref max);

            System.Collections.Generic.List <Fixture> affectedFixtures = new System.Collections.Generic.List <Fixture>();

            World.QueryAABB(fixture =>
            {
                affectedFixtures.Add(fixture);
                return(true);
            }, ref affected);

            HashSet <Body> uniqueBodies = new HashSet <Body>();

            foreach (Fixture f in affectedFixtures)
            {
                uniqueBodies.Add(f.Body);
            }

            foreach (Body body in uniqueBodies)
            {
                //Check if body is a Destructablebody)
                if (body.UserData is DestructableBody)
                {
                    //Clip the destructablebody against the clip shape
                    DestructableBody db = (DestructableBody)body.UserData;
                    db.Clip(_clipCircle, _mousePos);
                }
            }
        }
        public bool Clip(Vertices clipVertices, Vector2 position)
        {
            Rot       rot = new Rot(0);
            Transform t   = new Transform(ref position, ref rot);

            //Transform shape
            Transform thistransform;

            Body.GetTransform(out thistransform);

            //Transform the shape
            Vertices transformedshape = new Vertices(clipVertices.Count);

            foreach (Vector2 v in clipVertices)
            {
                Vector2 newv = v;
                newv = MathUtils.Mul(ref t, ref newv);
                newv = MathUtils.MulT(ref thistransform, ref newv);
                transformedshape.Add(newv);
            }

            PolyClipError   error;
            List <Vertices> result = YuPengClipper.Difference(Vertices, transformedshape, out error);

            if (error != PolyClipError.None)
            {
                return(false);
            }

            //Need to check if the entire shape was cut,
            //so we can destroy/erase it
            if (result.Count == 0)
            {
                return(false);
            }

            //The shape was split up,
            //so create a new DestructableBody for each piece
            if (result.Count > 1)
            {
                //Create a new destructable body for each extra shape
                for (int i = 1; i < result.Count; i++)
                {
                    DestructableBody db = new DestructableBody(_world, result[i]);
                    db.Body.Position = Body.Position;
                }
            }

            //Set Shape
            Vertices newshape = result[0];

            SetShape(newshape);

            return(true);
        }
        public bool Clip(Vertices clipVertices, Vector2 position)
        {
            Mat22 mat = new Mat22(0);
            Transform t = new Transform(ref position, ref mat);

            //Transform shape
            Transform thistransform;
            Body.GetTransform(out thistransform);

            //Transform the shape
            Vertices transformedshape = new Vertices(clipVertices.Count);
            foreach (Vector2 v in clipVertices)
            {
                Vector2 newv = v;
                newv = MathUtils.Multiply(ref t, ref newv);
                newv = MathUtils.MultiplyT(ref thistransform, ref newv);
                transformedshape.Add(newv);
            }

            PolyClipError error;
            List<Vertices> result = YuPengClipper.Difference(Vertices, transformedshape, out error);

            //Need to check if the entire shape was cut,
            //so we can destroy/erase it
            if (result.Count == 0)
                return false;

            //The shape was split up,
            //so create a new DestructableBody for each piece
            if (result.Count > 1)
            {
                //Create a new destructable body for each extra shape
                for (int i = 1; i < result.Count; i++)
                {
                    DestructableBody db = new DestructableBody(_world, result[i]);
                    db.Body.Position = Body.Position;
                }
            }

            //Set Shape
            Vertices newshape = result[0];
            SetShape(newshape);

            return true;
        }
        private DestructibleTerrainYuPengTest()
        {
            Settings.MaxPolygonVertices = 16;

            //Ground
            BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            //Create 7 blocks
            const float size = 2.0f;
            Vertices v = PolygonTools.CreateRectangle(size, size);

            for (int i = 0; i < 7; ++i)
            {
                DestructableBody db = new DestructableBody(World, v);
                db.Body.Position = new Vector2(-15.0f + size * 3 * i, 20.0f);
            }

            Radius = 3;
        }
Beispiel #5
0
        private DestructibleTerrainYuPengTest()
        {
            Settings.MaxPolygonVertices = 16;

            //Ground
            BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            //Create 7 blocks
            const float size = 2.0f;
            Vertices    v    = PolygonTools.CreateRectangle(size, size);

            for (int i = 0; i < 7; ++i)
            {
                DestructableBody db = new DestructableBody(World, v);
                db.Body.Position = new Vector2(-15.0f + size * 3 * i, 20.0f);
            }

            Radius = 3;
        }