/// <summary> /// Add the 4 child quad nodes. /// </summary> private void AddChildren() { //Add top left (northwest) child ChildTopLeft = new QuadNode(NodeType.TopLeft, (_nodeSize / 2), _nodeDepth + 1, this, _parentTree, VertexTopLeft.Index); //Add top right (northeast) child ChildTopRight = new QuadNode(NodeType.TopRight, (_nodeSize / 2), _nodeDepth + 1, this, _parentTree, VertexTop.Index); //Add bottom left (southwest) child ChildBottomLeft = new QuadNode(NodeType.BottomLeft, (_nodeSize / 2), _nodeDepth + 1, this, _parentTree, VertexLeft.Index); //Add bottom right (southeast) child ChildBottomRight = new QuadNode(NodeType.BottomRight, (_nodeSize / 2), _nodeDepth + 1, this, _parentTree, VertexCenter.Index); HasChildren = true; }
/// <summary> /// QuadNode constructor /// </summary> /// <param name="nodeType">Type of node.</param> /// <param name="nodeSize">Width/Height of node (# of vertices across - 1).</param> /// <param name="nodeDepth">Depth of current node</param> /// <param name="parent">Parent QuadNode</param> /// <param name="parentTree">Top level Tree.</param> /// <param name="positionIndex">Index of top left Vertice in the parent tree Vertices array</param> public QuadNode(NodeType nodeType, int nodeSize, int nodeDepth, QuadNode parent, QuadTree parentTree, int positionIndex) { NodeType = nodeType; _nodeSize = nodeSize; _nodeDepth = nodeDepth; _positionIndex = positionIndex; _parent = parent; _parentTree = parentTree; //Add the 9 vertices AddVertices(); Bounds = new BoundingBox(_parentTree.Vertices[VertexTopLeft.Index].Position, _parentTree.Vertices[VertexBottomRight.Index].Position); Bounds.Min.Y = -950f; Bounds.Max.Y = 950f; if (nodeSize >= 4) { AddChildren(); } //Make call to UpdateNeighbors from the parent node. //This will update all neighbors recursively for the //children as well. This ensures all nodes are created //prior to updating neighbors. if (_nodeDepth == 1) { AddNeighbors(); VertexTopLeft.Activated = true; VertexTopRight.Activated = true; VertexCenter.Activated = true; VertexBottomLeft.Activated = true; VertexBottomRight.Activated = true; } }
/// <summary> /// Update reference to neighboring quads /// </summary> private void AddNeighbors() { switch (NodeType) { case NodeType.TopLeft: //Top Left Corner //Top neighbor if (_parent.NeighborTop != null) { NeighborTop = _parent.NeighborTop.ChildBottomLeft; } //Right neighbor NeighborRight = _parent.ChildTopRight; //Bottom neighbor NeighborBottom = _parent.ChildBottomLeft; //Left neighbor if (_parent.NeighborLeft != null) { NeighborLeft = _parent.NeighborLeft.ChildTopRight; } break; case NodeType.TopRight: //Top Right Corner //Top neighbor if (_parent.NeighborTop != null) { NeighborTop = _parent.NeighborTop.ChildBottomRight; } //Right neighbor if (_parent.NeighborRight != null) { NeighborRight = _parent.NeighborRight.ChildTopLeft; } //Bottom neighbor NeighborBottom = _parent.ChildBottomRight; //Left neighbor NeighborLeft = _parent.ChildTopLeft; break; case NodeType.BottomLeft: //Bottom Left Corner //Top neighbor NeighborTop = _parent.ChildTopLeft; //Right neighbor NeighborRight = _parent.ChildBottomRight; //Bottom neighbor if (_parent.NeighborBottom != null) { NeighborBottom = _parent.NeighborBottom.ChildTopLeft; } //Left neighbor if (_parent.NeighborLeft != null) { NeighborLeft = _parent.NeighborLeft.ChildBottomRight; } break; case NodeType.BottomRight: //Bottom Right Corner //Top neighbor NeighborTop = _parent.ChildTopRight; //Right neighbor if (_parent.NeighborRight != null) { NeighborRight = _parent.NeighborRight.ChildBottomLeft; } //Bottom neighbor if (_parent.NeighborBottom != null) { NeighborBottom = _parent.NeighborBottom.ChildTopRight; } //Left neighbor NeighborLeft = _parent.ChildBottomLeft; break; } if (this.HasChildren) { ChildTopLeft.AddNeighbors(); ChildTopRight.AddNeighbors(); ChildBottomLeft.AddNeighbors(); ChildBottomRight.AddNeighbors(); } }