Exemplo n.º 1
0
        internal void Update(GameObject otherGO, Physics2D.Contact c)
        {
            if (this.gameObject == null)
            {
                this.gameObject = otherGO;
                this.collider   = this.gameObject.GetComponent <TSCollider2D>();
                this.rigidbody  = this.gameObject.GetComponent <TSRigidBody2D>();
                this.transform  = this.collider.tsTransform;
            }

            if (c != null)
            {
                if (contacts[0] == null)
                {
                    contacts[0] = new TSContactPoint2D();
                }

                TSVector2 normal;
                Physics2D.FixedArray2 <TSVector2> points;

                c.GetWorldManifold(out normal, out points);

                contacts[0].normal = normal;
                contacts[0].point  = points[0];

                this.relativeVelocity = c.CalculateRelativeVelocity();
            }
        }
Exemplo n.º 2
0
        // CALLBACKS

        private void OnRemovedRigidbody(Physics2D.Body i_Body)
        {
            GameObject go = m_GameObjectMap[i_Body];

            // Remove cached rb.
            if (go != null)
            {
                TSRigidBody2D attachedRigidBody = go.GetComponent <TSRigidBody2D>();
                if (attachedRigidBody != null)
                {
                    m_RigidBodies.Remove(attachedRigidBody);
                }
            }

            // Destroy object.

            if (go != null)
            {
                GameObject.Destroy(go);
            }

            // Clear object map.

            m_GameObjectMap.Remove(i_Body);
        }
        // TrueSyncBehaviour's interface

        public override void OnSyncedUpdate()
        {
            base.OnSyncedUpdate();

            if (!m_Running)
            {
                return;
            }

            FP area = m_Width * m_Height;

            if (area <= FP.Zero)
            {
                return;
            }

            FP deltaTime = TrueSyncManager.deltaTimeMain;

            TSVector2 center = tsTransform2D.position;

            TSCollider2D[] colliders = TSPhysics2D.OverlapBoxAll(center, size, m_Angle, m_LayerMask);

            if (colliders != null)
            {
                if (colliders.Length > 0)
                {
                    for (int index = 0; index < colliders.Length; ++index)
                    {
                        TSCollider2D currentCollider = colliders[index];

                        if (currentCollider == null)
                        {
                            continue;
                        }

                        TSRigidBody2D rigidbody = currentCollider.GetComponent <TSRigidBody2D>();
                        if (rigidbody != null)
                        {
                            // Drag

                            TSVector2 currentVelocity = rigidbody.velocity;
                            currentVelocity   *= FP.One / (FP.One + (deltaTime * m_Drag));
                            rigidbody.velocity = currentVelocity;

                            // Force

                            FP angle = MathFP.ClampAngle(m_ForceAngle, FP.Zero, 360f);

                            TSVector2 forceDirection = TSVector2.right;
                            forceDirection = forceDirection.Rotate(angle);

                            rigidbody.AddForce(forceDirection * m_ForceMagnitude);
                        }
                    }
                }
            }
        }
        // MonoBehaviour's interface

        public void Start()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            m_Rigidbody = GetComponent <TSRigidBody2D>();
        }
Exemplo n.º 5
0
        /**
         *  @brief Creates a new {@link TSRigidBody} when there is no one attached to this GameObject.
         **/
        public void Awake()
        {
            tsTransform = this.GetComponent <TSTransform2D>();
            tsRigidBody = this.GetComponent <TSRigidBody2D>();

            if (lossyScale == TSVector.one)
            {
                lossyScale = TSVector.Abs(transform.localScale.ToTSVector());
            }
        }
Exemplo n.º 6
0
        private void InternalUpdateTransformCache()
        {
            float elapsedTime = Time.fixedTime;

            for (int rbIndex = 0; rbIndex < m_RigidBodies.Count; ++rbIndex)
            {
                TSRigidBody2D rb = m_RigidBodies[rbIndex];
                rb.UpdateTransformCache(elapsedTime);
            }
        }
Exemplo n.º 7
0
        public void Start()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            Initialize();
            rb = GetComponent <TSRigidBody2D> ();
        }
Exemplo n.º 8
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();
        }
        private void InitializeObject(TrueSyncObject i_TrueSyncObject)
        {
            if (i_TrueSyncObject == null)
            {
                return;
            }

            // Register colliders on physics manager.

            for (int index = 0; index < i_TrueSyncObject.colliderCount; ++index)
            {
                TSCollider2D collider = i_TrueSyncObject.GetColliderByIndex(index);

                if (collider == null)
                {
                    continue;
                }

                PhysicsManager.AddBody(collider);
            }

            // Init transforms.

            for (int index = 0; index < i_TrueSyncObject.transformCount; ++index)
            {
                TSTransform2D t = i_TrueSyncObject.GetTransformByIndex(index);

                if (t == null)
                {
                    continue;
                }

                t.Initialize();
            }

            // Init rigidbody, if any.

            for (int index = 0; index < i_TrueSyncObject.rigidbodyCount; ++index)
            {
                TSRigidBody2D rigidbody = i_TrueSyncObject.GetRigidBodyByIndex(index);

                if (rigidbody == null)
                {
                    continue;
                }

                rigidbody.Initialize();
            }
        }
        internal void Update(GameObject i_OtherGO, Physics2D.Contact i_Contact)
        {
            gameObject = i_OtherGO;

            if (i_OtherGO != null)
            {
                collider  = i_OtherGO.GetComponent <TSCollider2D>();
                rigidbody = i_OtherGO.GetComponent <TSRigidBody2D>();
                transform = i_OtherGO.GetComponent <TSTransform2D>();
            }
            else
            {
                collider  = null;
                rigidbody = null;
                transform = null;
            }

            if (i_Contact != null)
            {
                if (contacts[0] == null)
                {
                    contacts[0] = new TSContactPoint2D();
                }

                TSVector2 normal;
                Physics2D.FixedArray2 <TSVector2> points;

                i_Contact.GetWorldManifold(out normal, out points);

                contacts[0].normal = normal;
                contacts[0].point  = points[0];

                relativeVelocity = i_Contact.CalculateRelativeVelocity();
            }
            else
            {
                relativeVelocity = TSVector2.zero;

                if (contacts[0] != null)
                {
                    contacts[0].normal = TSVector2.zero;
                    contacts[0].point  = TSVector2.zero;
                }
            }
        }
        // TrueSyncBehaviour's interface

        public override void OnSyncedTriggerStay(TSCollision2D i_Collision)
        {
            base.OnSyncedCollisionStay(i_Collision);

            GameObject otherGo = i_Collision.gameObject;

            if (otherGo == null)
            {
                return;
            }

            bool valid = ValidateGameObject(otherGo);

            if (!valid)
            {
                return;
            }

            if (m_Running)
            {
                FP deltaTime = TrueSyncManager.deltaTimeMain;

                TSRigidBody2D rigidbody = otherGo.GetComponent <TSRigidBody2D>();
                if (rigidbody != null)
                {
                    // Drag

                    TSVector2 currentVelocity = rigidbody.velocity;
                    currentVelocity   *= FP.One / (FP.One + (deltaTime * m_Drag));
                    rigidbody.velocity = currentVelocity;

                    // Force

                    FP angle = MathFP.ClampAngle(m_ForceAngle, FP.Zero, 360f);

                    TSVector2 forceDirection = TSVector2.right;
                    forceDirection = forceDirection.Rotate(angle);

                    rigidbody.AddForce(forceDirection * m_ForceMagnitude);
                }
            }
        }
        // MonoBehaviour's INTERFACE

        public void Awake()
        {
            m_TSTransform = GetComponent <TSTransform2D>();
            m_TSRigidBody = GetComponent <TSRigidBody2D>();
        }