Debug cleanup and some bug fixes.

This commit is contained in:
William Moberg
2016-02-05 14:38:08 +01:00
parent d61e605093
commit 4fe9e4f1c3
2 changed files with 13 additions and 83 deletions
+5 -63
View File
@@ -459,7 +459,7 @@ bool AABBvsTriangle(const AABB& box,
return false; return false;
} }
glm::vec3 cornerResolution = (1+t) * diagonal; glm::vec3 cornerResolution = (1+t) * diagonal;
//Overwrite the smallest resolution if this is smaller. //Overwrite the smallest resolution if cornerResolution is smaller.
float lenSq = glm::length2(cornerResolution); float lenSq = glm::length2(cornerResolution);
if (lenSq < resolveShortest.DistanceSq) { if (lenSq < resolveShortest.DistanceSq) {
resolveShortest.Vector = cornerResolution; resolveShortest.Vector = cornerResolution;
@@ -477,26 +477,6 @@ bool AABBvsTriangle(const AABB& box,
Resolution& bestResolve = takeUp ? resolveUpwards : resolveShortest; Resolution& bestResolve = takeUp ? resolveUpwards : resolveShortest;
outResolution = bestResolve.Vector; outResolution = bestResolve.Vector;
//TODO: Debug stuff.
std::string dbg;
switch (bestResolve.Case) {
case ResolveDimY:
case ResolveDimX:
case ResolveDimZ:
dbg = "Axis";
break;
case Line:
dbg = "Line";
break;
case Corner:
dbg = "Corner";
break;
default:
break;
}
std::string outs = (takeUp ? "Resolve upwards " : "Resolve normal ") + dbg;
//TODO: End debug stuff.
glm::vec3 projNorm; glm::vec3 projNorm;
switch (bestResolve.Case) { switch (bestResolve.Case) {
case ResolveDimY: case ResolveDimY:
@@ -505,14 +485,9 @@ bool AABBvsTriangle(const AABB& box,
isOnGround = true; isOnGround = true;
case ResolveDimX: case ResolveDimX:
case ResolveDimZ: case ResolveDimZ:
{
//If we get here, the resolution is along one coordinate axis. //If we get here, the resolution is along one coordinate axis.
//set velocity to 0 in y if it is along y-axis. //set velocity to 0 in y if it is along y-axis.
outs += isOnGround ? " ground" : " air";
ImGui::Text(outs.c_str());
LOG_INFO(outs.c_str());
return true; return true;
}
case Line: case Line:
projNorm = glm::normalize(outResolution); projNorm = glm::normalize(outResolution);
break; break;
@@ -523,11 +498,9 @@ bool AABBvsTriangle(const AABB& box,
break; break;
} }
isOnGround = false;
//If the collision was not on steep wall or similarly (e.g. walking on the ground), force resolution in y only. //If the collision was not on steep wall or similarly (e.g. walking on the ground), force resolution in y only.
if (FaceIsGround(projNorm.y)) { if (FaceIsGround(projNorm.y)) {
//Ensure that the player always is moved upwards, instead of sliding down. //Ensure that the player always is moved upwards, instead of sliding down.
//Also zero the vertical velocity.
float len = glm::length(outResolution); float len = glm::length(outResolution);
float ang = glm::half_pi<float>() - glm::acos(outResolution.y / len); float ang = glm::half_pi<float>() - glm::acos(outResolution.y / len);
if (len > 0.0000001f && ang > 0.0000001f) { if (len > 0.0000001f && ang > 0.0000001f) {
@@ -535,52 +508,21 @@ bool AABBvsTriangle(const AABB& box,
outResolution.y = len / glm::sin(ang); outResolution.y = len / glm::sin(ang);
outResolution.z = 0; outResolution.z = 0;
} }
//Also zero the vertical velocity, if it is positive, else project it onto the normal.
//Project the velocity onto the normal of the hit line/face. //Project the velocity onto the normal of the hit line/face.
//w = v - <v,n>*n, |n|==1. //w = v - <v,n>*n, |n|==1.
glm::vec3 projVel = boxVelocity - glm::dot(boxVelocity, projNorm) * projNorm; boxVelocity.y = std::min(boxVelocity.y - glm::dot(boxVelocity, projNorm) * projNorm.y, 0.f);
if (abs(originalBoxVelocity.x) < 0.001f && abs(originalBoxVelocity.z) < 0.001f) {
boxVelocity.y = 0.f;
} else {
boxVelocity.y = std::min(projVel.y, 0.f);
}
isOnGround = true; isOnGround = true;
} else if (projNorm.y > 0) { } else {
//Enter here if the triangle is a steep slope, and it is not facing downwards. //Enter here if the triangle is a steep slope, and it is not facing downwards.
//Ensure that the player will be pushed in the xz-plane.
//glm::vec3 moveDir(outResolution);
//moveDir.y = 0;
//if (vectorHasLength(moveDir)) {
// moveDir = glm::normalize(moveDir);
// float len = glm::length(outResolution);
// float dotMoveOut = moveDir.x * outResolution.x + moveDir.z * outResolution.z;
// float ang = glm::half_pi<float>() - glm::acos(dotMoveOut / len);
// if (len > 0.0000001f && ang > 0.0000001f) {
// outResolution = (len / glm::sin(ang)) * moveDir;
// }
//}
//Project the velocity onto the normal of the hit line/face. //Project the velocity onto the normal of the hit line/face.
//w = v - <v,n>*n, |n|==1. //w = v - <v,n>*n, |n|==1.
//TODO: What do we want.. //"ice cream"-effect, air resistance + projected velocity.
#if 0 //Walk up steep walls.
glm::vec3 projVel = boxVelocity - glm::dot(boxVelocity, projNorm) * projNorm;
boxVelocity.y = std::min(projVel.y, 0.f);
isOnGround = true;
#elif 1 //"ice cream"-effect, air resistance + projected velocity.
if (!isOnGround) { if (!isOnGround) {
boxVelocity = boxVelocity - glm::dot(boxVelocity, projNorm) * projNorm; boxVelocity = boxVelocity - glm::dot(boxVelocity, projNorm) * projNorm;
} }
isOnGround = false; isOnGround = false;
#elif 0 //"ice cream"-effect, ground friction (full control) + projected velocity.
if (!isOnGround) {
boxVelocity = boxVelocity - glm::dot(boxVelocity, projNorm) * projNorm;
}
isOnGround = true;
#endif
} }
outs += isOnGround ? " ground" : " air";
ImGui::Text(outs.c_str());
LOG_INFO(outs.c_str());
return true; return true;
} }
+8 -20
View File
@@ -39,26 +39,14 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
glm::vec3 inOutVelocity = (glm::vec3)cPhysics["Velocity"]; glm::vec3 inOutVelocity = (glm::vec3)cPhysics["Velocity"];
bool isOnGround = (bool)cPhysics["IsOnGround"]; bool isOnGround = (bool)cPhysics["IsOnGround"];
//glm::vec3 gravity = glm::vec3(0, 9.82f * dt, 0); float verticalStepHeight = (float)(double)cPhysics["VerticalStepHeight"];
//TODO: glm::abs(inOutVelocity - gravity) doesn't work, because of velocity projection on ground normal. if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, inOutVelocity, verticalStepHeight, isOnGround, resolutionVector)) {
//if (!isOnGround || glm::any(glm::greaterThan(glm::abs(inOutVelocity - gravity), glm::vec3(0.0001f)))) { (glm::vec3&)cTransform["Position"] += resolutionVector;
float verticalStepHeight = (float)(double)cPhysics["VerticalStepHeight"]; cPhysics["Velocity"] = inOutVelocity;
if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, inOutVelocity, verticalStepHeight, isOnGround, resolutionVector)) { (bool)cPhysics["IsOnGround"] = isOnGround;
glm::vec3 pos = (glm::vec3)cTransform["Position"] + resolutionVector; } else {
//Hack: Force the position to be at discrete values after collision, removes jittering. (bool)cPhysics["IsOnGround"] = false;
//constexpr float discreteValue = 1.0e2f; }
//pos = glm::vec3(glm::ivec3(discreteValue * pos)) / discreteValue;
//pos = glm::round(discreteValue * pos) / discreteValue;
(glm::vec3&)cTransform["Position"] = pos;
cPhysics["Velocity"] = inOutVelocity;
(bool)cPhysics["IsOnGround"] = isOnGround;
} else {
(bool)cPhysics["IsOnGround"] = false;
}
//} else {
// (glm::vec3&)cTransform["Position"] -= gravity;
// cPhysics["Velocity"] = glm::vec3(0.f);
//}
} 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;