Exemplo n.º 1
0
        public override bool CheckGraze(Collider other, out TwinCollisionChild result)
        {
            Vector2 mov  = movement;
            Vector2 oMov = other.movement;

            result = null;
            if (this.isTrigger || other.isTrigger || (mov == Vector2.zero && oMov == Vector2.zero))
            {
                return(false);
            }
            bool ret = false;
            Type t   = other.GetType();

            if (t == Rigidbody.tPolygonCollider || t.IsSubclassOf(Rigidbody.tPolygonCollider))
            {
                return(CheckGraze((PolygonCollider)other, out result));
            }
            return(ret);
        }
Exemplo n.º 2
0
        public bool CheckGraze(PolygonCollider other, out TwinCollisionChild result)
        {
            bool ret0 = false;

            result = null;

            Vector2 mov  = movement;
            Vector2 oMov = other.movement;

            Vector2[] myPoints         = worldPoints;
            Vector2[] otherPoints      = other.worldPoints;
            int       mpl              = myPoints.Length;
            int       mpl1             = mpl - 1;
            int       opl              = otherPoints.Length;
            int       opl1             = opl - 1;
            Vector2   prevContactPoint = Vector2.zero;

            DotToLineCollisionResult r = new DotToLineCollisionResult();

            for (int i = 0; i < mpl; ++i)
            {
                Vector2 p = myPoints [i];

                for (int j = 0; j < opl; ++j)
                {
                    int     j1 = Util.RotaryClamp(j - 1, 0, opl1);
                    Vector2 p1 = otherPoints [j1];
                    Vector2 p2 = otherPoints [j];
                    DotToLineCollisionResult rIJ;
                    if (Physics.MovingDotToMovingLineGraze(
                            p,
                            movement,
                            p1,
                            p2,
                            other.movement,
                            out rIJ) &&
                        (!ret0 || prevContactPoint != rIJ.collisionPoint))
                    {
                        if (!ret0 || rIJ.t < r.t ||
                            (
                                rIJ.t == r.t &&
                                rIJ.dotMovement.sqrMagnitude < r.dotMovement.sqrMagnitude

                                /*&& (
                                 * !r.CheckDotRequirement2(mov+rIJ.dotDepenetration - rIJ.thinDot)
                                 || !r.CheckLineRequirement2(oMov+rIJ.lineDepenetration - rIJ.thinLine)
                                 ||)*/
                            )
                            )
                        {
                            bool collidedIJ = false;
                            if (rIJ.r == 0)
                            {
                                if (Vector2.Dot(
                                        Util.Normal(myPoints[Util.RotaryClamp(i - 1, 0, mpl1)], p),
                                        rIJ.lineNormal
                                        ) == -1)
                                {
                                    collidedIJ = true;
                                }
                            }
                            else if (rIJ.r == 1)
                            {
                                if (Vector2.Dot(
                                        Util.Normal(p, myPoints[Util.RotaryClamp(i + 1, 0, mpl1)]),
                                        rIJ.lineNormal
                                        ) == -1)
                                {
                                    collidedIJ = true;
                                }
                            }
                            else
                            {
                                collidedIJ = true;
                            }
                            if (collidedIJ)
                            {
                                ret0 = true;
                                r    = rIJ;
                                //result = new TwinCollisionChild (this, other, rIJ);
                            }
                        }
                    }
                }
            }
            if (ret0)
            {
                result = new TwinCollisionChild(this, other, r);
            }
            if (ret0 && gameObject.name == "GreenBox" && (result.selfDepenetration.x == 0.3125f || result.selfDepenetration.x == 0.3124999f))
            {
                bool x = true;
            }
            if (ret0 && result.other.gameObject.name == "GreenBox" && (result.otherDepenetration.x == 0.3125f || result.otherDepenetration.x == 0.3124999f))
            {
                bool x = true;
            }
            return(ret0);
        }
Exemplo n.º 3
0
        public void CollisionCheck()
        {
            IEnumerator <Collider> cols0 = gameInstance.colliders.GetEnumerator();
            List <Collider>        cols1 = new List <Collider> ();

            while (cols0.MoveNext())
            {
                Collider c = cols0.Current;
                if (c.active && !c.isTrigger)
                {
                    cols1.Add(c);
                }
            }
            Util.Sort <Collider> (cols1);

            Dictionary <Collider, Vector2[]> movs   = new Dictionary <Collider, Vector2[]> ();
            IEnumerator <Collider>           selves = cols1.GetEnumerator();

            while (selves.MoveNext())
            {
                Collider c = selves.Current;
                movs [c] = new Vector2[] { c.movement, c.movement };
            }

            int                       l             = movs.Count;
            int                       i             = 0;
            long                      timeStamp     = this.timeStamp;
            CollisionBuilder          builder       = new CollisionBuilder();
            List <TwinCollisionChild> allTCC        = new List <TwinCollisionChild> ();
            int                       graze         = 0;
            bool                      collidedWhile = false;

            do
            {
                collidedWhile = false;
                List <TwinCollisionChild> possibleCollisions = new List <TwinCollisionChild> ();
                selves.Reset();
                while (selves.MoveNext())
                {
                    Collider self = selves.Current;
                    IEnumerator <Collider> others = self.possiblyCollidingColliders.GetEnumerator();
                    while (others.MoveNext())
                    {
                        TwinCollisionChild rIJ;
                        if (self.CheckCollision(others.Current, out rIJ))
                        {
                            TwinCollisionChild tc        = rIJ;
                            String             selfName  = tc.self.gameObject.name;
                            String             otherName = tc.other.gameObject.name;

                            /*if (selfName == "GreenBox" && otherName != "Land" && tc.otherDepenetration == Vector2.zero){
                             *      UnityEngine.Debug.Log("2 GreenBox collided with " + otherName + ". tc.otherDepenetration=" + Util.ToString(tc.otherDepenetration)
                             + ". tc.similarDepenetration=" + Util.ToString(tc.similarDepenetration)
                             + ". tc.otherMovement=" + Util.ToString(tc.otherMovement));
                             + }
                             + if (selfName != "Land" && otherName == "GreenBox" && tc.selfDepenetration == Vector2.zero){
                             +      UnityEngine.Debug.Log("2 " + selfName + " collided with GreenBox. tc.selfDepenetration=" + Util.ToString(tc.selfDepenetration)
                             + ". tc.similarDepenetration=" + Util.ToString(tc.similarDepenetration)
                             + ". tc.selfMovement=" + Util.ToString(tc.selfMovement));
                             + }*/
                            possibleCollisions.Add(rIJ);
                            if (rIJ.selfDepenetration != Vector2.zero || rIJ.otherDepenetration != Vector2.zero || rIJ.similarDepenetration != Vector2.zero)
                            {
                                collidedWhile = true;
                            }
                        }
                    }
                }
                Util.Sort(possibleCollisions);


                if (collidedWhile)
                {
                    IEnumerator <TwinCollisionChild> en  = possibleCollisions.GetEnumerator();
                    IEnumerator <TwinCollisionChild> en2 = possibleCollisions.GetEnumerator();
                    while (en.MoveNext())
                    {
                        TwinCollisionChild tc = en.Current;
                        if (tc.toDelete)
                        {
                            continue;
                        }

                        String selfName  = tc.self.gameObject.name;
                        String otherName = tc.other.gameObject.name;

                        /*if (selfName == "GreenBox" && otherName != "Land" && tc.otherDepenetration == Vector2.zero){
                         * UnityEngine.Debug.Log("3 GreenBox collided with " + otherName + ". tc.otherDepenetration=" + Util.ToString(tc.otherDepenetration)
                         + ". tc.similarDepenetration=" + Util.ToString(tc.similarDepenetration)
                         + ". tc.otherMovement=" + Util.ToString(tc.otherMovement));
                         + }
                         + if (selfName != "Land" && otherName == "GreenBox" && tc.selfDepenetration == Vector2.zero){
                         + UnityEngine.Debug.Log("3 " + selfName + " collided with GreenBox. tc.selfDepenetration=" + Util.ToString(tc.selfDepenetration)
                         + ". tc.similarDepenetration=" + Util.ToString(tc.similarDepenetration)
                         + ". tc.selfMovement=" + Util.ToString(tc.selfMovement));
                         + }*/
                        if (tc.selfDepenetration == Vector2.zero && tc.otherDepenetration == Vector2.zero)
                        {
                            continue;
                        }
                        movs[tc.self][1]  += tc.selfDepenetration;
                        movs[tc.other][1] += tc.otherDepenetration;


                        en2.Reset();
                        while (en2.MoveNext())
                        {
                            TwinCollisionChild tci = en2.Current;
                            if (tci.toDelete || tci == tc)
                            {
                                continue;
                            }
                            if (tci.self != tc.self &&
                                tci.self != tc.other &&
                                tci.other != tc.self &&
                                tci.other != tc.other &&
                                !tc.self.possiblyCollidingColliders.Contains(tci.self) &&
                                !tc.self.possiblyCollidingColliders.Contains(tci.other) &&
                                !tc.other.possiblyCollidingColliders.Contains(tci.self) &&
                                !tc.other.possiblyCollidingColliders.Contains(tci.other))
                            {
                                continue;
                            }
                            if (tci.self == tc.other && tci.other == tc.self)
                            {
                                tci.toDelete = true;
                            }
                            if (tci.t > tc.t)
                            {
                                tci.toDelete = true;
                            }
                            else if (tci.t == tc.t &&
                                     (tci.self == tc.self ||
                                      tci.self == tc.other ||
                                      tci.other == tc.self ||
                                      tci.other == tc.other ||
                                      tci.self.possiblyCollidingColliders.Contains(tc.self) ||
                                      tci.self.possiblyCollidingColliders.Contains(tc.other) ||
                                      tci.other.possiblyCollidingColliders.Contains(tc.self) ||
                                      tci.other.possiblyCollidingColliders.Contains(tc.other)))
                            {
                                /*if(!tci.CheckSelfRequirement2(movs[tci.self][1])
                                || !tci.CheckOtherRequirement2(movs[tci.other][1]
                                || )
                                || ){
                                || tci.toDelete = true;
                                || }*/
                                tci.toDelete = true;
                            }
                        }
                    }

                    possibleCollisions.RemoveAll((x) => x.toDelete);
                }
                foreach (TwinCollisionChild tc in possibleCollisions)
                {
                    if (tc.t > 1)
                    {
                        tc.t = 1;
                    }
                    else if (tc.t < 0)
                    {
                        tc.t = 0;
                    }
                    builder.Add(tc);
                    allTCC.Add(tc);
                    String selfName  = tc.self.gameObject.name;
                    String otherName = tc.other.gameObject.name;
                    if (selfName == "GreenBox" && otherName != "Land" && tc.otherDepenetration == Vector2.zero)
                    {
                        UnityEngine.Debug.Log("GreenBox collided with " + otherName + ". tc.otherDepenetration=" + Util.ToString(tc.otherDepenetration)
                                              + ". tc.similarDepenetration=" + Util.ToString(tc.similarDepenetration)
                                              + ". tc.otherMovement=" + Util.ToString(tc.otherMovement));
                    }
                    if (selfName != "Land" && otherName == "GreenBox" && tc.selfDepenetration == Vector2.zero)
                    {
                        UnityEngine.Debug.Log(selfName + " collided with GreenBox. tc.selfDepenetration=" + Util.ToString(tc.selfDepenetration)
                                              + ". tc.similarDepenetration=" + Util.ToString(tc.similarDepenetration)
                                              + ". tc.selfMovement=" + Util.ToString(tc.selfMovement));
                    }
                }
                builder.ApplyNotApplied();
                foreach (KeyValuePair <Collider, Vector2[]> kvp in movs)
                {
                    kvp.Value[0] = kvp.Key.movement;
                    kvp.Value[1] = kvp.Value[0];
                }
                ++i;
                if (i > 100)
                {
                    bool x = true;
                    if (i > 200)
                    {
                        UnityEngine.Debug.Log("STUCK");
                        break;
                    }
                }
            } while(collidedWhile);

            builder.BuildAndBroadcast();
        }
Exemplo n.º 4
0
		public abstract bool CheckCollision(Collider other, out TwinCollisionChild result);
Exemplo n.º 5
0
 public void Add(TwinCollisionChild tcc)
 {
     Add(tcc.selfCollisionChild);
     Add(tcc.otherCollisionChild);
 }
Exemplo n.º 6
0
 public override bool CheckGraze(Collider other, out TwinCollisionChild result)
 {
     throw new NotImplementedException();
 }