WIP triangles vs. AABB.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
|
||||
<Entity xmlns:c="components" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd">
|
||||
|
||||
<Components>
|
||||
<c:Transform/>
|
||||
</Components>
|
||||
|
||||
<Children>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Camera/>
|
||||
<c:Listener/>
|
||||
<c:Transform>
|
||||
<Position X="5.59488058" Y="4.13046598" Z="6.74478388"/>
|
||||
<Orientation X="-0.383969575" Y="0.802773356" Z="1.33006881e-06"/>
|
||||
</c:Transform>
|
||||
</Components>
|
||||
<Children/>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Collidable/>
|
||||
<c:Model>
|
||||
<Resource>../assets/Models/Core/UnitCube.obj</Resource>
|
||||
</c:Model>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="-0.665447116" Z="0"/>
|
||||
<Scale X="1" Y="0.200000003" Z="1"/>
|
||||
</c:Transform>
|
||||
</Components>
|
||||
<Children/>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:AABB/>
|
||||
<c:Collidable/>
|
||||
<c:Physics/>
|
||||
<c:Model>
|
||||
<Resource>../assets/Models/Core/UnitCube.obj</Resource>
|
||||
</c:Model>
|
||||
<c:Transform>
|
||||
<Position X="2.27028751" Y="0.393286705" Z="-2.67389679"/>
|
||||
</c:Transform>
|
||||
</Components>
|
||||
<Children/>
|
||||
</Entity>
|
||||
</Children>
|
||||
|
||||
</Entity>
|
||||
@@ -199,30 +199,93 @@ bool RayVsModel(const Ray& ray,
|
||||
return hit;
|
||||
}
|
||||
|
||||
bool AABBvsTriangles(const AABB& box, const std::vector<RawModel::Vertex>& modelVertices, const std::vector<unsigned int>& modelIndices, const glm::mat4& modelMatrix, glm::vec3& outResolutionVector)
|
||||
bool AABBvsTriangle(const AABB& box, const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, glm::vec3& outResolutionVector)
|
||||
{
|
||||
bool hit = false;
|
||||
|
||||
const glm::vec3& origin = box.Origin();
|
||||
const glm::vec3& min = box.MinCorner();
|
||||
const glm::vec3& max = box.MaxCorner();
|
||||
const glm::vec3& half = box.HalfSize();
|
||||
|
||||
outResolutionVector.x = INFINITY;
|
||||
const glm::vec3 triPos[] = {
|
||||
v0, v1, v2
|
||||
};
|
||||
|
||||
for (int i = 0; i < modelIndices.size(); ++i) {
|
||||
glm::vec3 p = modelVertices[i].Position;
|
||||
for (int ax = 0; ax < 3; ++ax) {
|
||||
auto outsideMinPlane = glm::tvec3<bool>(false);
|
||||
auto outsideMaxPlane = glm::tvec3<bool>(false);
|
||||
for (int pos = 0; pos < 3; ++pos) {
|
||||
outsideMinPlane[pos] = (min[ax] > triPos[pos][ax]);
|
||||
outsideMaxPlane[pos] = (triPos[pos][ax] > max[ax]);
|
||||
}
|
||||
if (glm::all(outsideMinPlane) || glm::all(outsideMaxPlane)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AABBvsTriangles(const AABB& box, const std::vector<RawModel::Vertex>& modelVertices, const std::vector<unsigned int>& modelIndices, const glm::mat4& modelMatrix, glm::vec3& outResolutionVector)
|
||||
{
|
||||
bool hit = false;
|
||||
for (int i = 0; i < modelIndices.size(); i += 3) {
|
||||
glm::vec3 resVec;
|
||||
hit = AABBvsTriangle(
|
||||
box,
|
||||
modelVertices[modelIndices[i]].Position,
|
||||
modelVertices[modelIndices[i + 1]].Position,
|
||||
modelVertices[modelIndices[i + 2]].Position,
|
||||
resVec
|
||||
);
|
||||
if (hit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return hit;
|
||||
//TODO: Remove old code.
|
||||
|
||||
/*
|
||||
auto hit = glm::tvec3<bool>(false);
|
||||
const glm::vec3& origin = box.Origin();
|
||||
const glm::vec3& min = box.MinCorner();
|
||||
const glm::vec3& max = box.MaxCorner();
|
||||
const glm::vec3& half = box.HalfSize();
|
||||
|
||||
float minResolution = INFINITY;
|
||||
outResolutionVector = glm::vec3(0.f);
|
||||
//TODO: Don't need indices? Remove?
|
||||
for (const auto& v : modelVertices) {
|
||||
glm::vec3 p = v.Position;
|
||||
p = glm::vec3(modelMatrix * glm::vec4(p.x, p.y, p.z, 1));
|
||||
|
||||
float distFromOrigin = glm::abs(origin.x - p.x);
|
||||
float penetration = box.HalfSize().x - distFromOrigin;
|
||||
if (penetration > 0 && penetration < glm::abs(outResolutionVector.x)) {
|
||||
if (p.x > origin.x) {
|
||||
outResolutionVector.x = -penetration;
|
||||
const glm::vec3 diffO = origin - p;
|
||||
const glm::vec3 distO = glm::abs(diffO);
|
||||
for (int axis = 0; axis < 3; ++axis) {
|
||||
float penetration = half[axis] - distO[axis];
|
||||
if (penetration > 0){
|
||||
if (penetration < minResolution) {
|
||||
minResolution = penetration;
|
||||
outResolutionVector = glm::vec3(0.f);
|
||||
if (p[axis] > origin[axis]) {
|
||||
outResolutionVector[axis] = -penetration;
|
||||
} else {
|
||||
outResolutionVector.x = penetration;
|
||||
outResolutionVector[axis] = penetration;
|
||||
}
|
||||
hit = true;
|
||||
}
|
||||
hit[axis] = true;
|
||||
}
|
||||
}
|
||||
|
||||
//float distFromOrigin = glm::abs(origin.x - p.x);
|
||||
//float penetration = box.HalfSize().x - distFromOrigin;
|
||||
//if (penetration > 0 && penetration < glm::abs(outResolutionVector.x)) {
|
||||
// if (p.x > origin.x) {
|
||||
// outResolutionVector.x = -penetration;
|
||||
// } else {
|
||||
// outResolutionVector.x = penetration;
|
||||
// }
|
||||
// hit = true;
|
||||
//}
|
||||
|
||||
//glm::vec3 pLocal = origin - p;
|
||||
//for (int axis = 0; axis < 3; ++axis) {
|
||||
// if (p[axis] < min[axis] || p[axis] > max[axis]) {
|
||||
@@ -236,7 +299,13 @@ bool AABBvsTriangles(const AABB& box, const std::vector<RawModel::Vertex>& model
|
||||
//}
|
||||
}
|
||||
|
||||
return hit;
|
||||
//for (int axis = 0; axis < 3; ++axis) {
|
||||
// if (glm::isinf(outResolutionVector[axis])) {
|
||||
// outResolutionVector[axis] = 0.f;
|
||||
// }
|
||||
//}
|
||||
|
||||
return glm::all(hit);*/
|
||||
}
|
||||
|
||||
bool IsSameBoxProbably(const AABB& first, const AABB& second, const float epsilon)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "Collision/Collision.h"
|
||||
#include "Collision/CollisionSystem.h"
|
||||
#include "Core/AABB.h"
|
||||
#include "Rendering/Model.h"
|
||||
|
||||
void CollisionSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& component, double dt)
|
||||
{
|
||||
@@ -22,40 +23,42 @@ void CollisionSystem::UpdateComponent(World* world, EntityWrapper& entity, Compo
|
||||
}
|
||||
|
||||
// Collide against octree
|
||||
std::vector<AABB> octreeResult;
|
||||
m_Octree->BoxesInSameRegion(*boundingBox, octreeResult);
|
||||
for (auto& boxB : octreeResult) {
|
||||
glm::vec3 resolutionVector;
|
||||
if (Collision::IsSameBoxProbably(boxA, boxB)) {
|
||||
continue;
|
||||
}
|
||||
if (Collision::AABBVsAABB(boxA, boxB, resolutionVector)) {
|
||||
(glm::vec3&)cTransform["Position"] += resolutionVector;
|
||||
cPhysics["Velocity"] = glm::vec3(0, 0, 0);
|
||||
}
|
||||
}
|
||||
//std::vector<AABB> octreeResult;
|
||||
//m_Octree->BoxesInSameRegion(*boundingBox, octreeResult);
|
||||
//for (auto& boxB : octreeResult) {
|
||||
// glm::vec3 resolutionVector;
|
||||
// if (Collision::IsSameBoxProbably(boxA, boxB)) {
|
||||
// continue;
|
||||
// }
|
||||
// if (Collision::AABBVsAABB(boxA, boxB, resolutionVector)) {
|
||||
// (glm::vec3&)cTransform["Position"] += resolutionVector;
|
||||
// cPhysics["Velocity"] = glm::vec3(0, 0, 0);
|
||||
// }
|
||||
//}
|
||||
|
||||
// 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 otherCollidables = world->GetComponents("Model");
|
||||
for (auto& cModel : *otherCollidables) {
|
||||
if (cModel.EntityID == 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 absPosition = Transform::AbsolutePosition(world, cModel.EntityID);
|
||||
auto absOrientation = Transform::AbsoluteOrientation(world, cModel.EntityID);
|
||||
auto absScale = Transform::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)) {
|
||||
RawModel* model = ResourceManager::Load<RawModel>(cModel["Resource"]);
|
||||
glm::vec3 resolutionVector;
|
||||
if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, resolutionVector)) {
|
||||
//TODO: remove Temp.
|
||||
(glm::vec3&)cTransform["Position"] = glm::vec3(2, 2, 2);
|
||||
//(glm::vec3&)cTransform["Position"] += resolutionVector;
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CollisionSystem::OnKeyUp(const Events::KeyUp & event)
|
||||
|
||||
Reference in New Issue
Block a user