private TSCollision GetCollisionInfo(RigidBody body1, RigidBody body2, Contact c)
        {
            if (!collisionInfo.ContainsKey(body1))
            {
                collisionInfo.Add(body1, new Dictionary <RigidBody, TSCollision>());
            }

            Dictionary <RigidBody, TSCollision> collisionInfoBody1 = collisionInfo[body1];

            TSCollision result = null;

            if (collisionInfoBody1.ContainsKey(body2))
            {
                result = collisionInfoBody1[body2];
            }
            else
            {
                result = new TSCollision();
                collisionInfoBody1.Add(body2, result);
            }


            result.Update(gameObjectMap[body2], c);

            return(result);
        }
 /**
  * @brief Callback called when trigger exit.
  **/
 public virtual void OnSyncedTriggerExit(TSCollision other)
 {
 }
 /**
  * @brief Callback called when collision exit.
  **/
 public virtual void OnSyncedCollisionExit(TSCollision other)
 {
 }
 /**
  * @brief Callback called when collision stay.
  **/
 public virtual void OnSyncedCollisionStay(TSCollision other)
 {
 }
Beispiel #5
0
        private void CollisionDetected(RigidBody body1, RigidBody body2, Contact c, string callbackName)
        {
            if (!gameObjectMap.ContainsKey(body1) || !gameObjectMap.ContainsKey(body2))
            {
                return;
            }

            GameObject b1 = gameObjectMap[body1];
            GameObject b2 = gameObjectMap[body2];

            if (b1 == null || b2 == null)
            {
                return;
            }

            TSCollision collision = null;
            HashList <TrueSyncBehaviour> b1Behaviours = behavioursMap[body1];

            collision = GetCollisionInfo(body1, body2, c);
            for (int i = 0, count = b1Behaviours.Count; i < count; i++)
            {
                if (String.Equals(callbackName, "OnSyncedCollisionEnter", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedCollisionEnter(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedCollisionStay", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedCollisionStay(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedCollisionExit", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedCollisionExit(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerEnter", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedTriggerEnter(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerStay", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedTriggerStay(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerExit", StringComparison.InvariantCultureIgnoreCase))
                {
                    b1Behaviours[i].OnSyncedTriggerExit(collision);
                }
            }

            HashList <TrueSyncBehaviour> b2Behaviours = behavioursMap[body2];

            collision = GetCollisionInfo(body2, body1, c);
            for (int i = 0, count = b2Behaviours.Count; i < count; i++)
            {
                if (String.Equals(callbackName, "OnSyncedCollisionEnter", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedCollisionEnter(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedCollisionStay", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedCollisionStay(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedCollisionExit", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedCollisionExit(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerEnter", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedTriggerEnter(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerStay", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedTriggerStay(collision);
                }
                else if (String.Equals(callbackName, "OnSyncedTriggerExit", StringComparison.InvariantCultureIgnoreCase))
                {
                    b2Behaviours[i].OnSyncedTriggerExit(collision);
                }
            }

            TrueSyncManager.UpdateCoroutines();
        }
        //这里的思路都是让玩家移出到碰撞区外,有2个缺点
        //1,瞬移移动,造成抖动
        //2,是否碰撞,临界值无法预料,多加0.01-0.1的距离,在变现上都很抖动.
        public void PositionBack(TSCollision other)
        {
            /***
             * //思路:碰撞点-(碰撞法向量+0.1的临界值)=新位置
             * _addTranslation = new TSVector(other.contacts[0].point.x, 0, other.contacts[0].point.z) - this.position;
             * this.position = new TSVector(other.contacts[0].point.x,0, other.contacts[0].point.z);
             * //FP x = FP.Abs(other.contacts[0].normal.x) * _addTranslation.x * 10;
             * //FP z = FP.Abs(other.contacts[0].normal.z) * _addTranslation.z * 10;
             * FP x =  other.contacts[0].normal.x;
             * if (x < FP.Zero) x -= 0.1;
             * else if (x > FP.Zero) x += 0.1;
             * x = FP.Abs(x);
             * if (_addTranslation.x < FP.Zero) x = -x;
             *
             * FP z =  other.contacts[0].normal.z;
             * if (z < FP.Zero) z -= 0.1;
             * else if (z > FP.Zero) z += 0.1 ;
             * z = FP.Abs(z);
             * if (_addTranslation.z < FP.Zero) z = -z;
             *
             * TSVector ss = new TSVector(x, 0, z);
             * if (x == 0 && z == 0)
             * {
             *  //this.position -= _addTranslation;
             *  this.position -= ss;
             * }
             * else
             * {
             *  this.position -= ss;
             * }
             * Debug.LogErrorFormat("Actor====>PositionBack=5=>_addTranslation={0},{1}", _addTranslation.ToString(), ss.ToString());
             ***/
            /***
             * //思路:碰撞点-(碰撞点-原位置+0.01的临界值)=新位置,即送回原位置
             * TSVector _addTranslation2 = new TSVector(other.contacts[0].point.x, 0, other.contacts[0].point.z) - this.position;
             * if (_addTranslation2.x < FP.Zero) _addTranslation2.x -= 0.01;
             * else if (_addTranslation2.x > FP.Zero) _addTranslation2.x += 0.01;
             * if (_addTranslation2.z < FP.Zero) _addTranslation2.z -= 0.01;
             * else if (_addTranslation2.z > FP.Zero) _addTranslation2.z += 0.01;
             * this.position = new TSVector(other.contacts[0].point.x, 0, other.contacts[0].point.z) - _addTranslation2;
             * Debug.LogErrorFormat("Actor====>PositionBack=5=>_addTranslation={0},{1}", _addTranslation.ToString(), _addTranslation2.ToString());
             ***/
            /***
             * //思路:用(碰撞点-原位置)确定相对方向,取1的模得到向量_addTranslation3;碰撞点-_addTranslation3乘以1.1临界值=新位置
             * TSVector _addTranslation2 = new TSVector(other.contacts[0].point.x, 0, other.contacts[0].point.z) - this.position;
             * TSVector _addTranslation3 = new TSVector(0,0,0);
             * if (_addTranslation2.x < FP.Zero) _addTranslation3.x -= 1;
             * else if (_addTranslation2.x > FP.Zero) _addTranslation3.x += 1;
             * if (_addTranslation2.z < FP.Zero) _addTranslation3.z -= 1;
             * else if (_addTranslation2.z > FP.Zero) _addTranslation3.z += 1;
             * _addTranslation3  = _addTranslation3.normalized;
             * this.position = new TSVector(other.contacts[0].point.x, 0, other.contacts[0].point.z) - _addTranslation3*1.1;
             * Debug.LogErrorFormat("Actor====>PositionBack=5=>_addTranslation={0},{1}", _addTranslation2.ToString(), _addTranslation3.ToString());
             ***/
            /***
             * TSVector _addTranslation2 = new TSVector(other.contacts[0].point.x, 0, other.contacts[0].point.z) - this.position;
             * TSVector _addTranslation3 = TSVector.Project(_addTranslation, new TSVector(-other.contacts[0].normal.z, 0, other.contacts[0].normal.x));
             * this.position -= _addTranslation3;
             * Debug.LogErrorFormat("Actor====>PositionBack=5=>{0},{1}", _addTranslation2.ToString(), _addTranslation3.ToString());
             ***/
            /***
             * TSVector _addTranslation2 = new TSVector(other.contacts[0].normal.x, 0, other.contacts[0].normal.z).normalized;
             * this.position = new TSVector(other.contacts[0].point.x, 0, other.contacts[0].point.z) - _addTranslation2;
             * Debug.LogErrorFormat("Actor====>PositionBack=5=>_addTranslation={0},{1}", this.position.ToString(), _addTranslation2.ToString());
             ***/
            /***
             * this.position -= _addTranslation;
             * FP x = FP.Abs(other.contacts[0].normal.x) * _addTranslation.x * 10;
             * FP z = FP.Abs(other.contacts[0].normal.z) * _addTranslation.z * 10;
             * this.position -= new TSVector(x, 0, z);
             ***/
            /***
             * //思路:控制想增加的距离向量,在碰撞法向量的投影:加上0.1临界值
             * TSVector _addTranslation2 = TSVector.Project(_addTranslation, new TSVector(other.contacts[0].normal.x, 0, other.contacts[0].normal.z));
             * //if (_addTranslation2.x < FP.Zero) _addTranslation2.x -= 0.01;
             * //else if (_addTranslation2.x > FP.Zero) _addTranslation2.x += 0.01;
             * //if (_addTranslation2.z < FP.Zero) _addTranslation2.z -= 0.01;
             * //else if (_addTranslation2.z > FP.Zero) _addTranslation2.z += 0.01;
             * Debug.LogErrorFormat("Actor====>PositionBack=5=>_addTranslation={0},{1}", _addTranslation.ToString(), _addTranslation2.ToString());
             * if (_addTranslation2 == TSVector.zero) _addTranslation2 = _addTranslation;
             * this.position -= _addTranslation2;
             ***/

            //UpdatePlayMode();
        }