Exemple #1
0
        private void CreateBody(Physics2D.World world)
        {
            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);

            if (tsMaterial == null)
            {
                tsMaterial = GetComponent <TSMaterial>();
            }

            Physics2D.Shape shape = Shape;
            if (shape != null)
            {
                CreateBodyFixture(body, shape);
            }
            else
            {
                Physics2D.Shape[] shapes = CreateShapes();
                for (int index = 0, length = shapes.Length; index < length; index++)
                {
                    CreateBodyFixture(body, shapes[index]);
                }
            }

            if (tsRigidBody == null)
            {
                body.BodyType = Physics2D.BodyType.Static;
            }
            else
            {
                if (tsRigidBody.isKinematic)
                {
                    body.BodyType = TrueSync.Physics2D.BodyType.Kinematic;
                }
                else
                {
                    body.BodyType      = Physics2D.BodyType.Dynamic;
                    body.IgnoreGravity = !tsRigidBody.useGravity;
                }

                if (tsRigidBody.mass <= 0)
                {
                    tsRigidBody.mass = 1;
                }

                body.FixedRotation = tsRigidBody.freezeZAxis;
                body.Mass          = tsRigidBody.mass;
                body.TSLinearDrag  = tsRigidBody.drag;
                body.TSAngularDrag = tsRigidBody.angularDrag;
            }

            body.IsSensor     = isTrigger;
            body.CollidesWith = Physics2D.Category.All;

            _body = body;
        }
Exemple #2
0
        private void CreateBody(Physics2D.World world)
        {
            Physics2D.Body    body    = Physics2D.BodyFactory.CreateBody(world);
            Physics2D.Fixture fixture = body.CreateFixture(Shape);

            if (tsMaterial == null)
            {
                tsMaterial = GetComponent <TSMaterial>();
            }

            if (tsMaterial != null)
            {
                fixture.Friction    = tsMaterial.friction;
                fixture.Restitution = tsMaterial.restitution;
            }

            if (tsRigidBody == null)
            {
                body.BodyType = Physics2D.BodyType.Static;
            }
            else
            {
                if (tsRigidBody.isKinematic)
                {
                    body.BodyType = TrueSync.Physics2D.BodyType.Kinematic;
                }
                else
                {
                    body.BodyType      = Physics2D.BodyType.Dynamic;
                    body.IgnoreGravity = !tsRigidBody.useGravity;
                }

                if (tsRigidBody.mass <= 0)
                {
                    tsRigidBody.mass = 1;
                }

                body.FixedRotation = tsRigidBody.freezeZAxis;
                body.Mass          = tsRigidBody.mass;
                body.TSLinearDrag  = tsRigidBody.drag;
                body.TSAngularDrag = tsRigidBody.angularDrag;
            }

            body.IsSensor     = isTrigger;
            body.CollidesWith = Physics2D.Category.All;

            _body = body;
        }
Exemple #3
0
        public void Init()
        {
            ChecksumExtractor.Init(this);

            Physics2D.Settings.ContinuousPhysics = SpeculativeContacts;

            TSVector2 gravity = new TSVector2(Gravity.x, Gravity.y);

            m_World = new Physics2D.World(gravity);

            Physics2D.ContactManager.physicsManager = this;

            m_World.BodyRemoved += OnRemovedRigidbody;

            m_World.ContactManager.BeginContact += CollisionEnter;
            m_World.ContactManager.StayContact  += CollisionStay;
            m_World.ContactManager.EndContact   += CollisionExit;
        }
        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);
        }
        // Use this for initialization
        public void Init()
        {
            ChecksumExtractor.Init(this);

            Settings.ContinuousPhysics = SpeculativeContacts;

            world = new TrueSync.Physics2D.World(new TSVector2(Gravity.x, Gravity.y));
            ContactManager.physicsManager = this;

            world.BodyRemoved += OnRemovedRigidBody;
            world.ContactManager.BeginContact += CollisionEnter;
            world.ContactManager.StayContact  += CollisionStay;
            world.ContactManager.EndContact   += CollisionExit;

            gameObjectMap = new Dictionary <IBody, GameObject>();
            collisionInfo = new Dictionary <Physics2D.Body, Dictionary <Physics2D.Body, TSCollision2D> >();

            instance = this;
            AddRigidBodies();
        }
Exemple #6
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);
        }
Exemple #7
0
 public void Clone(IWorld iWorld, bool doChecksum)
 {
     TrueSync.Physics2D.World world = (TrueSync.Physics2D.World)iWorld;
     this.Reset();
     if (doChecksum)
     {
         this.checksum = ChecksumExtractor.GetEncodedChecksum();
     }
     this.clonedPhysics.Clear();
     this.index  = 0;
     this.length = world.BodyList.Count;
     while (this.index < this.length)
     {
         Body        body = world.BodyList[this.index];
         BodyClone2D @new = WorldClone2D.poolRigidBodyClone.GetNew();
         @new.Clone(body);
         this.clonedPhysics.Add(body.BodyId, @new);
         this.index++;
     }
     this.contactsClone.Clear();
     this.index  = 0;
     this.length = world.ContactList.Count;
     while (this.index < this.length)
     {
         TrueSync.Physics2D.Contact contact = world.ContactList[this.index];
         ContactClone2D             new2    = WorldClone2D.poolContactClone.GetNew();
         new2.Clone(contact);
         this.contactsClone.Add(new2);
         this.index++;
     }
     this.islandClone.Clone(world.Island);
     this.toiClone.Clone(world._input);
     this.dynamicTreeClone.Clone((DynamicTreeBroadPhase)world.ContactManager.BroadPhase);
     this._worldHasNewFixture = world._worldHasNewFixture;
     this.bodyCounter         = Body._bodyIdCounter;
     this.fixtureCounter      = Fixture._fixtureIdCounter;
 }
Exemple #8
0
 /**
  *  @brief Initializes Shape and RigidBody and sets initial values to position and orientation based on Unity's transform.
  **/
 public void Initialize(Physics2D.World world)
 {
     CreateBody(world);
 }
Exemple #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;
 }
        private void CreateBody(Physics2D.World i_World)
        {
            Physics2D.Body body = Physics2D.BodyFactory.CreateBody(i_World);

            // Get collision/restitution data from physics material.

            FP materialFriction    = FP.Zero;
            FP materialRestitution = FP.Zero;

            if (m_PhysicsMaterial != null)
            {
                materialFriction    = m_PhysicsMaterial.friction;
                materialRestitution = m_PhysicsMaterial.restitution;
            }
            else
            {
                // Check for a TSMaterial2D. Note: this is deprecated.

                m_TSMaterial = GetComponent <TSMaterial>();

                if (m_TSMaterial != null)
                {
                    materialFriction    = m_TSMaterial.friction;
                    materialRestitution = m_TSMaterial.restitution;
                }
            }

            // Create collision shape(s).

            Physics2D.Shape shape = CreateShape();
            if (shape != null)
            {
                CreateBodyFixture(body, shape, materialFriction, materialRestitution);
            }
            else
            {
                Physics2D.Shape[] shapes = CreateShapes();
                for (int index = 0, length = shapes.Length; index < length; index++)
                {
                    CreateBodyFixture(body, shapes[index], materialFriction, materialRestitution);
                }
            }

            // Setup RigidBody.

            if (m_TSRigidBody == null)
            {
                body.BodyType = Physics2D.BodyType.Static;
            }
            else
            {
                if (m_TSRigidBody.isKinematic)
                {
                    body.BodyType = Physics2D.BodyType.Kinematic;
                }
                else
                {
                    body.BodyType      = Physics2D.BodyType.Dynamic;
                    body.IgnoreGravity = !m_TSRigidBody.useGravity;
                }

                if (m_TSRigidBody.mass <= 0)
                {
                    m_TSRigidBody.mass = 1;
                }

                body.FixedRotation = m_TSRigidBody.freezeZAxis;
                body.Mass          = m_TSRigidBody.mass;

                body.TSLinearDrag  = m_TSRigidBody.linearDrag;
                body.TSAngularDrag = m_TSRigidBody.angularDrag;
            }

            m_Body  = body;
            m_Shape = shape;

            SetIsSensor(m_IsTrigger);
            SetCollisionLayer(gameObject.layer);
        }