private void Decompose()
        {
            //Unsubsribe from the PostSolve delegate
            _world.ContactManager.PostSolve -= PostSolve;

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

                Shape  shape    = oldFixture.Shape.Clone();
                object userData = oldFixture.UserData;

                MainBody.DestroyFixture(oldFixture);

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

                Fixture newFixture = body.CreateFixture(shape);
                newFixture.UserData = userData;
                Parts[i]            = newFixture;

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

            _world.RemoveBody(MainBody);
            _world.RemoveBreakableBody(this);
        }
        public void RemoveBody(IBody iBody)
        {
            world.RemoveBody((TrueSync.Physics2D.Body)iBody);
            world.ProcessRemovedBodies();

            gameObjectMap.Remove(iBody);
            behavioursMap.Remove(iBody);

            // TODO: remove iBody from collisioninfo
        }
        private static object OverlapGeneric(Physics2D.Shape i_Shape, TSVector2 i_Position, Physics2D.BodySpecialSensor i_SensorType, int i_Mask)
        {
            Physics2D.World world = (Physics2D.World)PhysicsManager.GetWorld();

            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);
            body.CreateFixture(i_Shape);

            body.BodyType = Physics2D.BodyType.Static;

            body.CollisionCategories = Physics2D.Category.All; // Category.All is used for sweep test objects.
            body.CollidesWith        = (Physics2D.Category)i_Mask;

            body.CollisionGroup = 0;

            body.IsSensor          = true;
            body.SpecialSensor     = i_SensorType;
            body.SpecialSensorMask = (int)Physics2D.Category.All;

            body.Position = i_Position;

            world.RemoveBody(body);
            world.ProcessRemovedBodies();

            if (body._specialSensorResults.Count > 0)
            {
                if (i_SensorType == Physics2D.BodySpecialSensor.ActiveOnce)
                {
                    return(PhysicsManager.GetGameObject(body._specialSensorResults[0]).GetComponent <TSCollider2D>());
                }
                else
                {
                    TSCollider2D[] result = new TSCollider2D[body._specialSensorResults.Count];
                    for (int i = 0; i < body._specialSensorResults.Count; i++)
                    {
                        result[i] = PhysicsManager.GetGameObject(body._specialSensorResults[i]).GetComponent <TSCollider2D>();
                    }

                    return(result);
                }
            }

            return(null);
        }
Example #4
0
        private void RemoveOldData(int xStart, int xEnd, int yStart, int yEnd)
        {
            for (int x = xStart; x < xEnd; x++)
            {
                for (int y = yStart; y < yEnd; y++)
                {
                    //remove old terrain object at grid cell
                    if (_bodyMap[x, y] != null)
                    {
                        for (int i = 0; i < _bodyMap[x, y].Count; i++)
                        {
                            World.RemoveBody(_bodyMap[x, y][i]);
                        }
                    }

                    _bodyMap[x, y] = null;

                    //generate new one
                    GenerateTerrain(x, y);
                }
            }
        }
Example #5
0
        private static object OverlapGeneric(Physics2D.Shape shape, TSVector2 position, Physics2D.BodySpecialSensor sensorType, int layerMask)
        {
            Physics2D.World world = (Physics2D.World)Physics2DWorldManager.instance.GetWorld();

            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);
            body.CreateFixture(shape);

            body.BodyType     = Physics2D.BodyType.Static;
            body.IsSensor     = true;
            body.CollidesWith = Physics2D.Category.All;

            body.SpecialSensor     = sensorType;
            body.SpecialSensorMask = layerMask;
            body.Position          = position;

            world.RemoveBody(body);
            world.ProcessRemovedBodies();

            if (body._specialSensorResults.Count > 0)
            {
                if (sensorType == Physics2D.BodySpecialSensor.ActiveOnce)
                {
                    return(Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[0]).GetComponent <TSCollider2D>());
                }
                else
                {
                    TSCollider2D[] result = new TSCollider2D[body._specialSensorResults.Count];
                    for (int i = 0; i < body._specialSensorResults.Count; i++)
                    {
                        result[i] = Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[i]).GetComponent <TSCollider2D>();
                    }

                    return(result);
                }
            }

            return(null);
        }
Example #6
0
        public static bool Cut(World world, TSVector2 start, TSVector2 end)
        {
            List <Fixture>   fixtures    = new List <Fixture>();
            List <TSVector2> entryPoints = new List <TSVector2>();
            List <TSVector2> exitPoints  = new List <TSVector2>();
            bool             flag        = world.TestPoint(start) != null || world.TestPoint(end) != null;
            bool             result;

            if (flag)
            {
                result = false;
            }
            else
            {
                world.RayCast(delegate(Fixture f, TSVector2 p, TSVector2 n, FP fr)
                {
                    fixtures.Add(f);
                    entryPoints.Add(p);
                    return(1);
                }, start, end);
                world.RayCast(delegate(Fixture f, TSVector2 p, TSVector2 n, FP fr)
                {
                    exitPoints.Add(p);
                    return(1);
                }, end, start);
                bool flag2 = entryPoints.Count + exitPoints.Count < 2;
                if (flag2)
                {
                    result = false;
                }
                else
                {
                    for (int i = 0; i < fixtures.Count; i++)
                    {
                        bool flag3 = fixtures[i].Shape.ShapeType != ShapeType.Polygon;
                        if (!flag3)
                        {
                            bool flag4 = fixtures[i].Body.BodyType > BodyType.Static;
                            if (flag4)
                            {
                                Vertices vertices;
                                Vertices vertices2;
                                CuttingTools.SplitShape(fixtures[i], entryPoints[i], exitPoints[i], out vertices, out vertices2);
                                bool flag5 = vertices.CheckPolygon() == PolygonError.NoError;
                                if (flag5)
                                {
                                    Body body = BodyFactory.CreatePolygon(world, vertices, fixtures[i].Shape.Density, fixtures[i].Body.Position);
                                    body.Rotation        = fixtures[i].Body.Rotation;
                                    body.LinearVelocity  = fixtures[i].Body.LinearVelocity;
                                    body.AngularVelocity = fixtures[i].Body.AngularVelocity;
                                    body.BodyType        = BodyType.Dynamic;
                                }
                                bool flag6 = vertices2.CheckPolygon() == PolygonError.NoError;
                                if (flag6)
                                {
                                    Body body2 = BodyFactory.CreatePolygon(world, vertices2, fixtures[i].Shape.Density, fixtures[i].Body.Position);
                                    body2.Rotation        = fixtures[i].Body.Rotation;
                                    body2.LinearVelocity  = fixtures[i].Body.LinearVelocity;
                                    body2.AngularVelocity = fixtures[i].Body.AngularVelocity;
                                    body2.BodyType        = BodyType.Dynamic;
                                }
                                world.RemoveBody(fixtures[i].Body);
                            }
                        }
                    }
                    result = true;
                }
            }
            return(result);
        }
Example #7
0
 public void RemoveBody(IBody i_Body)
 {
     m_World.RemoveBody((Physics2D.Body)i_Body);
     m_World.ProcessRemovedBodies();
 }
 public void RemoveBody(IBody iBody)
 {
     world.RemoveBody((TrueSync.Physics2D.Body)iBody);
     world.ProcessRemovedBodies();
 }
Example #9
0
 public void Restore(IWorld iWorld)
 {
     TrueSync.Physics2D.World world = (TrueSync.Physics2D.World)iWorld;
     this.bodiesToRemove.Clear();
     this.index  = 0;
     this.length = world.BodyList.Count;
     while (this.index < this.length)
     {
         Body body = world.BodyList[this.index];
         bool flag = !this.clonedPhysics.ContainsKey(body.BodyId);
         if (flag)
         {
             this.bodiesToRemove.Add(body);
         }
         this.index++;
     }
     this.index  = 0;
     this.length = this.bodiesToRemove.Count;
     while (this.index < this.length)
     {
         Body body2 = this.bodiesToRemove[this.index];
         world.RemoveBody(body2);
         this.index++;
     }
     world.ProcessRemovedBodies();
     this.index  = 0;
     this.length = world.BodyList.Count;
     while (this.index < this.length)
     {
         Body body3 = world.BodyList[this.index];
         bool flag2 = this.clonedPhysics.ContainsKey(body3.BodyId);
         if (flag2)
         {
             BodyClone2D bodyClone2D = this.clonedPhysics[body3.BodyId];
             bodyClone2D.Restore(body3);
         }
         this.index++;
     }
     this.index  = 0;
     this.length = world.ContactList.Count;
     while (this.index < this.length)
     {
         TrueSync.Physics2D.Contact item = world.ContactList[this.index];
         world._contactPool.Enqueue(item);
         this.index++;
     }
     world.ContactList.Clear();
     this.contactDic.Clear();
     this.index  = 0;
     this.length = this.contactsClone.Count;
     while (this.index < this.length)
     {
         ContactClone2D             contactClone2D = this.contactsClone[this.index];
         bool                       flag3          = world._contactPool.Count > 0;
         TrueSync.Physics2D.Contact contact;
         if (flag3)
         {
             contact = world._contactPool.Dequeue();
         }
         else
         {
             contact = new TrueSync.Physics2D.Contact();
         }
         contactClone2D.Restore(contact);
         this.contactDic.Add(contact.Key, contact);
         world.ContactList.Add(contact);
         this.index++;
     }
     this.contactEdgeDic.Clear();
     this.index  = 0;
     this.length = this.contactsClone.Count;
     while (this.index < this.length)
     {
         ContactClone2D contactClone2D2 = this.contactsClone[this.index];
         this.contactDic[contactClone2D2.Key]._nodeA = contactClone2D2._nodeA.Restore(false, this.contactDic, this.contactEdgeDic);
         this.contactDic[contactClone2D2.Key]._nodeB = contactClone2D2._nodeB.Restore(false, this.contactDic, this.contactEdgeDic);
         this.index++;
     }
     this.index  = 0;
     this.length = this.contactsClone.Count;
     while (this.index < this.length)
     {
         ContactClone2D contactClone2D3 = this.contactsClone[this.index];
         this.contactDic[contactClone2D3.Key]._nodeA = contactClone2D3._nodeA.Restore(true, this.contactDic, this.contactEdgeDic);
         this.contactDic[contactClone2D3.Key]._nodeB = contactClone2D3._nodeB.Restore(true, this.contactDic, this.contactEdgeDic);
         this.index++;
     }
     this.index  = 0;
     this.length = world.BodyList.Count;
     while (this.index < this.length)
     {
         Body body4 = world.BodyList[this.index];
         bool flag4 = this.clonedPhysics.ContainsKey(body4.BodyId);
         if (flag4)
         {
             BodyClone2D bodyClone2D2 = this.clonedPhysics[body4.BodyId];
             bool        flag5        = bodyClone2D2.contactEdgeClone != null;
             if (flag5)
             {
                 bodyClone2D2.contactEdgeClone.Restore(false, this.contactDic, this.contactEdgeDic);
             }
             else
             {
                 body4.ContactList = null;
             }
         }
         this.index++;
     }
     this.index  = 0;
     this.length = world.BodyList.Count;
     while (this.index < this.length)
     {
         Body body5 = world.BodyList[this.index];
         bool flag6 = this.clonedPhysics.ContainsKey(body5.BodyId);
         if (flag6)
         {
             BodyClone2D bodyClone2D3 = this.clonedPhysics[body5.BodyId];
             bool        flag7        = bodyClone2D3.contactEdgeClone != null;
             if (flag7)
             {
                 body5.ContactList = bodyClone2D3.contactEdgeClone.Restore(true, this.contactDic, this.contactEdgeDic);
             }
         }
         this.index++;
     }
     this.islandClone.Restore(world.Island, this.contactDic);
     this.toiClone.Restore(world._input);
     TreeNode <FixtureProxy>[] nodes = ((DynamicTreeBroadPhase)world.ContactManager.BroadPhase)._tree._nodes;
     this.index  = 0;
     this.length = nodes.Length;
     while (this.index < this.length)
     {
         TreeNode <FixtureProxy> obj = nodes[this.index];
         WorldClone2D.poolTreeFixtureProxy.GiveBack(obj);
         this.index++;
     }
     this.dynamicTreeClone.Restore((DynamicTreeBroadPhase)world.ContactManager.BroadPhase);
     world._worldHasNewFixture = this._worldHasNewFixture;
     Body._bodyIdCounter       = this.bodyCounter;
     Fixture._fixtureIdCounter = this.fixtureCounter;
 }
        /// <summary>
        /// This is a high-level function to cuts fixtures inside the given world, using the start and end points.
        /// Note: We don't support cutting when the start or end is inside a shape.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="start">The startpoint.</param>
        /// <param name="end">The endpoint.</param>
        /// <returns>True if the cut was performed.</returns>
        public static bool Cut(World world, TSVector2 start, TSVector2 end)
        {
            List <Fixture>   fixtures    = new List <Fixture>();
            List <TSVector2> entryPoints = new List <TSVector2>();
            List <TSVector2> exitPoints  = new List <TSVector2>();

            //We don't support cutting when the start or end is inside a shape.
            if (world.TestPoint(start) != null || world.TestPoint(end) != null)
            {
                return(false);
            }

            //Get the entry points
            world.RayCast((f, p, n, fr) =>
            {
                fixtures.Add(f);
                entryPoints.Add(p);
                return(1);
            }, start, end);

            //Reverse the ray to get the exitpoints
            world.RayCast((f, p, n, fr) =>
            {
                exitPoints.Add(p);
                return(1);
            }, end, start);

            //We only have a single point. We need at least 2
            if (entryPoints.Count + exitPoints.Count < 2)
            {
                return(false);
            }

            for (int i = 0; i < fixtures.Count; i++)
            {
                // can't cut circles or edges yet !
                if (fixtures[i].Shape.ShapeType != ShapeType.Polygon)
                {
                    continue;
                }

                if (fixtures[i].Body.BodyType != BodyType.Static)
                {
                    //Split the shape up into two shapes
                    Vertices first;
                    Vertices second;
                    SplitShape(fixtures[i], entryPoints[i], exitPoints[i], out first, out second);

                    //Delete the original shape and create two new. Retain the properties of the body.
                    if (first.CheckPolygon() == PolygonError.NoError)
                    {
                        Body firstFixture = BodyFactory.CreatePolygon(world, first, fixtures[i].Shape.Density, fixtures[i].Body.Position);
                        firstFixture.Rotation        = fixtures[i].Body.Rotation;
                        firstFixture.LinearVelocity  = fixtures[i].Body.LinearVelocity;
                        firstFixture.AngularVelocity = fixtures[i].Body.AngularVelocity;
                        firstFixture.BodyType        = BodyType.Dynamic;
                    }

                    if (second.CheckPolygon() == PolygonError.NoError)
                    {
                        Body secondFixture = BodyFactory.CreatePolygon(world, second, fixtures[i].Shape.Density, fixtures[i].Body.Position);
                        secondFixture.Rotation        = fixtures[i].Body.Rotation;
                        secondFixture.LinearVelocity  = fixtures[i].Body.LinearVelocity;
                        secondFixture.AngularVelocity = fixtures[i].Body.AngularVelocity;
                        secondFixture.BodyType        = BodyType.Dynamic;
                    }

                    world.RemoveBody(fixtures[i].Body);
                }
            }

            return(true);
        }