cleanup and a few small optimizations

This commit is contained in:
Andrew Meadows 2018-02-23 16:09:03 -08:00
parent 79b9fec900
commit 6ab85d5800
2 changed files with 18 additions and 24 deletions

View file

@ -598,13 +598,13 @@ EntityItemID EntityTreeElement::findRayIntersection(const glm::vec3& origin, con
EntityItemID result; EntityItemID result;
float distanceToElementCube = std::numeric_limits<float>::max(); float distanceToElementCube = std::numeric_limits<float>::max();
float distanceToElementDetails = distance;
BoxFace localFace; BoxFace localFace;
glm::vec3 localSurfaceNormal; glm::vec3 localSurfaceNormal;
QVariantMap localExtraInfo;
// if the ray doesn't intersect with our cube, we can stop searching! // if the ray doesn't intersect with our cube OR the distance to element is less than current best distance
if (!_cube.findRayIntersection(origin, direction, distanceToElementCube, localFace, localSurfaceNormal)) { // we can stop searching!
if (!_cube.findRayIntersection(origin, direction, distanceToElementCube, localFace, localSurfaceNormal)
|| (!_cube.contains(origin) && distanceToElementCube > distance)) {
keepSearching = false; // no point in continuing to search keepSearching = false; // no point in continuing to search
return result; // we did not intersect return result; // we did not intersect
} }
@ -616,20 +616,17 @@ EntityItemID EntityTreeElement::findRayIntersection(const glm::vec3& origin, con
// if the distance to the element cube is not less than the current best distance, then it's not possible // if the distance to the element cube is not less than the current best distance, then it's not possible
// for any details inside the cube to be closer so we don't need to consider them. // for any details inside the cube to be closer so we don't need to consider them.
if (_cube.contains(origin) || distanceToElementCube < distance) { QVariantMap localExtraInfo;
float distanceToElementDetails = distance;
EntityItemID entityID = findDetailedRayIntersection(origin, direction, keepSearching, element, distanceToElementDetails, EntityItemID entityID = findDetailedRayIntersection(origin, direction, keepSearching, element, distanceToElementDetails,
face, localSurfaceNormal, entityIdsToInclude, entityIdsToDiscard, visibleOnly, collidableOnly, face, localSurfaceNormal, entityIdsToInclude, entityIdsToDiscard, visibleOnly, collidableOnly,
localExtraInfo, precisionPicking, distanceToElementCube); localExtraInfo, precisionPicking);
if (!entityID.isNull()) { if (!entityID.isNull() && distanceToElementDetails < distance) {
if (distanceToElementDetails < distance) { distance = distanceToElementDetails;
distance = distanceToElementDetails; face = localFace;
face = localFace; surfaceNormal = localSurfaceNormal;
surfaceNormal = localSurfaceNormal; extraInfo = localExtraInfo;
extraInfo = localExtraInfo; result = entityID;
result = entityID;
}
}
} }
return result; return result;
} }
@ -637,7 +634,7 @@ EntityItemID EntityTreeElement::findRayIntersection(const glm::vec3& origin, con
EntityItemID EntityTreeElement::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction, bool& keepSearching, EntityItemID EntityTreeElement::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction, bool& keepSearching,
OctreeElementPointer& element, float& distance, BoxFace& face, glm::vec3& surfaceNormal, OctreeElementPointer& element, float& distance, BoxFace& face, glm::vec3& surfaceNormal,
const QVector<EntityItemID>& entityIdsToInclude, const QVector<EntityItemID>& entityIDsToDiscard, const QVector<EntityItemID>& entityIdsToInclude, const QVector<EntityItemID>& entityIDsToDiscard,
bool visibleOnly, bool collidableOnly, QVariantMap& extraInfo, bool precisionPicking, float distanceToElementCube) { bool visibleOnly, bool collidableOnly, QVariantMap& extraInfo, bool precisionPicking) {
// only called if we do intersect our bounding cube, but find if we actually intersect with entities... // only called if we do intersect our bounding cube, but find if we actually intersect with entities...
int entityNumber = 0; int entityNumber = 0;
@ -650,10 +647,7 @@ EntityItemID EntityTreeElement::findDetailedRayIntersection(const glm::vec3& ori
if (!success) { if (!success) {
return; return;
} }
glm::vec3 sphereCenter = entityBox.calcCenter() - origin; if (!rayHitsSphere(origin, direction, entityBox.calcCenter(), 0.25f * glm::length2(entityBox.getScale()))) {
float r2 = 0.25f * glm::length2(entityBox.getScale());
float d = glm::dot(sphereCenter, direction);
if (glm::length2(sphereCenter) > r2 && (glm::abs(d) > 0.0f && glm::distance2(d * direction, sphereCenter) > r2)) {
return; return;
} }

View file

@ -155,7 +155,7 @@ public:
bool& keepSearching, OctreeElementPointer& element, float& distance, bool& keepSearching, OctreeElementPointer& element, float& distance,
BoxFace& face, glm::vec3& surfaceNormal, const QVector<EntityItemID>& entityIdsToInclude, BoxFace& face, glm::vec3& surfaceNormal, const QVector<EntityItemID>& entityIdsToInclude,
const QVector<EntityItemID>& entityIdsToDiscard, bool visibleOnly, bool collidableOnly, const QVector<EntityItemID>& entityIdsToDiscard, bool visibleOnly, bool collidableOnly,
QVariantMap& extraInfo, bool precisionPicking, float distanceToElementCube); QVariantMap& extraInfo, bool precisionPicking);
virtual bool findSpherePenetration(const glm::vec3& center, float radius, virtual bool findSpherePenetration(const glm::vec3& center, float radius,
glm::vec3& penetration, void** penetratedObject) const override; glm::vec3& penetration, void** penetratedObject) const override;