Simple quickly made CollisionSystem.

This commit is contained in:
William Moberg
2015-12-16 12:23:08 +01:00
parent d10df6ae09
commit cf0ff9bfe5
2 changed files with 11 additions and 16 deletions
+3 -3
View File
@@ -9,18 +9,18 @@
#include "Core/EventBroker.h" #include "Core/EventBroker.h"
#include "Core/EKeyUp.h" #include "Core/EKeyUp.h"
class CollisionSystem : public System class CollisionSystem : public PureSystem
{ {
public: public:
CollisionSystem(EventBroker* eventBroker) CollisionSystem(EventBroker* eventBroker)
: System(eventBroker, "AABB") : PureSystem(eventBroker, "AABB")
, zPress(false) , zPress(false)
{ {
//TODO: Debug stuff, remove later. //TODO: Debug stuff, remove later.
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &CollisionSystem::OnKeyUp); EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &CollisionSystem::OnKeyUp);
} }
virtual void Update(World* world, ComponentWrapper& cAABB, double dt) override; virtual void UpdateComponent(World* world, ComponentWrapper& cAABB, double dt) override;
private: private:
bool zPress; bool zPress;
+8 -13
View File
@@ -2,16 +2,13 @@
#include "Collision/CollisionSystem.h" #include "Collision/CollisionSystem.h"
#include "Core/AABB.h" #include "Core/AABB.h"
void CollisionSystem::Update(World * world, ComponentWrapper & cAABB, double dt) void CollisionSystem::UpdateComponent(World * world, ComponentWrapper & cAABB, double dt)
{ {
//cAABB is any entity that should be collideable. //cAABB is any entity that should be collideable.
m_EventBroker->Process<CollisionSystem>();
//Currently the box is translated according to the models Transform matrix,
//but not scaled (or rotated since it's AABB). This should be fine, if we don't
//want to shrink or scale up an collideable object after creation.
cAABB["BoxCenter"] = (glm::vec3)world->GetComponent(cAABB.EntityID, "Transform")["Position"];
AABB thisBox; AABB thisBox;
thisBox.CreateFromCenter(cAABB["BoxCenter"], cAABB["BoxSize"]); if (!Collision::GetEntityBox(world, cAABB, thisBox)) {
return;
}
//Here c should be an object that moves, currently only players. //Here c should be an object that moves, currently only players.
if (zPress) { if (zPress) {
return; return;
@@ -21,8 +18,9 @@ void CollisionSystem::Update(World * world, ComponentWrapper & cAABB, double dt)
continue; continue;
} }
AABB otherBox; AABB otherBox;
ComponentWrapper& aabbComp = world->GetComponent(mover.EntityID, "AABB"); if (!Collision::GetEntityBox(world, mover.EntityID, otherBox)) {
otherBox.CreateFromCenter(aabbComp["BoxCenter"], aabbComp["BoxSize"]); continue;
}
if (Collision::AABBVsAABB(thisBox, otherBox)) { if (Collision::AABBVsAABB(thisBox, otherBox)) {
ComponentWrapper& trans = world->GetComponent(mover.EntityID, "Transform"); ComponentWrapper& trans = world->GetComponent(mover.EntityID, "Transform");
//TODO: Move entity to correct position on collision instead of this. Special treatment if both are movers. //TODO: Move entity to correct position on collision instead of this. Special treatment if both are movers.
@@ -30,7 +28,6 @@ void CollisionSystem::Update(World * world, ComponentWrapper & cAABB, double dt)
float moveSpeed = 0.12f; float moveSpeed = 0.12f;
newPos += moveSpeed * glm::normalize(newPos - thisBox.Center()); newPos += moveSpeed * glm::normalize(newPos - thisBox.Center());
trans["Position"] = newPos; trans["Position"] = newPos;
aabbComp["BoxCenter"] = newPos;
} }
} }
} }
@@ -38,9 +35,7 @@ void CollisionSystem::Update(World * world, ComponentWrapper & cAABB, double dt)
bool CollisionSystem::OnKeyUp(const Events::KeyUp & event) bool CollisionSystem::OnKeyUp(const Events::KeyUp & event)
{ {
if (event.KeyCode == GLFW_KEY_Z) { if (event.KeyCode == GLFW_KEY_Z) {
zPress = true; zPress = !zPress;
} else if (event.KeyCode == GLFW_KEY_X) {
zPress = false;
} }
return false; return false;
} }