Collision with everything omfg
This commit is contained in:
@@ -93,7 +93,7 @@ void GameWorld::Initialize()
|
|||||||
m_Player = CreateEntity();
|
m_Player = CreateEntity();
|
||||||
SetProperty(m_Player, "Name", std::string("PlayerShip"));
|
SetProperty(m_Player, "Name", std::string("PlayerShip"));
|
||||||
transform = AddComponent<Components::Transform>(m_Player, "Transform");
|
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);
|
transform->Scale = glm::vec3(2.0f);
|
||||||
model = AddComponent<Components::Model>(m_Player, "Model");
|
model = AddComponent<Components::Model>(m_Player, "Model");
|
||||||
model->ModelFile = "ship.obj";
|
model->ModelFile = "ship.obj";
|
||||||
@@ -103,7 +103,6 @@ void GameWorld::Initialize()
|
|||||||
bounds = AddComponent<Components::Bounds>(m_Player, "Bounds");
|
bounds = AddComponent<Components::Bounds>(m_Player, "Bounds");
|
||||||
bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3);
|
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(4.f,0.7f,4);
|
||||||
//bounds->VolumeVector = glm::vec3(1.f,0.7f,1.f);
|
|
||||||
|
|
||||||
|
|
||||||
player2 = CreateEntity();
|
player2 = CreateEntity();
|
||||||
@@ -121,8 +120,8 @@ void GameWorld::Initialize()
|
|||||||
void GameWorld::Update(double dt)
|
void GameWorld::Update(double dt)
|
||||||
{
|
{
|
||||||
World::Update(dt);
|
World::Update(dt);
|
||||||
if(GetSystem<Systems::CollisionSystem>("CollisionSystem")->Intersects(m_Player, entcamera))
|
/*if(GetSystem<Systems::CollisionSystem>("CollisionSystem")->Intersects(m_Player, entcamera))
|
||||||
assert(false);
|
assert(false);*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameWorld::RegisterComponents()
|
void GameWorld::RegisterComponents()
|
||||||
|
|||||||
@@ -13,46 +13,73 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
|||||||
transform->Position[2] += parentTransform->Position[2];
|
transform->Position[2] += parentTransform->Position[2];
|
||||||
}
|
}
|
||||||
LOG_INFO("Updating entity %i with parent %i", entity, parent);
|
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");
|
auto aTransform = m_World->GetComponent<Components::Transform>(aEntity, "Transform");
|
||||||
if(aTransform == nullptr)
|
if(aTransform == nullptr)
|
||||||
return false;
|
return;
|
||||||
auto bTransform = m_World->GetComponent<Components::Transform>(bEntity, "Transform");
|
auto bTransform = m_World->GetComponent<Components::Transform>(bEntity, "Transform");
|
||||||
if(bTransform == nullptr)
|
if(bTransform == nullptr)
|
||||||
return false;
|
return;
|
||||||
|
|
||||||
auto aCollisionComponent = m_World->GetComponent<Components::Collision>(aEntity, "Collision");
|
auto aCollisionComponent = m_World->GetComponent<Components::Collision>(aEntity, "Collision");
|
||||||
if(aCollisionComponent == nullptr)
|
if(aCollisionComponent == nullptr)
|
||||||
return false;
|
return;
|
||||||
auto bCollisionComponent = m_World->GetComponent<Components::Collision>(bEntity, "Collision");
|
auto bCollisionComponent = m_World->GetComponent<Components::Collision>(bEntity, "Collision");
|
||||||
if(bCollisionComponent == nullptr)
|
if(bCollisionComponent == nullptr)
|
||||||
return false;
|
return;
|
||||||
aCollisionComponent->CollidingEntities.clear();
|
|
||||||
bCollisionComponent->CollidingEntities.clear();
|
|
||||||
|
|
||||||
auto aBounds = m_World->GetComponent<Components::Bounds>(aEntity, "Bounds");
|
auto aBounds = m_World->GetComponent<Components::Bounds>(aEntity, "Bounds");
|
||||||
|
|
||||||
auto bBounds = m_World->GetComponent<Components::Bounds>(bEntity, "Bounds");
|
auto bBounds = m_World->GetComponent<Components::Bounds>(bEntity, "Bounds");
|
||||||
|
|
||||||
glm::vec3 aMax = aTransform->Position + aBounds->Origin + aBounds->VolumeVector;
|
glm::vec3 aPos = aTransform->Position + (aBounds->Origin * aTransform->Scale);
|
||||||
glm::vec3 aMin = aTransform->Position + aBounds->Origin - aBounds->VolumeVector;
|
glm::vec3 bPos = bTransform->Position + (bBounds->Origin * bTransform->Scale);
|
||||||
glm::vec3 bMax = bTransform->Position + bBounds->Origin + bBounds->VolumeVector;
|
|
||||||
glm::vec3 bMin = bTransform->Position + bBounds->Origin - bBounds->VolumeVector;
|
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.x <= bMax.x && bMin.x <= aMax.x) {
|
||||||
if (aMin.y <= bMax.y && bMin.y <= aMax.y) {
|
if (aMin.y <= bMax.y && bMin.y <= aMax.y) {
|
||||||
if (aMin.z <= bMax.z && bMin.z <= aMax.z) {
|
if (aMin.z <= bMax.z && bMin.z <= aMax.z) {
|
||||||
aCollisionComponent->CollidingEntities.push_back(aEntity);
|
aCollisionComponent->CollidingEntities.push_back(aEntity);
|
||||||
bCollisionComponent->CollidingEntities.push_back(bEntity);
|
bCollisionComponent->CollidingEntities.push_back(bEntity);
|
||||||
return true;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Systems::CollisionSystem::CreateBoundingBox(std::shared_ptr<Components::Bounds> bounds)
|
void Systems::CollisionSystem::CreateBoundingBox(std::shared_ptr<Components::Bounds> bounds)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public:
|
|||||||
: System(world) { }
|
: System(world) { }
|
||||||
|
|
||||||
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
||||||
bool Intersects(EntityID, EntityID);
|
void Intersects(EntityID, EntityID);
|
||||||
void CreateBoundingBox(std::shared_ptr<Components::Bounds>);
|
void CreateBoundingBox(std::shared_ptr<Components::Bounds>);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
// 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");
|
name = m_World->GetProperty<std::string>(entity, "Name");
|
||||||
if (name == "PlayerShip")
|
if (name == "PlayerShip")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,8 +6,11 @@
|
|||||||
#include "System.h"
|
#include "System.h"
|
||||||
#include "Components/Transform.h"
|
#include "Components/Transform.h"
|
||||||
#include "Components/Input.h"
|
#include "Components/Input.h"
|
||||||
|
#include "Components/Collision.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace Systems
|
namespace Systems
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,8 @@ public:
|
|||||||
// Recursively update through the scene graph
|
// Recursively update through the scene graph
|
||||||
void RecursiveUpdate(std::shared_ptr<System> system, double dt, EntityID parentEntity);
|
void RecursiveUpdate(std::shared_ptr<System> system, double dt, EntityID parentEntity);
|
||||||
|
|
||||||
|
std::unordered_map<EntityID, EntityID>* GetEntities() { return &m_EntityParents; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SystemFactory m_SystemFactory;
|
SystemFactory m_SystemFactory;
|
||||||
ComponentFactory m_ComponentFactory;
|
ComponentFactory m_ComponentFactory;
|
||||||
@@ -77,8 +79,7 @@ protected:
|
|||||||
EntityID m_LastEntityID;
|
EntityID m_LastEntityID;
|
||||||
std::stack<EntityID> m_RecycledEntityIDs;
|
std::stack<EntityID> m_RecycledEntityIDs;
|
||||||
// A bottom to top tree. A map of child entities to parent entities.
|
// 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<EntityID, std::unordered_map<std::string, boost::any>> m_EntityProperties;
|
||||||
|
|
||||||
std::unordered_map<std::string, std::vector<std::shared_ptr<Component>>> m_ComponentsOfType;
|
std::unordered_map<std::string, std::vector<std::shared_ptr<Component>>> m_ComponentsOfType;
|
||||||
|
|||||||
Reference in New Issue
Block a user