示例#1
0
        public bool Collide(IPositionSet objPositionSet)
        {
            IPosition start        = new Position_Point();
            IPosition end          = new Position_Point();
            IPosition first        = new Position_Point();
            ILevel    currentLevel = new Level();

            for (int i = 1; i < m2mStructure.GetLevelNum(); i++)
            {
                currentLevel = m2mStructure.GetLevel(i);
                objPositionSet.InitToTraverseSet();
                //判断第一点所在的分块是否相交
                if (objPositionSet.NextPosition())
                {
                    start = new Position_Point(currentLevel.ConvertRealValueToRelativeValueX(objPositionSet.GetPosition().GetX()), currentLevel.ConvertRealValueToRelativeValueY(objPositionSet.GetPosition().GetY()));
                    first = start;
                    if (currentLevel.GetPartRefByPoint(start) != null)
                    {
                        if (i < m2mStructure.GetLevelNum() - 1)
                        {
                            goto NextLevel;
                        }
                        else
                        {
                            tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(start.GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(start.GetY())));
                        }
                    }
                }


                while (objPositionSet.NextPosition())
                {
                    //判断每条边所在的分块是否相交
                    end = new Position_Point(currentLevel.ConvertRealValueToRelativeValueX(objPositionSet.GetPosition().GetX()), currentLevel.ConvertRealValueToRelativeValueY(objPositionSet.GetPosition().GetY()));
                    IPositionSet positionSetTemp = writeLineInGridEngine.WriteLineInGrid(currentLevel.GetGridWidth(), currentLevel.GetGridHeight(), start, end);

                    if (positionSetTemp != null)
                    {
                        positionSetTemp.InitToTraverseSet();
                        while (positionSetTemp.NextPosition())
                        {
                            if (currentLevel.GetPartRefByPoint(positionSetTemp.GetPosition()) != null)
                            {
                                if (i < m2mStructure.GetLevelNum() - 1)
                                {
                                    goto NextLevel;
                                }
                                else
                                {
                                    tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(positionSetTemp.GetPosition().GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(positionSetTemp.GetPosition().GetY())));
                                }
                            }
                        }
                    }

                    //判断每条边的终点所在的分块是否相交
                    start = end;
                    if (currentLevel.GetPartRefByPoint(start) != null)
                    {
                        if (i < m2mStructure.GetLevelNum() - 1)
                        {
                            goto NextLevel;
                        }
                        else
                        {
                            tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(start.GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(start.GetY())));
                        }
                    }
                }

                //判断最后一条边所在的分块是否相交
                IPositionSet positionSetTemp2 = writeLineInGridEngine.WriteLineInGrid(currentLevel.GetGridWidth(), currentLevel.GetGridHeight(), end, first);
                if (positionSetTemp2 != null)
                {
                    positionSetTemp2.InitToTraverseSet();
                    while (positionSetTemp2.NextPosition())
                    {
                        if (currentLevel.GetPartRefByPoint(positionSetTemp2.GetPosition()) != null)
                        {
                            if (i < m2mStructure.GetLevelNum() - 1)
                            {
                                goto NextLevel;
                            }
                            else
                            {
                                tempIntersectPositionSet.AddPosition(new Position_Point(currentLevel.ConvertRelativeValueToPartSequenceX(positionSetTemp2.GetPosition().GetX()), currentLevel.ConvertRelativeValueToPartSequenceY(positionSetTemp2.GetPosition().GetY())));
                            }
                        }
                    }
                }

                if (i == m2mStructure.GetLevelNum() - 1 && tempIntersectPositionSet.GetNum() > 0)
                {
                    goto NextLevel;
                }

                #region code for algorithm demo
                if (GetNoCollision != null)
                {
                    GetNoCollision(objPositionSet);
                    Debug.WriteLine(i);
                }
                #endregion

                return(false);//free of collision

                NextLevel :;
            }

            #region code for algorithm demo
            if (GetCollision != null)
            {
                GetCollision(objPositionSet);
            }
            if (GetIntersectPart != null)
            {
                GetIntersectPart(currentLevel, tempIntersectPositionSet);
            }
            #endregion
            tempIntersectPositionSet.Clear();

            return(true);
        }
示例#2
0
 public float ConvertRealValueToRelativeValueY(float realityValue)
 {
     return(realityValue - relativelyPoint.GetY());
 }
示例#3
0
        public static IPositionSet getBlockLine2(IPosition refPos, float itv0, IPosition startPos, IPosition endPos)
        {
            rX  = refPos.GetX();
            rY  = refPos.GetY();
            itv = itv0;
            float sX = startPos.GetX();
            float sY = startPos.GetY();
            float eX = endPos.GetX();
            float eY = endPos.GetY();

            PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate();
            int   xDir = (eX > sX) ? 1 : (eX == sX) ? 0 : -1;
            int   yDir = (eY > sY) ? 1 : (eY == sY) ? 0 : -1;
            float x    = sX;
            float y    = sY;

            //结果只包含一个格
            if (getM(sX) == getM(eX) && getN(sY) == getN(eY))
            {
                x = getM(sX) * itv + itv / 2 + rX;
                y = getN(sY) * itv + itv / 2 + rY;
                result.AddPosition(new Position_Point(x, y));
                return(result);
            }

            //斜率为0或无限大的情况
            else if (xDir == 0 || yDir == 0)
            {
                //eX = getM(eX) * itv + itv / 2 + rX;
                //eY = getN(eY) * itv + itv / 2 + rY;
                //while ((eX - x) * xDir >= 0 && (eY - y) * yDir >= 0)
                //{
                //    x = getM(x) * itv + itv / 2 + rX;
                //    y = getN(y) * itv + itv / 2 + rY;
                //    result.AddPosition(new SimplePosition(x, y));
                //    x += itv * xDir;
                //    y += itv * yDir;
                //}

                while ((eX - x) * xDir >= 0 && (eY - y) * yDir >= 0)
                {
                    result.AddPosition(new Position_Point(getM(x) * itv + itv / 2 + rX, getN(y) * itv + itv / 2 + rY));
                    if (eX % itv == 0 && xDir == 0)
                    {
                        result.AddPosition(new Position_Point(getM(x - itv / 2) * itv + itv / 2 + rX, getN(y) * itv + itv / 2 + rY));
                    }
                    if (eY % itv == 0 && yDir == 0)
                    {
                        result.AddPosition(new Position_Point(getM(x) * itv + itv / 2 + rX, getN(y - itv / 2) * itv + itv / 2 + rY));
                    }
                    x += itv * xDir;
                    y += itv * yDir;
                }
                return(result);
            }

            //斜率不为0且不为无限大的情况
            float k = (eY - sY) / (eX - sX);
            //float nextX, nextY;

            //if (xDir > 0)
            //    nextX = (getM(sX) + 1) * itv + rX;
            //else
            //    nextX = getM(sX) * itv + rX - MIN_ITV;

            //if (yDir > 0)
            //    nextY = (getN(sY) + 1) * itv + rY;
            //else
            //    nextY = getN(sY) * itv + rY - MIN_ITV;

            //while ((eX - x) * xDir >= 0 || (eY - y) * yDir >= 0)
            //{
            //    float px = getM(x) * itv + itv / 2 + rX;
            //    float py = getN(y) * itv + itv / 2 + rY;
            //    result.AddPosition(new SimplePosition(px, py));

            //    float dx = Math.Abs(nextX - x);
            //    float dy = Math.Abs(nextY - y);
            //    double dk = dy / dx;
            //    if (dk > Math.Abs(k))
            //    {
            //        x = nextX;
            //        nextX += itv * xDir;
            //        y = sY + (x - sX) * k;
            //    }
            //    else if (dk < Math.Abs(k))
            //    {
            //        y = nextY;
            //        nextY += itv * yDir;
            //        x = sX + (y - sY) / k;
            //    }
            //    else
            //    {
            //        x = nextX;
            //        y = nextY;
            //        nextX += itv * xDir;
            //        nextY += itv * yDir;
            //    }
            //}

            //xDir>0 yDir>0
            ///找出所有边境交点

            int sxx = getM(sX);
            int exx = getM(eX);
            int syy = getN(sY);
            int eyy = getN(eY);
            List <Position_Point> pointList = new List <Position_Point>();

            if (xDir > 0)
            {
                for (int i = 0; i + sxx < exx + 1; i++)
                {
                    float xx = (i + sxx) * itv + rX;
                    float yy = k * xx + (eY - k * eX);
                    pointList.Add(new Position_Point(xx, yy));
                }
            }
            else
            {
                for (int i = 0; i + exx < sxx + 1; i++)
                {
                    float xx = (i + exx) * itv + rX;
                    float yy = k * xx + (eY - k * eX);
                    pointList.Add(new Position_Point(xx, yy));
                }
            }



            if (yDir > 0)
            {
                for (int i = 0; i + syy < eyy + 1; i++)
                {
                    float yy = (i + syy) * itv + rY;
                    float xx = (yy - (eY - k * eX)) / k;

                    pointList.Add(new Position_Point(xx, yy));
                }
            }
            else
            {
                for (int i = 0; i + eyy < syy + 1; i++)
                {
                    float yy = (i + eyy) * itv + rY;
                    float xx = (yy - (eY - k * eX)) / k;
                    pointList.Add(new Position_Point(xx, yy));
                }
            }

            pointList.Sort(new pointXcomparer());

            for (int i = 1; i < pointList.Count; i++)
            {
                Position_Point p1 = pointList[i - 1];
                Position_Point p2 = pointList[i];
                //把两个交点的中点添加进去
                float px = getM((p1.GetX() + p2.GetX()) / 2) * itv + itv / 2 + rX;
                float py = getN((p1.GetY() + p2.GetY()) / 2) * itv + itv / 2 + rY;
                result.AddPosition(new Position_Point(px, py));

                if (i == pointList.Count - 1)
                {
                    if (eX % itv == 0)
                    {
                        px = getM(eX + (itv / 2)) * itv + itv / 2 + rX;
                        py = getN(eY) * itv + itv / 2 + rY;
                        result.AddPosition(new Position_Point(px, py));
                        px = getM(eX - (itv / 2)) * itv + itv / 2 + rX;
                        result.AddPosition(new Position_Point(px, py));
                    }
                    if (eY % itv == 0)
                    {
                        px = getM(eX) * itv + itv / 2 + rX;
                        py = getN(eY + (itv / 2)) * itv + itv / 2 + rY;
                        result.AddPosition(new Position_Point(px, py));
                        py = getN(eY - (itv / 2)) * itv + itv / 2 + rY;
                        result.AddPosition(new Position_Point(px, py));
                    }
                }
                else if (i == 0)
                {
                    if (sX % itv == 0)
                    {
                        px = getM(sX + (itv / 2)) * itv + itv / 2 + rX;
                        py = getN(sY) * itv + itv / 2 + rY;;
                        result.AddPosition(new Position_Point(px, py));
                        px = getM(sX - (itv / 2)) * itv + itv / 2 + rX;
                        result.AddPosition(new Position_Point(px, py));
                    }
                    if (sY % itv == 0)
                    {
                        px = getM(sX) * itv + itv / 2 + rX;
                        py = getN(sY + (itv / 2)) * itv + itv / 2 + rY;
                        result.AddPosition(new Position_Point(px, py));
                        py = getN(sY - (itv / 2)) * itv + itv / 2 + rY;
                        result.AddPosition(new Position_Point(px, py));
                    }
                }

                if (p1.GetX() == p2.GetX() && p1.GetY() == p2.GetY())
                {
                    px = p1.GetX();
                    py = p2.GetY();
                    result.AddPosition(new Position_Point(px - itv / 2, py + itv / 2));
                    result.AddPosition(new Position_Point(px - itv / 2, py - itv / 2));
                    result.AddPosition(new Position_Point(px + itv / 2, py - itv / 2));
                    result.AddPosition(new Position_Point(px + itv / 2, py + itv / 2));
                }
            }

            return(result);
        }