CollisionSystem now utilizes Octree for collision detection.
This commit is contained in:
@@ -7,10 +7,10 @@
|
|||||||
<xs:complexType>
|
<xs:complexType>
|
||||||
<xs:all>
|
<xs:all>
|
||||||
<xs:element name="Origin" type="t:Vector" minOccurs="0">
|
<xs:element name="Origin" type="t:Vector" minOccurs="0">
|
||||||
<xs:annotation><xs:documentation>Middle point of the bounding box</xs:documentation></xs:annotation>
|
<xs:annotation><xs:documentation>Middle point of the bounding box, in model space</xs:documentation></xs:annotation>
|
||||||
</xs:element>
|
</xs:element>
|
||||||
<xs:element name="Size" type="t:Vector" minOccurs="0">
|
<xs:element name="Size" type="t:Vector" minOccurs="0">
|
||||||
<xs:annotation><xs:documentation>Size of the bounding box</xs:documentation></xs:annotation>
|
<xs:annotation><xs:documentation>Size of the bounding box, in model space</xs:documentation></xs:annotation>
|
||||||
</xs:element>
|
</xs:element>
|
||||||
</xs:all>
|
</xs:all>
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
|
|||||||
@@ -8,6 +8,9 @@
|
|||||||
<Children>
|
<Children>
|
||||||
<Entity name="Scenemesh">
|
<Entity name="Scenemesh">
|
||||||
<Components>
|
<Components>
|
||||||
|
<c:AABB>
|
||||||
|
<Size X="150" Y="64" Z="180"/>
|
||||||
|
</c:AABB>
|
||||||
<c:Collidable/>
|
<c:Collidable/>
|
||||||
<c:Model>
|
<c:Model>
|
||||||
<Resource>Models\MapVersion1.mesh</Resource>
|
<Resource>Models\MapVersion1.mesh</Resource>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "Collision/Collision.h"
|
#include "Collision/Collision.h"
|
||||||
#include "Collision/CollisionSystem.h"
|
#include "Collision/CollisionSystem.h"
|
||||||
#include "Core/AABB.h"
|
#include "Core/AABB.h"
|
||||||
|
#include "Rendering/Model.h"
|
||||||
|
|
||||||
void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt)
|
void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt)
|
||||||
{
|
{
|
||||||
@@ -25,33 +26,28 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Collision::AABBVsAABB(boxA, boxB, resolutionVector)) {
|
if (boxB.Entity.HasComponent("Model") && Collision::AABBVsAABB(boxA, boxB)) {
|
||||||
|
//Here we know boxB is a entity with Collideable, AABB, and Model.
|
||||||
|
RawModel* model;
|
||||||
|
try {
|
||||||
|
model = ResourceManager::Load<RawModel, true>(boxB.Entity["Model"]["Resource"]);
|
||||||
|
} catch (const std::exception&) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::mat4 modelMatrix = Transform::ModelMatrix(boxB.Entity);
|
||||||
|
|
||||||
|
glm::vec3 newVelocity = (glm::vec3)cPhysics["Velocity"];
|
||||||
|
if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, newVelocity, resolutionVector)) {
|
||||||
|
(glm::vec3&)cTransform["Position"] += resolutionVector;
|
||||||
|
cPhysics["Velocity"] = newVelocity;
|
||||||
|
}
|
||||||
|
} else if (Collision::AABBVsAABB(boxA, boxB, resolutionVector)) {
|
||||||
|
//Enter here if boxB has no Model.
|
||||||
(glm::vec3&)cTransform["Position"] += resolutionVector;
|
(glm::vec3&)cTransform["Position"] += resolutionVector;
|
||||||
if (resolutionVector.y > 0) {
|
if (resolutionVector.y > 0) {
|
||||||
((glm::vec3&)cPhysics["Velocity"]).y = 0.f;
|
((glm::vec3&)cPhysics["Velocity"]).y = 0.f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HACK: Temporarily collide against all collidable models since they're not in the octree yet
|
|
||||||
//auto otherCollidables = world->GetComponents("Model");
|
|
||||||
//for (auto& cModel : *otherCollidables) {
|
|
||||||
// if (cModel.EntityID == entity) {
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// if (!world->HasComponent(cModel.EntityID, "Collidable")) {
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// auto absPosition = RenderQueueFactory::AbsolutePosition(world, cModel.EntityID);
|
|
||||||
// auto absOrientation = RenderQueueFactory::AbsoluteOrientation(world, cModel.EntityID);
|
|
||||||
// auto absScale = RenderQueueFactory::AbsoluteScale(world, cModel.EntityID);
|
|
||||||
// glm::mat4 modelMatrix = glm::translate(absPosition); // *glm::toMat4(absOrientation) * glm::scale(absScale);
|
|
||||||
|
|
||||||
// auto model = ResourceManager::Load<Model>(cModel["Resource"]);
|
|
||||||
// glm::vec3 resolutionVector;
|
|
||||||
// if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, resolutionVector)) {
|
|
||||||
// (glm::vec3&)cTransform["Position"] += resolutionVector;
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user