/// <summary> /// 获得物体所在Quadtree节点 /// </summary> /// <param name="pRect"></param> /// <returns></returns> private Quadtree RetrieveQt(MGFObject pRect) { Quadtree qt = this; int index = GetIndex(pRect); if (index != -1 && nodes[0] != null) { qt = nodes[index].RetrieveQt(pRect); } return(qt); }
/// <summary> /// 分裂树 /// </summary> private void Split() { Fix64 subWidth = rect.halfSize.X / (Fix64)2; Fix64 subHeight = rect.halfSize.Y / (Fix64)2; Fix64Vector2 subSize = new Fix64Vector2(subWidth, subHeight); Fix64 X = rect.center.X; Fix64 Y = rect.center.Y; nodes[0] = new Quadtree(_level + 1, new Rectangle(new Fix64Vector2(X + subWidth, Y + subHeight), subSize)); nodes[1] = new Quadtree(_level + 1, new Rectangle(new Fix64Vector2(X - subWidth, Y + subHeight), subSize)); nodes[2] = new Quadtree(_level + 1, new Rectangle(new Fix64Vector2(X - subWidth, Y - subHeight), subSize)); nodes[3] = new Quadtree(_level + 1, new Rectangle(new Fix64Vector2(X + subWidth, Y - subHeight), subSize)); }
/// <summary> /// 物体发生移动时调用 /// </summary> /// <param name="pRect"></param> internal void Move(MGFObject pRect) { Quadtree now = RetrieveQt(pRect); //计算该物体目前所在的位置是否与前一帧所在位置一致 if (!dic.ContainsKey(pRect) || dic[pRect] == null) { return; } if (now != dic[pRect]) { //不一致时重新插入物体 dic[pRect].Remove(pRect); now.Insert(pRect); } }
/// <summary> /// 物体发生移动时调用 /// </summary> /// <param name="pRect"></param> internal bool Move(MGFObject pRect) { Quadtree now = RetrieveQt(pRect); if (!dic.ContainsKey(pRect) || dic[pRect] == null) { return(false); } //计算该物体目前所在的节点是否与前一帧所在位置一致 if (now != dic[pRect]) { //不一致时重新插入物体 dic[pRect].Remove(pRect); now.Insert(pRect); return(true); } else { return(false); } }
/// <summary> /// 初始化 /// </summary> /// <param name="mgfList"></param> /// <param name="planePos"></param> public void Init(MGFObject[] mgfList, Rectangle planePos) { m_Qt = new Quadtree(0, planePos); if (mgfList != null && mgfList.Length != 0) { for (int i = 0; i < mgfList.Length; i++) { mgfList[i].Init(); mgfList[i].MGFEnable(); m_Objects.Add(mgfList[i]); if (mgfList[i].IsCollisionAble != false) { m_Qt.Insert(mgfList[i]); } if (mgfList[i].IsStatic == false) { m_DynamicObjs.Add(mgfList[i]); } } } }