Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/AABB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "AABB.h"

AABB::AABB(float minX, float minY, float maxX, float maxY)
{
m_minX = minX;
m_minY = minY;
m_maxX = maxX;
m_maxY = maxY;
}

AABB::~AABB(void)
{
}

bool AABB::containsPoint(glm::vec4 point)
{
return (point.x >= m_minX && point.x < m_maxX && point.y >= m_minY && point.y < m_maxY);
}

AABB *AABB::northWest(void)
{
return new AABB(m_minX, m_maxY / 2.0f, m_maxX / 2.0f, m_maxY);
}

AABB *AABB::northEast(void)
{
return new AABB(m_maxX / 2.0f, m_maxY / 2.0f, m_maxX, m_maxY);
}

AABB *AABB::southEast(void)
{
return new AABB(m_maxX / 2.0f, m_minY, m_maxX, m_maxY / 2.0f);
}

AABB *AABB::southWest(void)
{
return new AABB(m_minX, m_minY, m_maxX / 2.0f, m_maxY / 2.0f);
}

27 changes: 27 additions & 0 deletions src/AABB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef AABB_H
#define AABB_H

#define GLM_SWIZZLE
#include <glm/glm.hpp>

class AABB
{
public:
AABB(float minX, float minY, float maxX, float maxY);
~AABB(void);

bool containsPoint(glm::vec4 point);

AABB *northWest(void);
AABB *northEast(void);
AABB *southEast(void);
AABB *southWest(void);

private:
float m_minX;
float m_minY;
float m_maxX;
float m_maxY;
};

#endif
42 changes: 42 additions & 0 deletions src/QuadTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "QuadTree.h"

QuadTree::QuadTree(AABB *aabb)
{
northWest = nullptr;
northEast = nullptr;
southEast = nullptr;
southWest = nullptr;
m_entity = nullptr;

m_aabb = aabb;
}

QuadTree::~QuadTree(void)
{
delete m_aabb;

if (northWest != nullptr) {
delete northWest;
delete northEast;
delete southEast;
delete southWest;
}
}

bool QuadTree::insert(Entity *entity)
{
if (!m_aabb->containsPoint(entity->getPosition()))
return false;

if (m_entity == nullptr) {
m_entity = entity;
return true;
}

northWest = new QuadTree(m_aabb->northWest());
northEast = new QuadTree(m_aabb->northEast());
southEast = new QuadTree(m_aabb->southEast());
southWest = new QuadTree(m_aabb->southWest());

return (northWest->insert(entity) || northEast->insert(entity) || southWest->insert(entity) || southEast->insert(entity));
}
26 changes: 26 additions & 0 deletions src/QuadTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef QUAD_TREE_H
#define QUAD_TREE_H

#include "Entity.h"
#include "AABB.h"

class QuadTree
{
public:
QuadTree(AABB *aabb);
~QuadTree(void);

bool insert(Entity *entity);

private:
QuadTree* northWest;
QuadTree* northEast;
QuadTree* southEast;
QuadTree* southWest;

Entity *m_entity;

AABB *m_aabb;
};

#endif
5 changes: 4 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
#include "components/PointLight.h"
#include "components/Sphere.h"

#include "QuadTree.h"

#include "Plane.h"
#include "Mesh.h"
#include "Texture.h"
#include "Logger.h"
#include "MeshLoader.h"


class CoolGame : public Game
{
public:
Expand All @@ -37,6 +38,8 @@ void CoolGame::update(int delta)

void CoolGame::init(GLManager *glManager)
{
QuadTree *qt = new QuadTree(new AABB(0, 0, 10, 10));

Entity *plane = new Entity();
plane->addComponent(new MeshRenderer((new Plane())->getMesh(), new Material(new Texture(Asset("bricks2.jpg")), new Texture(Asset("bricks2_normal.jpg")), new Texture(Asset("bricks2_specular.png")))));
plane->getTransform().setPosition(glm::vec3(0, -2, 0));
Expand Down