Collision with everything omfg

This commit is contained in:
Adam
2014-03-13 02:56:29 +01:00
parent 41e0c7b07a
commit 76da942fdb
6 changed files with 58 additions and 21 deletions
+3 -4
View File
@@ -93,7 +93,7 @@ void GameWorld::Initialize()
m_Player = CreateEntity();
SetProperty(m_Player, "Name", std::string("PlayerShip"));
transform = AddComponent<Components::Transform>(m_Player, "Transform");
transform->Position = glm::vec3(0.f, 4.f, 0.f);
transform->Position = glm::vec3(0.f, 4.f, -5.f);
transform->Scale = glm::vec3(2.0f);
model = AddComponent<Components::Model>(m_Player, "Model");
model->ModelFile = "ship.obj";
@@ -103,7 +103,6 @@ void GameWorld::Initialize()
bounds = AddComponent<Components::Bounds>(m_Player, "Bounds");
bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3);
bounds->VolumeVector = glm::vec3(4.f,0.7f,4);
//bounds->VolumeVector = glm::vec3(1.f,0.7f,1.f);
player2 = CreateEntity();
@@ -121,8 +120,8 @@ void GameWorld::Initialize()
void GameWorld::Update(double dt)
{
World::Update(dt);
if(GetSystem<Systems::CollisionSystem>("CollisionSystem")->Intersects(m_Player, entcamera))
assert(false);
/*if(GetSystem<Systems::CollisionSystem>("CollisionSystem")->Intersects(m_Player, entcamera))
assert(false);*/
}
void GameWorld::RegisterComponents()
+41 -14
View File
@@ -13,46 +13,73 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID
transform->Position[2] += parentTransform->Position[2];
}
LOG_INFO("Updating entity %i with parent %i", entity, parent);
auto entities = m_World->GetEntities();
for (auto pair : *entities) {
EntityID entity2 = pair.first;
EntityID parent2 = pair.second;
// Check if entity2 is a child of entity
/*auto currentParent = parent;
bool childOfEntity2 = false;
while (currentParent != 0) {
if (currentParent == entity2) {
childOfEntity2 = true;
break;
}
currentParent = entities[currentParent];
}
if (childOfEntity2)
continue;*/
// Check if we're the parent to entity2
//pair.first, pair.second;
if(entity == entity2)
break;
Intersects(entity, entity2);
}
}
bool Systems::CollisionSystem::Intersects(EntityID aEntity, EntityID bEntity)
void Systems::CollisionSystem::Intersects(EntityID aEntity, EntityID bEntity)
{
auto aTransform = m_World->GetComponent<Components::Transform>(aEntity, "Transform");
if(aTransform == nullptr)
return false;
return;
auto bTransform = m_World->GetComponent<Components::Transform>(bEntity, "Transform");
if(bTransform == nullptr)
return false;
return;
auto aCollisionComponent = m_World->GetComponent<Components::Collision>(aEntity, "Collision");
if(aCollisionComponent == nullptr)
return false;
return;
auto bCollisionComponent = m_World->GetComponent<Components::Collision>(bEntity, "Collision");
if(bCollisionComponent == nullptr)
return false;
aCollisionComponent->CollidingEntities.clear();
bCollisionComponent->CollidingEntities.clear();
return;
auto aBounds = m_World->GetComponent<Components::Bounds>(aEntity, "Bounds");
auto bBounds = m_World->GetComponent<Components::Bounds>(bEntity, "Bounds");
glm::vec3 aMax = aTransform->Position + aBounds->Origin + aBounds->VolumeVector;
glm::vec3 aMin = aTransform->Position + aBounds->Origin - aBounds->VolumeVector;
glm::vec3 bMax = bTransform->Position + bBounds->Origin + bBounds->VolumeVector;
glm::vec3 bMin = bTransform->Position + bBounds->Origin - bBounds->VolumeVector;
glm::vec3 aPos = aTransform->Position + (aBounds->Origin * aTransform->Scale);
glm::vec3 bPos = bTransform->Position + (bBounds->Origin * bTransform->Scale);
glm::vec3 aMax = aPos + aBounds->VolumeVector * aTransform->Scale;
glm::vec3 aMin = aPos - aBounds->VolumeVector * aTransform->Scale;
glm::vec3 bMax = bPos + bBounds->VolumeVector * bTransform->Scale;
glm::vec3 bMin = bPos - bBounds->VolumeVector * bTransform->Scale;
if (aMin.x <= bMax.x && bMin.x <= aMax.x) {
if (aMin.y <= bMax.y && bMin.y <= aMax.y) {
if (aMin.z <= bMax.z && bMin.z <= aMax.z) {
aCollisionComponent->CollidingEntities.push_back(aEntity);
bCollisionComponent->CollidingEntities.push_back(bEntity);
return true;
return;
}
}
}
return false;
}
void Systems::CollisionSystem::CreateBoundingBox(std::shared_ptr<Components::Bounds> bounds)
+1 -1
View File
@@ -19,7 +19,7 @@ public:
: System(world) { }
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
bool Intersects(EntityID, EntityID);
void Intersects(EntityID, EntityID);
void CreateBoundingBox(std::shared_ptr<Components::Bounds>);
};
+7
View File
@@ -60,8 +60,15 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
//---------------------------------------------------------------------
// TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
}
auto collision = m_World->GetComponent<Components::Collision>(entity, "Collision");
if(collision->CollidingEntities.size() != 0)
int c = 1; // lol
}
name = m_World->GetProperty<std::string>(entity, "Name");
if (name == "PlayerShip")
{
+3
View File
@@ -6,8 +6,11 @@
#include "System.h"
#include "Components/Transform.h"
#include "Components/Input.h"
#include "Components/Collision.h"
#include "logging.h"
namespace Systems
{
+3 -2
View File
@@ -68,6 +68,8 @@ public:
// Recursively update through the scene graph
void RecursiveUpdate(std::shared_ptr<System> system, double dt, EntityID parentEntity);
std::unordered_map<EntityID, EntityID>* GetEntities() { return &m_EntityParents; }
protected:
SystemFactory m_SystemFactory;
ComponentFactory m_ComponentFactory;
@@ -77,8 +79,7 @@ protected:
EntityID m_LastEntityID;
std::stack<EntityID> m_RecycledEntityIDs;
// A bottom to top tree. A map of child entities to parent entities.
std::unordered_map<EntityID, EntityID> m_EntityParents;
std::unordered_map<EntityID, EntityID> m_EntityParents ;
std::unordered_map<EntityID, std::unordered_map<std::string, boost::any>> m_EntityProperties;
std::unordered_map<std::string, std::vector<std::shared_ptr<Component>>> m_ComponentsOfType;