TriggerSystem implemented and working.

This commit is contained in:
William Moberg
2015-12-16 12:22:15 +01:00
parent 99b76d7ae1
commit d10df6ae09
5 changed files with 75 additions and 54 deletions
+6 -48
View File
@@ -3,7 +3,7 @@
#include "Core/AABB.h"
#include "Rendering/Model.h"
void TriggerSystem::Update(World* world, ComponentWrapper& trigger, double dt)
void TriggerSystem::UpdateComponent(World* world, ComponentWrapper& trigger, double dt)
{
//Currently only players can trigger things.
auto players = world->GetComponents("Player");
@@ -13,14 +13,14 @@ void TriggerSystem::Update(World* world, ComponentWrapper& trigger, double dt)
EntityID tId = trigger.EntityID;
AABB triggerBox;
//The trigger *should* have a bounding box, or something, to test against so it can be triggered.
if (!getEntityBox(world, tId, triggerBox)) {
if (!Collision::GetEntityBox(world, tId, triggerBox, true)) {
return;
}
for (auto& pc : *players) {
EntityID pId = pc.EntityID;
AABB playerBox;
//The player can't trigger anything without an AABB.
if (!getEntityBox(world, pId, playerBox)) {
if (!Collision::GetEntityBox(world, pId, playerBox, true)) {
continue;
}
if (!Collision::AABBVsAABB(triggerBox, playerBox)) {
@@ -35,8 +35,9 @@ void TriggerSystem::Update(World* world, ComponentWrapper& trigger, double dt)
} else {
//Entity is at least touching the trigger.
AABB completelyInsideBox;
completelyInsideBox.CreateFromCenter(triggerBox.Center(), triggerBox.Size() - playerBox.Size());
if (Collision::AABBVsAABB(completelyInsideBox, playerBox)) {
completelyInsideBox.CreateFromCenter(triggerBox.Center(), triggerBox.Size() - 2.0f * playerBox.Size());
if (Collision::AABBVsAABB(completelyInsideBox, playerBox) &&
glm::all(glm::greaterThan(triggerBox.Size(), playerBox.Size()))) {
//Entity is completely inside the trigger.
//If it was only touching before, it is erased.
m_EntitiesTouchingTrigger[tId].erase(pId);
@@ -66,23 +67,6 @@ void TriggerSystem::Update(World* world, ComponentWrapper& trigger, double dt)
}
}
bool TriggerSystem::getEntityBox(World* world, EntityID id, AABB& outBox)
{
//TODO: Improve checking if component exists. Remove try
bool retry;
do {
retry = false;
try {
ComponentWrapper& cBox = world->GetComponent(id, "AABB");
outBox.CreateFromCenter(cBox["BoxCenter"], cBox["BoxSize"]);
} catch (std::out_of_range e) {
retry = true;
attachAABBComponentFromModel(world, id);
}
} while (retry);
return true;
}
bool TriggerSystem::throwLeaveIfWasInTrigger(std::unordered_set<EntityID>& triggerSet, EntityID pId, EntityID tId)
{
const auto& it = triggerSet.find(pId);
@@ -95,29 +79,3 @@ bool TriggerSystem::throwLeaveIfWasInTrigger(std::unordered_set<EntityID>& trigg
return false;
}
void TriggerSystem::attachAABBComponentFromModel(World* world, EntityID id)
{
ComponentWrapper model = world->GetComponent(id, "Model");
ComponentWrapper transform = world->GetComponent(id, "Transform");
ComponentWrapper collision = world->AttachComponent(id, "AABB");
Model* modelRes = ResourceManager::Load<Model>(model["Resource"]);
glm::mat4 modelMatrix = modelRes->m_Matrix *
glm::translate(glm::mat4(), (glm::vec3)transform["Position"]) *
glm::toMat4((glm::quat)transform["Orientation"]) *
glm::scale((glm::vec3)transform["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;
}