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
+10
View File
@@ -4,6 +4,8 @@
#include "Core/AABB.h"
struct Ray;
class World;
class Camera;
class OctTree
{
@@ -23,6 +25,9 @@ public:
OctTree(const OctTree&& other) = delete;
OctTree& operator= (const OctTree& other) = delete;
//Collision test function. WTODO: Probably remove or relocate elsewhere, Collision system?
void Update(float dt, World* world, Camera* cam);
void AddDynamicObject(const AABB& box);
void AddStaticObject(const AABB& box);
@@ -47,6 +52,11 @@ private:
std::vector<AABB> m_DynamicObjects;
AABB m_Box;
bool m_UpdatedOnce;
unsigned int m_BoxID;
glm::vec3 m_PrevPos;
glm::quat m_PrevOri;
inline bool hasChildren() const;
int childIndexContainingPoint(const glm::vec3& point) const;
std::vector<int> childIndicesContainingBox(const AABB& box) const;
+3
View File
@@ -10,6 +10,8 @@
#include "Core/World.h"
#include "Rendering/RenderQueueFactory.h"
class OctTree;
class Game
{
public:
@@ -28,6 +30,7 @@ private:
GUI::Frame* m_FrameStack;
World* m_World;
RenderQueueFactory* m_RenderQueueFactory;
OctTree* m_OctTree;
};
#endif
+36
View File
@@ -42,6 +42,11 @@ private:
f.AddProperty("Color", glm::vec4(1.f, 1.f, 1.f, 1.f));
f.AddProperty("Visible", true);
RegisterComponent(f);
f = ComponentWrapperFactory("Collision");
f.AddProperty("BoxCenter", glm::vec3(0.f, 0.f, 0.f));
f.AddProperty("BoxSize", glm::vec3(1.f, 1.f, 1.f));
RegisterComponent(f);
}
void createTestEntities()
@@ -103,6 +108,37 @@ private:
ComponentWrapper model = world.AttachComponent(entityDummyScene, "Model");
model["Resource"] = "Models/DummyScene.obj";
}
{
EntityID entityCollisionBox = world.CreateEntity();
ComponentWrapper transform = world.AttachComponent(entityCollisionBox, "Transform");
transform["Position"] = glm::vec3(0.f, 2.f, 0.f);
ComponentWrapper model = world.AttachComponent(entityCollisionBox, "Model");
model["Resource"] = "Models/Core/UnitBox.obj";
ComponentWrapper collision = world.AttachComponent(entityCollisionBox, "Collision");
glm::vec3 pos = transform["Position"];
glm::vec3 scale = transform["Scale"];
glm::quat ori = transform["Orientation"];
Model* modelRes = ResourceManager::Load<Model>(model["Resource"]);
//WTODO: This only works for objects that never moves/scales/rotates since modelMatrix don't change.
//For dynamic objects, should save a collision box in modelspace and transform box with modelMatrix per collision check.
glm::mat4 modelMatrix = modelRes->m_Matrix * glm::translate(glm::mat4(), pos) * glm::toMat4(ori) * glm::scale(scale);
glm::vec3 mini = glm::vec3(INFINITY, INFINITY, INFINITY);
glm::vec3 maxi = glm::vec3(-INFINITY, -INFINITY, -INFINITY);
for (const auto& v : modelRes->m_Vertices) {
const auto& wPos = modelMatrix * glm::vec4(v.Position.x, v.Position.y, v.Position.z, 1);
maxi.x = std::max(wPos.x, maxi.x);
maxi.y = std::max(wPos.y, maxi.y);
maxi.z = std::max(wPos.z, maxi.z);
mini.x = std::min(wPos.x, mini.x);
mini.y = std::min(wPos.y, mini.y);
mini.z = std::min(wPos.z, mini.z);
}
collision["BoxCenter"] = 0.5f * (maxi + mini);
collision["BoxSize"] = maxi - mini;
}