Merge branch 'BoxModelCollision' into FrustumCulling

# Conflicts:
#	src/Game/Game.cpp
This commit is contained in:
William Moberg
2016-02-09 15:03:13 +01:00
11 changed files with 73 additions and 50 deletions
+2
View File
@@ -78,6 +78,8 @@ bool AABBVsAABB(const AABB& a, const AABB& b);
//Also outputs the minimum translation that box [a] would need in order to resolve collision. //Also outputs the minimum translation that box [a] would need in order to resolve collision.
bool AABBVsAABB(const AABB& a, const AABB& b, glm::vec3& minimumTranslation); bool AABBVsAABB(const AABB& a, const AABB& b, glm::vec3& minimumTranslation);
//Attaches an AABB which contains all vertices in the entitys Model.
bool AttachAABBComponentFromModel(EntityWrapper entity);
// Calculates an absolute AABB from an entity AABB component // Calculates an absolute AABB from an entity AABB component
boost::optional<EntityAABB> EntityAbsoluteAABB(EntityWrapper& entity); boost::optional<EntityAABB> EntityAbsoluteAABB(EntityWrapper& entity);
@@ -6,12 +6,12 @@
#include "Collision.h" #include "Collision.h"
#include "EntityAABB.h" #include "EntityAABB.h"
class CollidableOctreeSystem : public ImpureSystem, public PureSystem class FillOctreeSystem : public ImpureSystem, public PureSystem
{ {
public: public:
CollidableOctreeSystem(World* world, EventBroker* eventBroker, Octree<EntityAABB>* octree, const std::string& componentType) FillOctreeSystem(World* world, EventBroker* eventBroker, Octree<EntityAABB>* octree, const std::string& fillComponentType)
: System(world, eventBroker) : System(world, eventBroker)
, PureSystem(componentType) , PureSystem(fillComponentType)
, m_Octree(octree) , m_Octree(octree)
{ } { }
+1
View File
@@ -25,6 +25,7 @@ struct EntityWrapper
const std::string Name(); const std::string Name();
bool HasComponent(const std::string& componentType); bool HasComponent(const std::string& componentType);
void AttachComponent(const char* componentName);
EntityWrapper Parent(); EntityWrapper Parent();
EntityWrapper FirstChildByName(const std::string& name); EntityWrapper FirstChildByName(const std::string& name);
EntityWrapper FirstParentWithComponent(const std::string& componentType); EntityWrapper FirstParentWithComponent(const std::string& componentType);
+2 -1
View File
@@ -9,7 +9,8 @@
<Entity name="Scenemesh"> <Entity name="Scenemesh">
<Components> <Components>
<c:AABB> <c:AABB>
<Size X="150" Y="64" Z="180"/> <Origin X="0.195705414" Y="26.0382366" Z="-0.010017395"/>
<Size X="135.414337" Y="68.3848572" Z="180.020035"/>
</c:AABB> </c:AABB>
<c:Collidable/> <c:Collidable/>
<c:Model> <c:Model>
@@ -1,18 +0,0 @@
#include "Collision/CollidableOctreeSystem.h"
void CollidableOctreeSystem::Update(double dt)
{
m_Octree->ClearDynamicObjects();
}
void CollidableOctreeSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt)
{
if (entity.HasComponent("AABB")) {
boost::optional<EntityAABB> absoluteAABB = Collision::EntityAbsoluteAABB(entity);
if (absoluteAABB) {
m_Octree->AddDynamicObject(*absoluteAABB);
}
} else if (entity.HasComponent("Model")) {
// TODO: Derive AABB from model
}
}
+16 -20
View File
@@ -564,33 +564,29 @@ bool AABBvsTriangles(const AABB& box,
return hit; return hit;
} }
bool attachAABBComponentFromModel(World* world, EntityID id) bool AttachAABBComponentFromModel(EntityWrapper entity)
{ {
if (!world->HasComponent(id, "Model")) { if (!entity.HasComponent("Model")) {
return false; return false;
} }
ComponentWrapper model = world->GetComponent(id, "Model"); //Derive AABB from model
ComponentWrapper collision = world->AttachComponent(id, "AABB"); RawModel* model;
Model* modelRes = ResourceManager::Load<Model>(model["Resource"]); try {
if (modelRes == nullptr) { model = ResourceManager::Load<RawModel, true>(entity["Model"]["Resource"]);
} catch (const std::exception&) {
return false; return false;
} }
glm::mat4 modelMatrix = modelRes->Matrix(); glm::vec3 mini(INFINITY);
glm::vec3 maxi(-INFINITY);
glm::vec3 mini = glm::vec3(INFINITY, INFINITY, INFINITY); for (const auto& v : model->m_Vertices) {
glm::vec3 maxi = glm::vec3(-INFINITY, -INFINITY, -INFINITY); mini = glm::min(mini, v.Position);
for (const auto& v : modelRes->Vertices()) { maxi = glm::max(maxi, v.Position);
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["Origin"] = 0.5f * (maxi + mini);
collision["Size"] = maxi - mini; entity.AttachComponent("AABB");
entity["AABB"]["Origin"] = 0.5f * (maxi + mini);
entity["AABB"]["Size"] = maxi - mini;
return true; return true;
} }
+13 -5
View File
@@ -20,6 +20,7 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
// Collide against octree items // Collide against octree items
m_OctreeResult.clear(); m_OctreeResult.clear();
m_Octree->ObjectsInSameRegion(*boundingBox, m_OctreeResult); m_Octree->ObjectsInSameRegion(*boundingBox, m_OctreeResult);
bool everHitTheGround = false;
for (auto& boxB : m_OctreeResult) { for (auto& boxB : m_OctreeResult) {
glm::vec3 resolutionVector; glm::vec3 resolutionVector;
if (boxA.Entity == boxB.Entity) { if (boxA.Entity == boxB.Entity) {
@@ -43,17 +44,24 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, inOutVelocity, verticalStepHeight, isOnGround, resolutionVector)) { if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, inOutVelocity, verticalStepHeight, isOnGround, resolutionVector)) {
(glm::vec3&)cTransform["Position"] += resolutionVector; (glm::vec3&)cTransform["Position"] += resolutionVector;
cPhysics["Velocity"] = inOutVelocity; cPhysics["Velocity"] = inOutVelocity;
(bool)cPhysics["IsOnGround"] = isOnGround; if (isOnGround) {
} else { everHitTheGround = true;
(bool)cPhysics["IsOnGround"] = false; (bool)cPhysics["IsOnGround"] = true;
}
} }
} else if (Collision::AABBVsAABB(boxA, boxB, resolutionVector)) { } else if (Collision::AABBVsAABB(boxA, boxB, resolutionVector)) {
//Enter here if boxB has no Model. //Enter here if boxB has no Model.
(glm::vec3&)cTransform["Position"] += resolutionVector; (glm::vec3&)cTransform["Position"] += resolutionVector;
(bool)cPhysics["IsOnGround"] = resolutionVector.y > 0; if (resolutionVector.y > 0) {
if ((bool)cPhysics["IsOnGround"]){ everHitTheGround = true;
(bool)cPhysics["IsOnGround"] = true;
((glm::vec3&)cPhysics["Velocity"]).y = 0.f; ((glm::vec3&)cPhysics["Velocity"]).y = 0.f;
} }
} }
} }
//This should apply air friction and such, iff zero models were hit.
if (!everHitTheGround) {
(bool)cPhysics["IsOnGround"] = false;
}
} }
+20
View File
@@ -0,0 +1,20 @@
#include "Collision/FillOctreeSystem.h"
void FillOctreeSystem::Update(double dt)
{
m_Octree->ClearDynamicObjects();
}
void FillOctreeSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt)
{
if (!entity.HasComponent("AABB")) {
//Derive AABB from model.
if (!Collision::AttachAABBComponentFromModel(entity)) {
return;
}
}
boost::optional<EntityAABB> absoluteAABB = Collision::EntityAbsoluteAABB(entity);
if (absoluteAABB) {
m_Octree->AddDynamicObject(*absoluteAABB);
}
}
+4
View File
@@ -6,6 +6,10 @@
void TriggerSystem::UpdateComponent(EntityWrapper& triggerEntity, ComponentWrapper& cTrigger, double dt) void TriggerSystem::UpdateComponent(EntityWrapper& triggerEntity, ComponentWrapper& cTrigger, double dt)
{ {
// The trigger *should* have a bounding box, or something, to test against so it can be triggered. // The trigger *should* have a bounding box, or something, to test against so it can be triggered.
// If it doesn't, add one as big as the model for now, then size can be modified in editor if necessary.
if (!triggerEntity.HasComponent("AABB")) {
Collision::AttachAABBComponentFromModel(triggerEntity);
}
boost::optional<EntityAABB> triggerBox = Collision::EntityAbsoluteAABB(triggerEntity); boost::optional<EntityAABB> triggerBox = Collision::EntityAbsoluteAABB(triggerEntity);
if (!triggerBox) { if (!triggerBox) {
return; return;
+9
View File
@@ -16,6 +16,15 @@ bool EntityWrapper::HasComponent(const std::string& componentName)
return World->HasComponent(ID, componentName); return World->HasComponent(ID, componentName);
} }
void EntityWrapper::AttachComponent(const char* componentName)
{
if (!Valid()) {
LOG_WARNING("Could not attach \"%s\" component to #%i, component is not valid.", componentName, ID);
return;
}
World->AttachComponent(ID, componentName);
}
EntityWrapper EntityWrapper::Parent() EntityWrapper EntityWrapper::Parent()
{ {
if (this->World == nullptr || this->ID == EntityID_Invalid) { if (this->World == nullptr || this->ID == EntityID_Invalid) {
+3 -3
View File
@@ -1,5 +1,5 @@
#include "Game.h" #include "Game.h"
#include "Collision/CollidableOctreeSystem.h" #include "Collision/FillOctreeSystem.h"
#include "Collision/EntityAABB.h" #include "Collision/EntityAABB.h"
#include "Collision/TriggerSystem.h" #include "Collision/TriggerSystem.h"
#include "Collision/CollisionSystem.h" #include "Collision/CollisionSystem.h"
@@ -98,8 +98,8 @@ Game::Game(int argc, char* argv[])
m_SystemPipeline->AddSystem<PickupSpawnSystem>(updateOrderLevel); m_SystemPipeline->AddSystem<PickupSpawnSystem>(updateOrderLevel);
// Populate Octree with collidables // Populate Octree with collidables
++updateOrderLevel; ++updateOrderLevel;
m_SystemPipeline->AddSystem<CollidableOctreeSystem>(updateOrderLevel, m_OctreeCollision, "Collidable"); m_SystemPipeline->AddSystem<FillOctreeSystem>(updateOrderLevel, m_OctreeCollision, "Collidable");
m_SystemPipeline->AddSystem<CollidableOctreeSystem>(updateOrderLevel, m_OctreeTrigger, "Player"); m_SystemPipeline->AddSystem<FillOctreeSystem>(updateOrderLevel, m_OctreeTrigger, "Player");
m_SystemPipeline->AddSystem<CollidableOctreeSystem>(updateOrderLevel, m_OctreeFrustrumCulling, "Model"); m_SystemPipeline->AddSystem<CollidableOctreeSystem>(updateOrderLevel, m_OctreeFrustrumCulling, "Model");
m_SystemPipeline->AddSystem<PlayerHUD>(updateOrderLevel); m_SystemPipeline->AddSystem<PlayerHUD>(updateOrderLevel);
m_SystemPipeline->AddSystem<AnimationSystem>(updateOrderLevel); m_SystemPipeline->AddSystem<AnimationSystem>(updateOrderLevel);