Example #1
0
        /**
         *  @brief Add a new RigidBody to the world.
         *
         *  @param jRigidBody Instance of a {@link TSRigidBody}.
         **/
        public void AddBody(ICollider iCollider)
        {
            if (!(iCollider is TSCollider))
            {
                Debug.LogError("You have a 2D object but your Physics 2D is disabled.");
                return;
            }

            TSCollider tsCollider = (TSCollider)iCollider;

            if (tsCollider._body != null)
            {
                //already added
                return;
            }

            TSRigidBody            tsRB        = tsCollider.tsTransform.rb; // tsCollider.GetComponent<TSRigidBody>();
            TSRigidBodyConstraints constraints = tsRB != null ? tsRB.constraints : TSRigidBodyConstraints.None;

            tsCollider.Initialize();
            world.AddBody(tsCollider._body);
            GameObject gameObject = tsCollider.gameObject;

            gameObjectMap[tsCollider._body] = gameObject;

            HashList <TrueSyncBehaviour> behaviours = new HashList <TrueSyncBehaviour>();

            TrueSyncBehaviour[] behavioursArray = gameObject.GetComponents <TrueSyncBehaviour>();
            for (int i = 0, count = behavioursArray.Length; i < count; i++)
            {
                behaviours.Add(behavioursArray[i]);
            }
            behavioursMap[tsCollider._body] = behaviours;

            transformMap[tsCollider._body] = tsCollider.tsTransform;

            if (tsCollider.gameObject.transform.parent != null && tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider>() != null)
            {
                TSCollider parentCollider = tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider>();
                world.AddConstraint(new ConstraintHierarchy(parentCollider.Body, tsCollider._body, (tsCollider.tsTransform.position + tsCollider.ScaledCenter) - (parentCollider.tsTransform.position + parentCollider.ScaledCenter)));
            }

            tsCollider._body.FreezeConstraints = constraints;
        }
        public void AddBody(ICollider iCollider)
        {
            if (!(iCollider is TSCollider2D))
            {
                Debug.LogError("You have a 3D object but your Physics 3D is disabled.");
                return;
            }

            TSCollider2D tsCollider = (TSCollider2D)iCollider;

            if (tsCollider._body != null)
            {
                //already added
                return;
            }

            tsCollider.Initialize(world);
            GameObject gameObject = tsCollider.gameObject;

            gameObjectMap[tsCollider._body] = gameObject;

            HashList <TrueSyncBehaviour> behaviours = new HashList <TrueSyncBehaviour>();

            TrueSyncBehaviour[] behavioursArray = gameObject.GetComponents <TrueSyncBehaviour>();
            for (int i = 0, count = behavioursArray.Length; i < count; i++)
            {
                behaviours.Add(behavioursArray[i]);
            }
            behavioursMap[tsCollider._body] = behaviours;

            if (tsCollider.gameObject.transform.parent != null && tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider2D>() != null)
            {
                TSCollider2D   parentCollider = tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider2D>();
                Physics2D.Body childBody      = tsCollider._body;

                childBody.bodyConstraints.Add(new ConstraintHierarchy2D(((Physics2D.Body)parentCollider.Body), tsCollider._body, (tsCollider.GetComponent <TSTransform2D>().position + tsCollider.ScaledCenter) - (parentCollider.GetComponent <TSTransform2D>().position + parentCollider.ScaledCenter)));
            }

            world.ProcessAddedBodies();
        }
Example #3
0
 private void SortAxis(List <SweepPoint> axis)
 {
     for (int i = 1; i < axis.Count; i++)
     {
         SweepPoint sweepPoint = axis[i];
         FP         value      = sweepPoint.Value;
         int        num        = i - 1;
         while (num >= 0 && axis[num].Value > value)
         {
             SweepPoint sweepPoint2 = axis[num];
             bool       flag        = sweepPoint.Begin && !sweepPoint2.Begin;
             if (flag)
             {
                 bool flag2 = base.CheckBoundingBoxes(sweepPoint2.Body, sweepPoint.Body);
                 if (flag2)
                 {
                     HashList <OverlapPair> obj = this.fullOverlaps;
                     lock (obj)
                     {
                         this.fullOverlaps.Add(new OverlapPair(sweepPoint2.Body, sweepPoint.Body));
                     }
                 }
             }
             bool flag3 = !sweepPoint.Begin && sweepPoint2.Begin;
             if (flag3)
             {
                 HashList <OverlapPair> obj2 = this.fullOverlaps;
                 lock (obj2)
                 {
                     this.fullOverlaps.Remove(new OverlapPair(sweepPoint2.Body, sweepPoint.Body));
                 }
             }
             axis[num + 1] = sweepPoint2;
             num--;
         }
         axis[num + 1] = sweepPoint;
     }
 }
Example #4
0
        private void CollisionDetected(RigidBody body1, RigidBody body2, Contact c, string callbackName)
        {
            if (!gameObjectMap.ContainsKey(body1) || !gameObjectMap.ContainsKey(body2))
            {
                return;
            }

            GameObject b1 = gameObjectMap[body1];
            GameObject b2 = gameObjectMap[body2];

            if (b1 == null || b2 == null)
            {
                return;
            }

            TSCollision collision = null;
            HashList <TrueSyncBehaviour> b1Behaviours = behavioursMap[body1];

            collision = GetCollisionInfo(body1, body2, c);
            for (int i = 0, count = b1Behaviours.Count; i < count; i++)
            {
                if (String.Equals(callbackName, "OnSyncedCollisionEnter", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedCollisionEnter(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedCollisionStay", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedCollisionStay(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedCollisionExit", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedCollisionExit(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerEnter", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedTriggerEnter(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerStay", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedTriggerStay(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerExit", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedTriggerExit(collision);
                }
            }

            HashList <TrueSyncBehaviour> b2Behaviours = behavioursMap[body2];

            collision = GetCollisionInfo(body2, body1, c);
            for (int i = 0, count = b2Behaviours.Count; i < count; i++)
            {
                if (String.Equals(callbackName, "OnSyncedCollisionEnter", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedCollisionEnter(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedCollisionStay", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedCollisionStay(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedCollisionExit", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedCollisionExit(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerEnter", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedTriggerEnter(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerStay", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedTriggerStay(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerExit", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedTriggerExit(collision);
                }
            }

            TrueSyncManager.UpdateCoroutines();
        }
Example #5
0
 public ReadOnlyHashset(HashList <T> hashset)
 {
     this.hashset = hashset;
 }