public OCTree(int maxObjectPerNode, Vector3 center, Vector3 extents) { Root = new Node(this, null, center, extents, 0); Root.Divide(); MaxObjectsPerNode = maxObjectPerNode; }
internal Node(OCTree tree, Node parent, Vector3 center, Vector3 extents, int level) { Tree = tree; Center = center; Extents = extents; Objects = new List<Collidable>(); Box = new BoundingBox(center - extents, center + extents); Level = level; Parent = parent; }
internal void Remove() { if (Node == null) return; Node.Objects.Remove(this); while (Node != null) { Node.Count -= 1; Node = Node.Parent; } }
public void Clear() { Root = new Node(this, null, Root.Center, Root.Extents, 0); Root.Divide(); }
internal void Divide() { var x2 = Extents.X / 2; var y2 = Extents.Y / 2; var z2 = Extents.Z / 2; var extents = new Vector3(x2, y2, z2); var right = Center.X + x2; var left = Center.X - x2; var top = Center.Y + y2; var bottom = Center.Y - y2; var front = Center.Z + z2; var back = Center.Z - z2; var l = Level+1; Children = new Node[8]; Children[LEFT_TOP_FRONT] = new Node(Tree, this, new Vector3(left, top, front), extents, l); Children[RIGHT_TOP_FRONT] = new Node(Tree, this, new Vector3(right, top, front), extents, l); Children[LEFT_TOP_BACK] = new Node(Tree, this, new Vector3(left, top, back), extents, l); Children[RIGHT_TOP_BACK] = new Node(Tree, this, new Vector3(right, top, back), extents, l); Children[LEFT_BOTTOM_FRONT] = new Node(Tree, this, new Vector3(left, bottom, front), extents, l); Children[RIGHT_BOTTOM_FRONT] = new Node(Tree, this, new Vector3(right, bottom, front), extents, l); Children[LEFT_BOTTOM_BACK] = new Node(Tree, this, new Vector3(left, bottom, back), extents, l); Children[RIGHT_BOTTOM_BACK] = new Node(Tree, this, new Vector3(right, bottom, back), extents, l); }