Tried a horrible recursive method, extreme framedrop.

This commit is contained in:
William Moberg
2016-01-27 10:34:26 +01:00
parent 3389b965eb
commit 6ea490e215
4 changed files with 67 additions and 14 deletions
+1
View File
@@ -65,6 +65,7 @@ bool AABBvsTriangles(const AABB& box,
const std::vector<RawModel::Vertex>& modelVertices,
const std::vector<unsigned int>& modelIndices,
const glm::mat4& modelMatrix,
const glm::vec3& boxVelocity,
glm::vec3& outResolutionVector);
//Return true if the boxes are intersecting.
@@ -6,9 +6,7 @@
<c:Model>
<Resource>../assets\Models/MapVersion1.obj</Resource>
</c:Model>
<c:Transform>
<Position X="0" Y="0" Z="-0.313012958"/>
</c:Transform>
<c:Transform/>
</Components>
<Children>
@@ -24,7 +22,7 @@
</c:Physics>
<c:Player/>
<c:Transform>
<Position X="-2.88221216" Y="7.13004351" Z="54.4666595"/>
<Position X="-1.48400009" Y="3.68200016" Z="24.7580013"/>
</c:Transform>
</Components>
<Children/>
+58 -4
View File
@@ -327,11 +327,14 @@ constexpr std::array<std::pair<int, int>, 3> dimensionPairs({ std::pair<int, int
bool AABBvsTriangle(const AABB& box,
const std::array<glm::vec3, 3>& triPos,
const glm::vec3& boxVelocity,
glm::vec3& outVector)
{
//Check so we don't have a zero area triangle when calculating the normal.
//Also, don't check a triangle facing away from the player.
//Less checks, and we should be able to walk out from models if we are trapped inside.
glm::vec3 triNormal = glm::cross(triPos[1] - triPos[0], triPos[2] - triPos[0]);
if (!vectorHasLength(triNormal)) {
if (!vectorHasLength(triNormal) || glm::dot(triNormal, boxVelocity) > 0) {
return false;
}
@@ -385,10 +388,18 @@ bool AABBvsTriangle(const AABB& box,
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 AABBvsTriangles(const AABB& box,
const std::vector<RawModel::Vertex>& modelVertices,
const std::vector<unsigned int>& modelIndices,
const glm::mat4& modelMatrix,
const glm::vec3& boxVelocity,
glm::vec3& outResolutionVector,
int startIndex,
int recursiveDepth)
{
AABB newBox = box;
bool hit = false;
AABB newBox = box;
outResolutionVector = glm::vec3(0.f);
for (int i = 0; i < modelIndices.size(); ) {
std::array<glm::vec3, 3> triVertices = {
@@ -397,13 +408,38 @@ bool AABBvsTriangles(const AABB& box, const std::vector<RawModel::Vertex>& model
Transform::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix)
};
glm::vec3 outVec;
if (AABBvsTriangle(newBox, triVertices, outVec)) {
if (AABBvsTriangle(newBox, triVertices, boxVelocity, outVec)) {
hit = true;
outResolutionVector += outVec;
newBox = AABB::FromOriginSize(newBox.Origin() + outVec, newBox.Size());
}
}
//outResolutionVector = glm::vec3(INFINITY);
//if (recursiveDepth > 2) {
// return true;
//}
//for (int i = startIndex; i < modelIndices.size(); ) {
// std::array<glm::vec3, 3> triVertices = {
// Transform::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix),
// Transform::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix),
// Transform::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix)
// };
// glm::vec3 outVec;
// if (AABBvsTriangle(box, triVertices, boxVelocity, outVec) && glm::length2(outVec) < glm::length2(outResolutionVector)) {
// hit = true;
// glm::vec3 potentialResolution = outVec;
// const AABB& resolvedBox = AABB::FromOriginSize(box.Origin() + potentialResolution, box.Size());
// if (AABBvsTriangles(resolvedBox, modelVertices, modelIndices, modelMatrix, boxVelocity, outVec, i, recursiveDepth+1)) {
// potentialResolution += outVec;
// if (glm::length2(potentialResolution) > glm::length2(outResolutionVector)) {
// continue;
// }
// }
// outResolutionVector = potentialResolution;
// }
//}
//outResolutionVector = glm::vec3(INFINITY);
//std::stack<AABB> testBoxes;
//std::stack<int> resolveIndices;
@@ -449,6 +485,24 @@ bool AABBvsTriangles(const AABB& box, const std::vector<RawModel::Vertex>& model
//}
return hit;
}
bool AABBvsTriangles(const AABB& box,
const std::vector<RawModel::Vertex>& modelVertices,
const std::vector<unsigned int>& modelIndices,
const glm::mat4& modelMatrix,
const glm::vec3& boxVelocity,
glm::vec3& outResolutionVector)
{
return AABBvsTriangles(
box,
modelVertices,
modelIndices,
modelMatrix,
boxVelocity,
outResolutionVector,
0,
0
);
}
bool IsSameBoxProbably(const AABB& first, const AABB& second, const float epsilon)
{
+6 -6
View File
@@ -41,11 +41,6 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
continue;
}
auto absPosition = Transform::AbsolutePosition(m_World, cModel.EntityID);
auto absOrientation = Transform::AbsoluteOrientation(m_World, cModel.EntityID);
auto absScale = Transform::AbsoluteScale(m_World, cModel.EntityID);
glm::mat4 modelMatrix = glm::translate(absPosition) * glm::toMat4(absOrientation) * glm::scale(absScale);
RawModel* model;
try {
model = ResourceManager::Load<RawModel, true>(cModel["Resource"]);
@@ -53,8 +48,13 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
continue;
}
auto absPosition = Transform::AbsolutePosition(m_World, cModel.EntityID);
auto absOrientation = Transform::AbsoluteOrientation(m_World, cModel.EntityID);
auto absScale = Transform::AbsoluteScale(m_World, cModel.EntityID);
glm::mat4 modelMatrix = glm::translate(absPosition) * glm::toMat4(absOrientation) * glm::scale(absScale);
glm::vec3 resolutionVector;
if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, resolutionVector)) {
if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, (glm::vec3&)cPhysics["Velocity"], resolutionVector)) {
(glm::vec3&)cTransform["Position"] += resolutionVector;
cPhysics["Velocity"] = glm::vec3(0, 0, 0);
}