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);
            gameObjectMap[tsCollider._body] = tsCollider.gameObject;

            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 #2
0
        public void AddBody(ICollider i_Collider)
        {
            if (i_Collider == null)
            {
                return;
            }

            bool is2dCollider = (i_Collider is TSCollider2D);

            Debug.Assert(is2dCollider, "3D Physics is not supported.");

            if (!is2dCollider)
            {
                return;
            }

            TSCollider2D tsCollider = (TSCollider2D)i_Collider;

            Debug.Assert(tsCollider.body == null, "Body already added.");

            if (tsCollider.body != null)
            {
                return;
            }

            tsCollider.Initialize(m_World);

            m_GameObjectMap[tsCollider.body] = tsCollider.gameObject;

            Transform    parent         = tsCollider.transform.parent;
            TSCollider2D parentCollider = (parent != null) ? parent.GetComponentInParent <TSCollider2D>() : null;

            if (parentCollider != null)
            {
                Physics2D.Body childBody = tsCollider.body;

                TSTransform2D parentTransform   = parentCollider.GetComponent <TSTransform2D>();
                TSTransform2D colliderTransform = tsCollider.GetComponent <TSTransform2D>();

                childBody.bodyConstraints.Add(new ConstraintHierarchy2D(parentCollider.body, tsCollider.body, (colliderTransform.position + tsCollider.center) - (parentTransform.position + parentCollider.center)));
            }

            TSRigidBody2D attachedRigidBody = tsCollider.GetComponent <TSRigidBody2D>();

            if (attachedRigidBody != null)
            {
                m_RigidBodies.Add(attachedRigidBody);
            }

            m_World.ProcessAddedBodies();
        }
        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();
        }