示例#1
0
    // 旋转图形
    static public SceneShapePolygon rotate(SceneShapeRect rect, Vector2 pos, float angles)
    {
        SceneShapePolygon shape = new SceneShapePolygon();
        Vector3           lt    = new Vector3(rect.mLeft, 0.0f, rect.mTop);
        Vector3           rt    = new Vector3(rect.mRight, 0.0f, rect.mTop);
        Vector3           lb    = new Vector3(rect.mLeft, 0.0f, rect.mBottom);
        Vector3           rb    = new Vector3(rect.mRight, 0.0f, rect.mBottom);

        Quaternion q = new Quaternion();

        q.eulerAngles = new Vector3(0, angles, 0);

        Vector3 center = new Vector3(pos.x, 0.0f, pos.y);

        Vector3 ltnew = (q * (lt - center)) + center;
        Vector3 rtnew = (q * (rt - center)) + center;
        Vector3 lbnew = (q * (lb - center)) + center;
        Vector3 rbnew = (q * (rb - center)) + center;

        shape.PushbackVector2(new Vector2(ltnew.x, ltnew.z));
        shape.PushbackVector2(new Vector2(rtnew.x, rtnew.z));
        shape.PushbackVector2(new Vector2(rbnew.x, rbnew.z));
        shape.PushbackVector2(new Vector2(lbnew.x, lbnew.z));

        return(shape);
    }
示例#2
0
    // 矩形与多边形重叠(相交或包含)
    static public bool overlap(SceneShapeRect rect, SceneShapePolygon polygon)
    {
        // 矩形有顶点在多边形内 一定相交
        if (polygon.contains(rect.leftTop()))
        {
            return(true);
        }

        if (polygon.contains(rect.rightTop()))
        {
            return(true);
        }

        if (polygon.contains(rect.rightBottom()))
        {
            return(true);
        }

        if (polygon.contains(rect.leftBottom()))
        {
            return(true);
        }

        for (int i = 0; i < polygon.mPts.Count; ++i)
        {
            Vector2 pt1 = polygon.mPts[i];
            Vector2 pt2 = polygon.mPts[((i + 1) % polygon.mPts.Count)];

            // 多边形有顶点在矩形内 一定相交
            if (rect.contains(pt1))
            {
                return(true);
            }

            // 多边形与矩形有边相交 两图形一定相交
            if (Utility.isSegmentIntersect(pt1, pt2, rect.leftTop(), rect.rightTop()))
            {
                return(true);
            }

            if (Utility.isSegmentIntersect(pt1, pt2, rect.rightTop(), rect.rightBottom()))
            {
                return(true);
            }

            if (Utility.isSegmentIntersect(pt1, pt2, rect.rightBottom(), rect.leftBottom()))
            {
                return(true);
            }

            if (Utility.isSegmentIntersect(pt1, pt2, rect.leftBottom(), rect.leftTop()))
            {
                return(true);
            }
        }

        return(false);
    }
    // 解析Zones
    private void ParseZones(XmlNode node)
    {
        XmlNodeList nodeList = node.ChildNodes;

        for (int i = 0; i < nodeList.Count; ++i)
        {
            XmlNode childNode = nodeList[i];
            if (childNode != null && childNode.Name == "Zone")
            {
                int type = System.Convert.ToInt32(childNode.Attributes["type"].Value);
                if ((Zone.ZoneType)type == Zone.ZoneType.ZoneType_Invalid || !System.Enum.IsDefined(typeof(Zone.ZoneType), type))
                {
                    continue;
                }

                Zone zone = new Zone();
                zone.type = (Zone.ZoneType)type;
                zone.name = childNode.Attributes["name"].Value;

                ShapeType shapetype = (ShapeType)(System.Convert.ToInt32(childNode.Attributes["shapeType"].Value));
                if (shapetype == ShapeType.ShapeType_Round)
                {
                    zone.shape = new SceneShapeRound();
                    SceneShapeRound round = zone.shape as SceneShapeRound;
                    round.mCenter.x = (float)System.Convert.ToDouble(childNode.Attributes["x"].Value);
                    round.mCenter.y = (float)System.Convert.ToDouble(childNode.Attributes["y"].Value);
                    round.mRadius   = (float)System.Convert.ToDouble(childNode.Attributes["r"].Value);
                }
                else if (shapetype == ShapeType.ShapeType_Rect)
                {
                    zone.shape = new SceneShapeRect();
                    SceneShapeRect rect = zone.shape as SceneShapeRect;
                    rect.mLeft   = (float)System.Convert.ToDouble(childNode.Attributes["x1"].Value);
                    rect.mTop    = (float)System.Convert.ToDouble(childNode.Attributes["y1"].Value);
                    rect.mRight  = (float)System.Convert.ToDouble(childNode.Attributes["x2"].Value);
                    rect.mBottom = (float)System.Convert.ToDouble(childNode.Attributes["y2"].Value);
                }
                else if (shapetype == ShapeType.ShapeType_Polygon)
                {
                    zone.shape = new SceneShapePolygon();
                    SceneShapePolygon polygon = zone.shape as SceneShapePolygon;

                    int          index   = 1;
                    XmlAttribute attribX = null;
                    XmlAttribute attribY = null;

                    while ((attribX = childNode.Attributes["x" + index.ToString()]) != null && (attribY = childNode.Attributes["y" + index.ToString()]) != null)
                    {
                        polygon.PushbackVector2(new Vector2((float)System.Convert.ToDouble(attribX.Value), (float)System.Convert.ToDouble(attribY.Value)));
                        index++;
                    }
                }

                mZones.Add(zone.name, zone);
            }
        }
    }
示例#4
0
    public SceneShapePolygon(SceneShapePolygon polygon)
    {
        mPts.Clear();

        foreach (Vector2 pt in polygon.mPts)
        {
            mPts.Add(pt);
        }
    }
示例#5
0
    // 根据形状参数、位置、弧度 创建形状
    static public SceneShape Create(SceneShapeParam param, Vector2 pos, float radians)
    {
        if (param.mType == ShapeType.ShapeType_Round)
        {
            if (param == null || param.mParams == null || param.mParams.Count < 1)
            {
                return(null);
            }

            return(new SceneShapeRound(pos, param.mParams[0]));
        }
        else if (param.mType == ShapeType.ShapeType_Rect)
        {
            if (param == null || param.mParams == null || param.mParams.Count < 2)
            {
                return(null);
            }

            if ((int)(radians * 100000) % (int)(Mathf.PI * 100000 * 2) == 0)
            {
                if ((int)(radians * 100000) % (int)(Mathf.PI * 100000) == 0)
                {
                    return(new SceneShapeRect(pos, param.mParams[0], param.mParams[1]));
                }

                return(new SceneShapeRect(pos, param.mParams[1], param.mParams[0]));
            }
            else
            {
                SceneShapeRect rect = new SceneShapeRect(pos, param.mParams[0], param.mParams[1]);

                return(rotate(rect, pos, radians * Mathf.Rad2Deg));
            }
        }
        else if (param.mType == ShapeType.ShapeType_Polygon)
        {
            if (param == null || param.mParams == null || param.mParams.Count < 2 || param.mParams.Count % 2 != 0)
            {
                return(null);
            }

            SceneShapePolygon shape = new SceneShapePolygon();
            for (int i = 0; i < (int)(param.mParams.Count * 0.5); ++i)
            {
                shape.PushbackVector2(new Vector2(param.mParams[2 * i], param.mParams[2 * i + 1]));
            }

            return(rotate(ref shape, pos, radians * Mathf.Rad2Deg));
        }

        GameDebug.LogError("SceneShape.Creat() 未知的图形类别");

        return(null);
    }
示例#6
0
    static public bool overlap(SceneShapeLine shape1, SceneShapePolygon shape2)
    {
        LineSegf line = new LineSegf(new Vector2f(shape1.mVertex1.x, shape1.mVertex1.y), new Vector2f(shape1.mVertex2.x, shape1.mVertex2.y));

        for (int i = 1; i < shape2.mPts.Count; i++)
        {
            Vector2f p1 = new Vector2f(shape2.mPts[i - 1].x, shape2.mPts[i - 1].y);
            Vector2f p2 = new Vector2f(shape2.mPts[i].x, shape2.mPts[i].y);

            if (Geometryalgorithm2d.lineseg_intersect_lineseg(line, new LineSegf(p1, p2)))
            {
                return(true);
            }
        }

        return(false);
    }
示例#7
0
    // 多边形与多边形重叠(相交或包含)
    static public bool overlap(SceneShapePolygon polygon1, SceneShapePolygon polygon2)
    {
        for (int i = 0; i < polygon1.mPts.Count; ++i)
        {
            for (int j = i + 1; j < polygon1.mPts.Count; ++j)
            {
                for (int s = 0; s < polygon2.mPts.Count; ++s)
                {
                    for (int t = s + 1; t < polygon2.mPts.Count; ++t)
                    {
                        if (Utility.isSegmentIntersect(polygon1.mPts[i], polygon1.mPts[j], polygon2.mPts[s], polygon2.mPts[t]))
                        {
                            return(false);
                        }
                    }
                }
            }
        }

        return(!polygon2.contains(polygon1.mPts[polygon1.mPts.Count - 1]) && !polygon1.contains(polygon2.mPts[polygon2.mPts.Count - 1]));
    }
示例#8
0
    // 圆与多边形重叠(相交或包含)
    static public bool overlap(SceneShapeRound round, SceneShapePolygon polygon)
    {
        if (polygon.contains(round.mCenter))
        {
            return(true);
        }

        for (int i = 0; i < polygon.mPts.Count; ++i)
        {
            Vector2 pt1 = polygon.mPts[i];
            Vector2 pt2 = polygon.mPts[((i + 1) % polygon.mPts.Count)];

            float minDist = Utility.minDist(round.mCenter, pt1, pt2);

            if (minDist <= round.mRadius)
            {
                return(true);
            }
        }

        return(false);
    }
示例#9
0
 // 旋转图形
 static public SceneShapePolygon rotate(ref SceneShapePolygon polygon, Vector2 pos, float angles)
 {
     //SceneShapePolygon newPolygon = new SceneShapePolygon(polygon);
     //return newPolygon.rotate(pos, angles);
     return(polygon.rotate(pos, angles));
 }