AABBs are auto attached when adding Trigger/Collideable/etc, also added AttachComponent method to ComponentWrapper.
This commit is contained in:
@@ -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);
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,12 +7,14 @@ void FillOctreeSystem::Update(double dt)
|
|||||||
|
|
||||||
void FillOctreeSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt)
|
void FillOctreeSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt)
|
||||||
{
|
{
|
||||||
if (entity.HasComponent("AABB")) {
|
if (!entity.HasComponent("AABB")) {
|
||||||
boost::optional<EntityAABB> absoluteAABB = Collision::EntityAbsoluteAABB(entity);
|
//Derive AABB from model.
|
||||||
if (absoluteAABB) {
|
if (!Collision::AttachAABBComponentFromModel(entity)) {
|
||||||
m_Octree->AddDynamicObject(*absoluteAABB);
|
return;
|
||||||
}
|
}
|
||||||
} else if (entity.HasComponent("Model")) {
|
}
|
||||||
// TODO: Derive AABB from model
|
boost::optional<EntityAABB> absoluteAABB = Collision::EntityAbsoluteAABB(entity);
|
||||||
|
if (absoluteAABB) {
|
||||||
|
m_Octree->AddDynamicObject(*absoluteAABB);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user