Skip to content

Instantly share code, notes, and snippets.

@Shaptic
Last active December 15, 2015 11:59
Show Gist options
  • Save Shaptic/5257421 to your computer and use it in GitHub Desktop.
Save Shaptic/5257421 to your computer and use it in GitHub Desktop.
Bare-bones quad tree node structure.
#include <list>
#include <cstdint>
// A generic object class (supporting collision) that is within the nodes.
// See here: https://github.com/Ruskiy69/IronClad/blob/master/Source/include/Entity/RigidBody.hpp
// For a real definition of the objects I use.
class CQuadTree;
class CEntity;
// This is a rectangle representation containing x, y position, and
// w, h dimensions. Something barebones could be this:
struct rect_t { float x, y; uint32_t w, h; };
// The quad tree node.
struct QTNode
{
QTNode* pParent;
QTNode** pChildren;
rect_t Rect;
uint16_t depth;
std::list<CEntity*> nodeObjects;
};
// This defines a BAREBONES entity class. This is nowhere
// near complete, and has only the essential members
// necessary for the quad tree to update it.
class CEntity
{
public:
CEntity() : pCurrentNode(nullptr), needs_update(false) {}
// You should know how to do this :)
bool CheckCollision(const rect_t& Other);
// The class needs access to the private data members.
friend class CQuadTree;
private:
QTNode* pCurrentNode;
bool needs_update; // The entity should track if it has moved
// since the last frame. For example, if
// CEntity::Adjust() is called with non-zero values.
float x, y;
rect_t Dimensions;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment