/**
         *  @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.GetComponent <TSRigidBody>();
            TSRigidBodyConstraints constraints = tsRB != null ? tsRB.constraints : TSRigidBodyConstraints.None;

            tsCollider.Initialize();
            world.AddBody(tsCollider._body);
            gameObjectMap[tsCollider._body] = tsCollider.gameObject;

            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.GetComponent <TSTransform>().position + tsCollider.ScaledCenter) - (parentCollider.GetComponent <TSTransform>().position + parentCollider.ScaledCenter)));
            }

            tsCollider._body.FreezeConstraints = constraints;
        }
Exemplo n.º 2
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;
        }