Beispiel #1
0
        private bool IsSelfCollision(Vector ph, Vector ch, double w, Snake sk, out double it)
        {
            double thr    = sk.TurningRadius * Math.PI; // half
            double curlen = 0;
            int    i      = 0;

            for (; i < sk.Body.Count - 1; i++)
            {
                double len = (sk.Body[i] - sk.Body[i + 1]).Length;
                if (curlen + len >= thr)
                {
                    break;
                }
                curlen += len;
            }
            for (; i < sk.Body.Count - 1; i++)
            {
                double s, t, distsq;
                distsq = IntersectionSolver.ClosestSegSeg(ph, ch, sk.Body[i], sk.Body[i + 1], out s, out t);
                double len = (sk.Body[i] - sk.Body[i + 1]).Length;
                it = curlen + t * len;
                double plen = it / sk.Length;
                double tw   = MathHelper.Map(plen, sk.WidthBeginPercentage, sk.WidthEndPercentage) * sk.Width;
                double ttw  = tw + w;
                if (it >= thr && distsq <= ttw * ttw)
                {
                    return(true);
                }
                curlen += len;
            }
            it = 0;
            return(false);
        }
Beispiel #2
0
        private bool IsHeadCollision(Vector ph, Vector ch, double w, Snake sk, out double it)
        {
            double curlen = 0;

            for (int i = 0; i < sk.Body.Count - 1; i++)
            {
                double s, t, distsq;
                distsq = IntersectionSolver.ClosestSegSeg(ph, ch, sk.Body[i], sk.Body[i + 1], out s, out t);
                double len = (sk.Body[i] - sk.Body[i + 1]).Length;
                it = curlen + t * len;
                double plen = it / sk.Length;
                double tw   = MathHelper.Map(plen, sk.WidthBeginPercentage, sk.WidthEndPercentage) * sk.Width;
                double ttw  = tw + w;
                if (distsq <= ttw * ttw)
                {
                    return(true);
                }
                curlen += len;
            }
            it = 0;
            return(false);
        }