Example #1
0
        // RAYCAST

        public TSRaycastHit2D[] Raycast(TSVector2 i_Origin, TSVector2 i_Direction, FP i_Distance, int i_Mask)
        {
            List <TSRaycastHit2D> result = new List <TSRaycastHit2D>();

            Func <Physics2D.Fixture, TSVector2, TSVector2, FP, FP> callback = delegate(Physics2D.Fixture i_Fixture, TSVector2 i_Point, TSVector2 i_Normal, FP i_Fraction)
            {
                GameObject go = GetGameObject(i_Fixture.Body);

                int layerMask = (1 << go.layer);
                if ((i_Mask & layerMask) != 0)
                {
                    TSCollider2D collider2D = go.GetComponent <TSCollider2D>();

                    TSVector2 distanceVector = i_Point - i_Origin;
                    FP        distance       = distanceVector.magnitude;

                    result.Add(new TSRaycastHit2D(collider2D, i_Point, i_Normal, distance, i_Fraction));
                }

                return(-1);
            };

            m_World.RayCast(callback, i_Origin, i_Origin + i_Direction * i_Distance);

            if (result.Count == 0)
            {
                return(null);
            }

            return(result.ToArray());
        }
Example #2
0
        /**
         * @brief Destroys a GameObject in a deterministic way.
         *
         * The method {@link #DestroyTSRigidBody} is called and attached TrueSyncBehaviors are disabled.
         *
         * @param rigidBody Instance of a {@link TSRigidBody}
         **/
        public static void SyncedDestroy(GameObject gameObject)
        {
            if (instance != null && instance.lockstep != null)
            {
                SyncedDisableBehaviour(gameObject);

                TSCollider[] tsColliders = gameObject.GetComponentsInChildren <TSCollider>();
                if (tsColliders != null)
                {
                    for (int index = 0, length = tsColliders.Length; index < length; index++)
                    {
                        TSCollider tsCollider = tsColliders[index];
                        DestroyTSRigidBody(tsCollider.gameObject, tsCollider.Body);
                    }
                }

                TSCollider2D[] tsColliders2D = gameObject.GetComponentsInChildren <TSCollider2D>();
                if (tsColliders2D != null)
                {
                    for (int index = 0, length = tsColliders2D.Length; index < length; index++)
                    {
                        TSCollider2D tsCollider2D = tsColliders2D[index];
                        DestroyTSRigidBody(tsCollider2D.gameObject, tsCollider2D.Body);
                    }
                }
            }
        }
Example #3
0
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider2D} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            tsCollider = GetComponent <TSCollider2D>();
            if (transform.parent != null)
            {
                tsParent = transform.parent.GetComponent <TSTransform2D>();
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (tsCollider != null)
            {
                if (tsCollider.IsBodyInitialized)
                {
                    tsCollider.Body.TSPosition    = _position + scaledCenter;
                    tsCollider.Body.TSOrientation = _rotation * FP.Deg2Rad;
                }
            }
            else
            {
                StateTracker.AddTracking(this);
            }

            initialized = true;
        }
        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();
        }
        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();
            }
        }
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider2D} attached.
         **/
        public void Initialize()
        {
            if (m_Initialized)
            {
                return;
            }

            m_TSCollider = GetComponent <TSCollider2D>();

            if (transform.parent != null)
            {
                m_TSParent = transform.parent.GetComponent <TSTransform2D>();
            }

            if (!m_Serialized)
            {
                UpdateEditMode();
            }

            if (m_TSCollider != null)
            {
                if (m_TSCollider.isBodyInitialized)
                {
                    m_TSCollider.body.TSPosition    = m_Position + m_TSCollider.center;
                    m_TSCollider.body.TSOrientation = m_Rotation * FP.Deg2Rad;
                }
            }
            else
            {
                StateTracker.AddTracking(this);
            }

            m_Initialized = true;
        }
Example #7
0
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider2D} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            tsCollider = GetComponent <TSCollider2D>();
            if (transform.parent != null)
            {
                tsParent = transform.parent.GetComponent <TSTransform2D>();
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (tsCollider != null)
            {
                if (tsCollider.IsBodyInitialized)
                {
                    tsCollider.Body.TSPosition    = _position + scaledCenter;
                    tsCollider.Body.TSOrientation = _rotation * FP.Deg2Rad;
                }
            }

            #if STORE_DATA
            _trName = name;
            #endif

            initialized = true;
            position    = _position;
            UpdatePlayMode();
        }
        // 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);
                        }
                    }
                }
            }
        }
Example #9
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;
                }
            }
        }
        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);
        }
Example #13
0
        /// <summary>
        ///@brief Removes objets related to a provided player.
        ///DV 移除与所提供的player相关的对象 :移除玩家
        ///@param playerId Target player's id.
        /// </summary>
        public static void RemovePlayer(int playerId)
        {
            if (instance != null && instance.lockstep != null)
            {
                List <TrueSyncManagedBehaviour> behaviorsList = instance.behaviorsByPlayer[(byte)playerId];

                for (int index = 0, length = behaviorsList.Count; index < length; index++)
                {
                    TrueSyncManagedBehaviour tsmb = behaviorsList[index];
                    tsmb.disabled = true;//第一步将该玩家的 行为全部禁止帧更新

                    //第二步将这些行为的GameObject上拥有TSCollider、TSCollider2D的物理全部掉DestroyTSRigidBody
                    TSCollider[] tsColliders = ((TrueSyncBehaviour)tsmb.trueSyncBehavior).gameObject.GetComponentsInChildren <TSCollider>();
                    if (tsColliders != null)
                    {
                        for (int index2 = 0, length2 = tsColliders.Length; index2 < length2; index2++)
                        {
                            TSCollider tsCollider = tsColliders[index2];

                            if (!tsCollider.Body.TSDisabled)
                            {
                                DestroyTSRigidBody(tsCollider.gameObject, tsCollider.Body);
                            }
                        }
                    }

                    TSCollider2D[] tsCollider2Ds = ((TrueSyncBehaviour)tsmb.trueSyncBehavior).gameObject.GetComponentsInChildren <TSCollider2D>();
                    if (tsCollider2Ds != null)
                    {
                        for (int index2 = 0, length2 = tsCollider2Ds.Length; index2 < length2; index2++)
                        {
                            TSCollider2D tsCollider2D = tsCollider2Ds[index2];

                            if (!tsCollider2D.Body.TSDisabled)
                            {
                                DestroyTSRigidBody(tsCollider2D.gameObject, tsCollider2D.Body);
                            }
                        }
                    }
                }
            }
        }
        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 #15
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);
        }