diff options
Diffstat (limited to 'src/common/Collision/DynamicTree.cpp')
-rw-r--r-- | src/common/Collision/DynamicTree.cpp | 81 |
1 files changed, 29 insertions, 52 deletions
diff --git a/src/common/Collision/DynamicTree.cpp b/src/common/Collision/DynamicTree.cpp index a32ad1e17c8..c0aa9805d8d 100644 --- a/src/common/Collision/DynamicTree.cpp +++ b/src/common/Collision/DynamicTree.cpp @@ -146,53 +146,32 @@ void DynamicMapTree::update(uint32 t_diff) struct DynamicTreeIntersectionCallback { - bool did_hit; - uint32 phase_mask; - DynamicTreeIntersectionCallback(uint32 phasemask) : did_hit(false), phase_mask(phasemask) { } - bool operator()(const G3D::Ray& r, const GameObjectModel& obj, float& distance) - { - did_hit = obj.intersectRay(r, distance, true, phase_mask); - return did_hit; - } - bool didHit() const { return did_hit;} -}; + DynamicTreeIntersectionCallback(std::set<uint32> const& phases) : _didHit(false), _phases(phases) { } -struct DynamicTreeIntersectionCallback_WithLogger -{ - bool did_hit; - uint32 phase_mask; - DynamicTreeIntersectionCallback_WithLogger(uint32 phasemask) : did_hit(false), phase_mask(phasemask) - { - TC_LOG_DEBUG("maps", "Dynamic Intersection log"); - } - bool operator()(const G3D::Ray& r, const GameObjectModel& obj, float& distance) + bool operator()(G3D::Ray const& r, GameObjectModel const& obj, float& distance) { - TC_LOG_DEBUG("maps", "testing intersection with %s", obj.name.c_str()); - bool hit = obj.intersectRay(r, distance, true, phase_mask); - if (hit) - { - did_hit = true; - TC_LOG_DEBUG("maps", "result: intersects"); - } - return hit; + _didHit = obj.intersectRay(r, distance, true, _phases); + return _didHit; } - bool didHit() const { return did_hit;} + + bool didHit() const { return _didHit; } + +private: + bool _didHit; + std::set<uint32> _phases; }; -bool DynamicMapTree::getIntersectionTime(const uint32 phasemask, const G3D::Ray& ray, - const G3D::Vector3& endPos, float& maxDist) const +bool DynamicMapTree::getIntersectionTime(std::set<uint32> const& phases, G3D::Ray const& ray, G3D::Vector3 const& endPos, float& maxDist) const { float distance = maxDist; - DynamicTreeIntersectionCallback callback(phasemask); + DynamicTreeIntersectionCallback callback(phases); impl->intersectRay(ray, callback, distance, endPos); if (callback.didHit()) maxDist = distance; return callback.didHit(); } -bool DynamicMapTree::getObjectHitPos(const uint32 phasemask, const G3D::Vector3& startPos, - const G3D::Vector3& endPos, G3D::Vector3& resultHit, - float modifyDist) const +bool DynamicMapTree::getObjectHitPos(std::set<uint32> const& phases, G3D::Vector3 const& startPos, G3D::Vector3 const& endPos, G3D::Vector3& resultHitPos, float modifyDist) const { bool result = false; float maxDist = (endPos - startPos).magnitude(); @@ -201,56 +180,54 @@ bool DynamicMapTree::getObjectHitPos(const uint32 phasemask, const G3D::Vector3& // prevent NaN values which can cause BIH intersection to enter infinite loop if (maxDist < 1e-10f) { - resultHit = endPos; + resultHitPos = endPos; return false; } G3D::Vector3 dir = (endPos - startPos)/maxDist; // direction with length of 1 G3D::Ray ray(startPos, dir); float dist = maxDist; - if (getIntersectionTime(phasemask, ray, endPos, dist)) + if (getIntersectionTime(phases, ray, endPos, dist)) { - resultHit = startPos + dir * dist; + resultHitPos = startPos + dir * dist; if (modifyDist < 0) { - if ((resultHit - startPos).magnitude() > -modifyDist) - resultHit = resultHit + dir*modifyDist; + if ((resultHitPos - startPos).magnitude() > -modifyDist) + resultHitPos += dir * modifyDist; else - resultHit = startPos; + resultHitPos = startPos; } else - resultHit = resultHit + dir*modifyDist; + resultHitPos += dir * modifyDist; result = true; } else { - resultHit = endPos; + resultHitPos = endPos; result = false; } return result; } -bool DynamicMapTree::isInLineOfSight(float x1, float y1, float z1, float x2, float y2, float z2, uint32 phasemask) const +bool DynamicMapTree::isInLineOfSight(G3D::Vector3 const& startPos, G3D::Vector3 const& endPos, std::set<uint32> const& phases) const { - G3D::Vector3 v1(x1, y1, z1), v2(x2, y2, z2); - - float maxDist = (v2 - v1).magnitude(); + float maxDist = (endPos - startPos).magnitude(); if (!G3D::fuzzyGt(maxDist, 0) ) return true; - G3D::Ray r(v1, (v2-v1) / maxDist); - DynamicTreeIntersectionCallback callback(phasemask); - impl->intersectRay(r, callback, maxDist, v2); + G3D::Ray r(startPos, (endPos - startPos) / maxDist); + DynamicTreeIntersectionCallback callback(phases); + impl->intersectRay(r, callback, maxDist, endPos); - return !callback.did_hit; + return !callback.didHit(); } -float DynamicMapTree::getHeight(float x, float y, float z, float maxSearchDist, uint32 phasemask) const +float DynamicMapTree::getHeight(float x, float y, float z, float maxSearchDist, std::set<uint32> const& phases) const { G3D::Vector3 v(x, y, z + 0.5f); G3D::Ray r(v, G3D::Vector3(0, 0, -1)); - DynamicTreeIntersectionCallback callback(phasemask); + DynamicTreeIntersectionCallback callback(phases); impl->intersectZAllignedRay(r, callback, maxSearchDist); if (callback.didHit()) |