/**
         *  @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 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);
                    }
                }
            }
        }
        public TSRaycastHit Raycast(TSRay ray, FP maxDistance, RaycastCallback callback = null)
        {
            IBody    hitBody;
            TSVector hitNormal;
            FP       hitFraction;

            TSVector origin    = ray.origin;
            TSVector direction = ray.direction;

            if (Raycast(origin, direction, callback, out hitBody, out hitNormal, out hitFraction))
            {
                if (hitFraction <= maxDistance)
                {
                    GameObject  other              = PhysicsManager.instance.GetGameObject(hitBody);
                    TSRigidBody bodyComponent      = other.GetComponent <TSRigidBody>();
                    TSCollider  colliderComponent  = other.GetComponent <TSCollider>();
                    TSTransform transformComponent = other.GetComponent <TSTransform>();
                    return(new TSRaycastHit(bodyComponent, colliderComponent, transformComponent, hitNormal, ray.origin, ray.direction, hitFraction));
                }
            }
            else
            {
                direction *= maxDistance;
                if (Raycast(origin, direction, callback, out hitBody, out hitNormal, out hitFraction))
                {
                    GameObject  other              = PhysicsManager.instance.GetGameObject(hitBody);
                    TSRigidBody bodyComponent      = other.GetComponent <TSRigidBody>();
                    TSCollider  colliderComponent  = other.GetComponent <TSCollider>();
                    TSTransform transformComponent = other.GetComponent <TSTransform>();
                    return(new TSRaycastHit(bodyComponent, colliderComponent, transformComponent, hitNormal, ray.origin, direction, hitFraction));
                }
            }
            return(null);
        }
        public int RaycastAll(TSVector rayOrigin, TSVector rayDirection, FP maxDistance, out TSRaycastHit[] hits, int layerMask)
        {
            List <TSRaycastHit> raycastHits = new List <TSRaycastHit>();
            var direction = rayDirection;

            direction *= maxDistance;
            var d = TSVector.Distance(rayOrigin, rayOrigin + direction);

            world.CollisionSystem.RaycastAll(rayOrigin, direction, (b, n, f) =>
            {
                //UnityEngine.Debug.LogWarning($"{maxDistance}   ,   {f}");
                GameObject other               = PhysicsManager.instance.GetGameObject(b);
                TSRigidBody bodyComponent      = other.GetComponent <TSRigidBody>();
                TSCollider colliderComponent   = other.GetComponent <TSCollider>();
                TSTransform transformComponent = other.GetComponent <TSTransform>();
                var hit = new TSRaycastHit(bodyComponent, colliderComponent, transformComponent, n, rayOrigin, direction, f);
                var tmp = TSVector.Distance(hit.point, rayOrigin);
                Debug.LogWarning($"碰撞的距离{tmp},最大距离{d}");
                if (tmp <= d)
                {
                    raycastHits.Add(hit);
                }
                return(true);
            }, layerMask);
            hits = raycastHits.ToArray();
            return(hits.Length);
        }
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

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

            if (!_serialized)
            {
                UpdateEditMode();
            }

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

            initialized = true;
        }
Exemplo n.º 6
0
        internal void Update(GameObject otherGO, Contact c)
        {
            if (this.gameObject == null)
            {
                this.gameObject = otherGO;
                this.collider   = this.gameObject.GetComponent <TSCollider>();
                this.rigidbody  = this.gameObject.GetComponent <TSRigidBody>();
                this.transform  = this.collider.tsTransform;
            }

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

                this.relativeVelocity = c.CalculateRelativeVelocity();

                contacts[0].normal      = c.Normal;
                contacts[0].point       = c.p1;
                contacts[0].point2      = c.p2;
                contacts[0].Penetration = c.Penetration;
            }
        }
Exemplo n.º 7
0
 public void Init(TSRigidBody rigidbody, TSCollider collider, TSTransform transform, TSVector normal, TSVector origin, TSVector direction, FP fraction)
 {
     this.rigidbody = rigidbody;
     this.collider  = collider;
     this.transform = transform;
     this.normal    = normal;
     this.point     = origin + direction * fraction;
     this.distance  = fraction * direction.magnitude;
 }
Exemplo n.º 8
0
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            rb             = GetComponent <TSRigidBody>();
            transformCache = transform;

            tsCollider = GetComponent <TSCollider>();
            if (transformCache.parent != null)
            {
                tsParent = transformCache.parent.GetComponent <TSTransform>();
            }

            foreach (Transform child in transformCache)
            {
                TSTransform tsChild = child.GetComponent <TSTransform>();
                if (tsChild != null)
                {
                    tsChildren.Add(tsChild);
                }
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

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

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

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

            foreach (Transform child in transform)
            {
                TSTransform tsChild = child.GetComponent <TSTransform>();
                if (tsChild != null)
                {
                    tsChildren.Add(tsChild);
                }
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (tsCollider != null)
            {
                if (tsCollider.IsBodyInitialized)
                {
                    tsCollider.Body.TSPosition    = transform.position.ToTSVector() + scaledCenter;
                    tsCollider.Body.TSOrientation = transform.rotation.ToTSMatrix();
                    localScale = transform.localScale.ToTSVector();
                }
            }
            else
            {
                StateTracker.AddTracking(this);
            }

            initialized = true;
        }
Exemplo n.º 11
0
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

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

            foreach (Transform child in transform)
            {
                TSTransform tsChild = child.GetComponent <TSTransform>();
                if (tsChild != null)
                {
                    tsChildren.Add(tsChild);
                }
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (tsCollider != null)
            {
                if (tsCollider.IsBodyInitialized)
                {
                    tsCollider.Body.TSPosition    = _position + scaledCenter;
                    tsCollider.Body.TSOrientation = TSMatrix.CreateFromQuaternion(_rotation);
                }
            }
            else
            {
                //配合 [AddTracking] Attribute 使用, StateTracker.AddTracking(object obj)通过反射获取obj的成员变量
                StateTracker.AddTracking(this);
            }

            initialized = true;
        }
Exemplo n.º 12
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);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 13
0
        public TSRaycastHit Raycast(TSRay ray, FP maxDistance, int layerMask, RaycastCallback callback = null)
        {
            IBody    hitBody;
            TSVector hitNormal;
            FP       hitFraction;

            TSVector origin    = ray.origin;
            TSVector direction = ray.direction;

            direction *= maxDistance;
            if (Raycast(origin, direction, callback, layerMask, out hitBody, out hitNormal, out hitFraction))
            {
                GameObject  other = PhysicsManager.instance.GetGameObject(hitBody);
                TSTransform transformComponent = transformMap[hitBody];
                TSRigidBody bodyComponent      = transformComponent.rb;
                TSCollider  colliderComponent  = transformComponent.tsCollider;
                hit.Init(bodyComponent, colliderComponent, transformComponent, hitNormal, ray.origin, ray.direction, hitFraction);
                return(hit);
            }

            return(null);
        }