Beispiel #1
0
        /**
         * @brief create a BoundingBox and its children from XML node
         *
         * @param node the XML node
         * @param scene the scene it belongs to
         * @param gameObject the gameObject it belong to
         *
         * @result CatsBoundingBox
         * */
        public static CatsBoundingBox LoadFromNode(XmlNode node, Scene scene, GameObject gameObject)
        {
            XmlElement boundingBox = (XmlElement)node;

            bool isRectangle = bool.Parse(boundingBox.GetAttribute("isRectangle"));

            // heightRange
            XmlElement heightRange    = (XmlElement)boundingBox.SelectSingleNode("HeightRange");
            Vector2    newHeightRange = new Vector2(float.Parse(heightRange.GetAttribute("min")),
                                                    float.Parse(heightRange.GetAttribute("max")));
            CatsBoundingBox newBoundingBox = new CatsBoundingBox();

            newBoundingBox.m_heightRange = newHeightRange;
            newBoundingBox.m_isRectangle = isRectangle;

            if (isRectangle)
            {
                // XBound
                XmlElement xBound = (XmlElement)boundingBox.SelectSingleNode("XBound");
                newBoundingBox.m_XBound = new Vector2(float.Parse(xBound.GetAttribute("min")),
                                                      float.Parse(xBound.GetAttribute("max")));
                // YBound
                XmlElement yBound = (XmlElement)boundingBox.SelectSingleNode("YBound");
                newBoundingBox.m_YBound = new Vector2(float.Parse(yBound.GetAttribute("min")),
                                                      float.Parse(yBound.GetAttribute("max")));

                newBoundingBox.UpdateBoundFromRectangle();
            }

            // TODO: sub boundingBox
            return(newBoundingBox);
        }
Beispiel #2
0
        /**
         * @brief judge whether this boundingBox collide with other
         *
         * @param boundingBox the other boundingBox we want to test
         * @param XYOffset the offset of the other boundingBox on XY
         * @param ZOffset the offset of the other boundingBox on Z
         * @param iXYOffset the offset of this boundingBox on XY
         * @param iZOffset the offset of this boundingBox on Z
         *
         * @result whether they collide
         * */
        public bool IsCollide(CatsBoundingBox boundingBox,
                              Vector2 XYOffset, float ZOffset,
                              Vector2 iXYOffset, float iZOffset)
        {
            // test Collide on Z
            Vector2 iZRange = new Vector2(iZOffset, iZOffset) + m_heightRange;
            Vector2 ZRange  = new Vector2(ZOffset, ZOffset) + boundingBox.m_heightRange;

            if (iZRange.X > ZRange.Y || iZRange.Y < ZRange.X)
            {
                // no need to do further test if no contact on Z
                return(false);
            }

            // test Collide on XYPlane
            // do the separation axes algorithm
            // potential separation axes. they get converted into push
            List <Vector2> axis = new List <Vector2>();

            // max of 16 vertexes per polygon
            int i, j;

            for (j = m_convex.Length - 1, i = 0;
                 i < m_convex.Length; j = i, i++)
            {
                Vector2 edge   = m_convex[i] - m_convex[j];
                Vector2 normal = new Vector2(-edge.Y, edge.X);
                axis.Add(normal);

                if (AxisSeparatePolygons(normal, m_convex, boundingBox.m_convex,
                                         iXYOffset, iZOffset, XYOffset, ZOffset))
                {
                    return(false);
                }
            }

            for (j = boundingBox.m_convex.Length - 1, i = 0;
                 i < boundingBox.m_convex.Length; j = i, i++)
            {
                Vector2 edge   = boundingBox.m_convex[i] - boundingBox.m_convex[j];
                Vector2 normal = new Vector2(-edge.Y, edge.X);
                axis.Add(normal);

                if (AxisSeparatePolygons(normal, m_convex, boundingBox.m_convex,
                                         iXYOffset, iZOffset, XYOffset, ZOffset))
                {
                    return(false);
                }
            }

            /*
             * // find the MTD among all the separation vectors
             * MTD = FindMTD(Axis, iNumAxis);
             *
             * // makes sure the push vector is pushing A away from B
             * Vector D = A.Position – B.Position;
             * if (D dot MTD < 0.0f)
             *  MTD = -MTD;
             */
            // this level collide, check sub boundingbox
            if (m_subBounding != null && m_subBounding.Count > 0)
            {
                foreach (CatsBoundingBox bounding in m_subBounding)
                {
                    bool result = bounding.IsCollide(boundingBox,
                                                     XYOffset, ZOffset,
                                                     iXYOffset, iZOffset);
                    if (result == true)
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else
            {
                return(true);
            }
        }
Beispiel #3
0
 public void AddChildBoundingBox(CatsBoundingBox boundingBox)
 {
     m_subBounding.Add(boundingBox);
 }