Dragonfly 4.20
A text-based game engine
QuadtreeNode.h
1//
2// QuadtreeNode.h - A Quadtree node class.
3//
4
5#ifndef __QUADTREENODE_H__
6#define __QUADTREENODE_H__
7
8#include "Object.h"
9#include "ObjectList.h"
10
11namespace df {
12
13const int QUADTREE_THRESH_DEFAULT = 1;
14const int QUADTREE_MAX_DEPTH = 10;
15
17
18 private:
19 int m_depth; ///< Depth of this node.
20 Box m_boundary; ///< Bounding box of this node.
21 QuadtreeNode *m_p_parent; ///< Parent node (NULL if root).
22 ObjectList m_objects; ///< Objects contained in this node.
23 bool m_split; ///< True if already split children.
24 QuadtreeNode *m_p_child[4]; ///< Child nodes (NULL when none).
25
26 // Helper function.
27 // Return all Objects that collide at location where. Recurse in
28 // delta direction (-1 up, 0 stay, +1 down). If self is true, add
29 // collisions with self node. List is empty if none.
30 ObjectList getCollisions(Object* p_o, Vector where, int delta,
31 bool self) const;
32
33public:
34
35 /// Construct new node with given parent, depth and boundary.
36 QuadtreeNode(QuadtreeNode *p_parent, int depth, Box boundary);
37
38 /// Destructor, free up child nodes.
40
41 /// Return list of Objects at this node.
43
44 /// Get boundary.
45 Box getBoundary() const;
46
47 /// Get depth.
48 int getDepth() const;
49
50 /// Insert Object at this node and split if count > threshold.
51 /// Return 0 if ok, else -1.
53
54 /// Remove Object from this node.
55 /// Return 0 if ok (found), else -1.
57
58 /// Split this node if it hasn't already.
59 /// Return 0 if ok, else -1.
60 int split();
61
62 /// Return node in hierarchy where Object should be placed.
63 /// Return NULL if error.
65
66 /// Return node in hierarchy where Object should be placed
67 /// if moved to 'where'.
68 /// Return NULL if error.
70
71 /// Return node in hierarchy where Object should be placed
72 /// if has new bounding box.
73 /// Return NULL if error.
75
76 /// Return all Objects that collide at given location.
77 /// List is empty if none.
79
80 /// Return current node as string.
81 std::string toString() const;
82
83#ifdef DEBUG_QUADTREE
84 /// Write node and descendants to logfile.
85 void print(std::string label="") const;
86
87 /// Draw self and descendants.
88 void draw();
89#endif
90};
91
92} // end of namespace df
93
94#endif // __QUADTREENODE_H__
Definition: Box.h:16
Definition: ObjectList.h:15
Definition: Object.h:57
Definition: QuadtreeNode.h:16
QuadtreeNode * findOwner(Object *p_o, Box b)
Return node in hierarchy where Object should be placed if has new bounding box.
ObjectList m_objects
Objects contained in this node.
Definition: QuadtreeNode.h:22
ObjectList getCollisions(Object *p_o, Vector where) const
Return all Objects that collide at given location.
bool m_split
True if already split children.
Definition: QuadtreeNode.h:23
QuadtreeNode * findOwner(Object *p_o)
Return node in hierarchy where Object should be placed.
ObjectList getObjects() const
Return list of Objects at this node.
QuadtreeNode(QuadtreeNode *p_parent, int depth, Box boundary)
Construct new node with given parent, depth and boundary.
int insertObject(Object *p_o)
Insert Object at this node and split if count > threshold.
QuadtreeNode * findOwner(Object *p_o, Vector where)
Return node in hierarchy where Object should be placed if moved to 'where'.
int getDepth() const
Get depth.
QuadtreeNode * m_p_child[4]
Child nodes (NULL when none).
Definition: QuadtreeNode.h:24
Box getBoundary() const
Get boundary.
~QuadtreeNode()
Destructor, free up child nodes.
int split()
Split this node if it hasn't already.
int removeObject(Object *p_o)
Remove Object from this node.
Box m_boundary
Bounding box of this node.
Definition: QuadtreeNode.h:20
std::string toString() const
Return current node as string.
int m_depth
Depth of this node.
Definition: QuadtreeNode.h:19
QuadtreeNode * m_p_parent
Parent node (NULL if root).
Definition: QuadtreeNode.h:21
Definition: Vector.h:12
An animation for a sprite.
Definition: Animation.h:15