Added Update call in Game to test OctTree by using collision.

This commit is contained in:
William Moberg
2015-12-09 13:52:17 +01:00
parent bad42fae1c
commit 14bcb7ea0d
5 changed files with 101 additions and 0 deletions
+43
View File
@@ -4,6 +4,8 @@
#include "Core/OctTree.h"
#include "Core/Collision.h"
#include "Core/World.h"
#include "Rendering/Camera.h"
namespace
{
@@ -27,6 +29,7 @@ OctTree::OctTree()
OctTree::OctTree(const AABB& octTreeBounds, int subDivisions)
: m_Box(octTreeBounds)
, m_UpdatedOnce(false)
{
if (subDivisions == 0) {
for (OctTree*& c : m_Children) {
@@ -80,6 +83,46 @@ OctTree::~OctTree()
}
}
void OctTree::Update(float dt, World* world, Camera* cam)
{
AABB aabb;
for (ComponentWrapper& c : world->GetComponents("Collision")) {
aabb.CreateFromCenter(c["BoxCenter"], c["BoxSize"]);
AddStaticObject(aabb);
}
const glm::vec4 redCol = glm::vec4(1, 0.2f, 0, 1);
const glm::vec4 greenCol = glm::vec4(0.1f, 1.0f, 0.25f, 1);
const glm::vec3 boxSize = 0.1f*glm::vec3(1.0f, 1.0f, 1.0f);
if (!m_UpdatedOnce) {
m_BoxID = world->CreateEntity();
ComponentWrapper transform = world->AttachComponent(m_BoxID, "Transform");
transform["Scale"] = boxSize;
ComponentWrapper model = world->AttachComponent(m_BoxID, "Model");
model["Resource"] = "Models/Core/UnitBox.obj";
m_UpdatedOnce = true;
}
AABB box;
auto boxPos = cam->Position() + 1.2f*cam->Forward();
box.CreateFromCenter(boxPos, boxSize);
ComponentWrapper transform = world->GetComponent(m_BoxID, "Transform");
transform["Position"] = boxPos;
ComponentWrapper model = world->GetComponent(m_BoxID, "Model");
//if (BoxCollides(box, AABB())) {
if (Collision::AABBVsAABB(box, aabb)) {
cam->SetPosition(m_PrevPos);
cam->SetOrientation(m_PrevOri);
model["Color"] = greenCol;
} else {
model["Color"] = redCol;
}
m_PrevPos = cam->Position();
m_PrevOri = cam->Orientation();
ClearObjects();
}
bool OctTree::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const
{
if (hasChildren()) {