diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/Collision/DynamicTree.cpp | 81 | ||||
| -rw-r--r-- | src/common/Collision/DynamicTree.h | 14 | ||||
| -rw-r--r-- | src/common/Collision/Models/GameObjectModel.cpp | 14 | ||||
| -rw-r--r-- | src/common/Collision/Models/GameObjectModel.h | 17 |
4 files changed, 49 insertions, 77 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()) diff --git a/src/common/Collision/DynamicTree.h b/src/common/Collision/DynamicTree.h index cbb7c7fdaef..f52c5364362 100644 --- a/src/common/Collision/DynamicTree.h +++ b/src/common/Collision/DynamicTree.h @@ -40,17 +40,11 @@ public: DynamicMapTree(); ~DynamicMapTree(); - bool isInLineOfSight(float x1, float y1, float z1, float x2, float y2, - float z2, uint32 phasemask) const; + bool isInLineOfSight(G3D::Vector3 const& startPos, G3D::Vector3 const& endPos, std::set<uint32> const& phases) const; + bool getIntersectionTime(std::set<uint32> const& phases, G3D::Ray const& ray, G3D::Vector3 const& endPos, float& maxDist) const; + bool getObjectHitPos(std::set<uint32> const& phases, G3D::Vector3 const& startPos, G3D::Vector3 const& endPos, G3D::Vector3& resultHitPos, float modifyDist) const; - bool getIntersectionTime(uint32 phasemask, const G3D::Ray& ray, - const G3D::Vector3& endPos, float& maxDist) const; - - bool getObjectHitPos(uint32 phasemask, const G3D::Vector3& pPos1, - const G3D::Vector3& pPos2, G3D::Vector3& pResultHitPos, - float pModifyDist) const; - - float getHeight(float x, float y, float z, float maxSearchDist, uint32 phasemask) const; + float getHeight(float x, float y, float z, float maxSearchDist, std::set<uint32> const& phases) const; void insert(const GameObjectModel&); void remove(const GameObjectModel&); diff --git a/src/common/Collision/Models/GameObjectModel.cpp b/src/common/Collision/Models/GameObjectModel.cpp index b111b0de301..f3e5d9f6e64 100644 --- a/src/common/Collision/Models/GameObjectModel.cpp +++ b/src/common/Collision/Models/GameObjectModel.cpp @@ -115,7 +115,6 @@ bool GameObjectModel::initialize(std::unique_ptr<GameObjectModelOwnerBase> model name = it->second.name; iPos = modelOwner->GetPosition(); - phasemask = modelOwner->GetPhaseMask(); iScale = modelOwner->GetScale(); iInvScale = 1.f / iScale; @@ -153,9 +152,12 @@ GameObjectModel* GameObjectModel::Create(std::unique_ptr<GameObjectModelOwnerBas return mdl; } -bool GameObjectModel::intersectRay(const G3D::Ray& ray, float& MaxDist, bool StopAtFirstHit, uint32 ph_mask) const +bool GameObjectModel::intersectRay(G3D::Ray const& ray, float& maxDist, bool stopAtFirstHit, std::set<uint32> const& phases) const { - if (!(phasemask & ph_mask) || !owner->IsSpawned()) + if (!isCollisionEnabled() || !owner->IsSpawned()) + return false; + + if (!owner->IsInPhase(phases)) return false; float time = ray.intersectionTime(iBound); @@ -165,12 +167,12 @@ bool GameObjectModel::intersectRay(const G3D::Ray& ray, float& MaxDist, bool Sto // child bounds are defined in object space: Vector3 p = iInvRot * (ray.origin() - iPos) * iInvScale; Ray modRay(p, iInvRot * ray.direction()); - float distance = MaxDist * iInvScale; - bool hit = iModel->IntersectRay(modRay, distance, StopAtFirstHit); + float distance = maxDist * iInvScale; + bool hit = iModel->IntersectRay(modRay, distance, stopAtFirstHit); if (hit) { distance *= iScale; - MaxDist = distance; + maxDist = distance; } return hit; } diff --git a/src/common/Collision/Models/GameObjectModel.h b/src/common/Collision/Models/GameObjectModel.h index a9fce400146..477d49ccf80 100644 --- a/src/common/Collision/Models/GameObjectModel.h +++ b/src/common/Collision/Models/GameObjectModel.h @@ -26,6 +26,7 @@ #include "Define.h" #include <memory> +#include <set> namespace VMAP { @@ -40,7 +41,7 @@ class TC_COMMON_API GameObjectModelOwnerBase public: virtual bool IsSpawned() const { return false; } virtual uint32 GetDisplayId() const { return 0; } - virtual uint32 GetPhaseMask() const { return 0; } + virtual bool IsInPhase(std::set<uint32> const& phases) const { return false; } virtual G3D::Vector3 GetPosition() const { return G3D::Vector3::zero(); } virtual float GetOrientation() const { return 0.0f; } virtual float GetScale() const { return 1.0f; } @@ -49,7 +50,7 @@ public: class TC_COMMON_API GameObjectModel /*, public Intersectable*/ { - GameObjectModel() : phasemask(0), iInvScale(0), iScale(0), iModel(NULL) { } + GameObjectModel() : _collisionEnabled(false), iInvScale(0), iScale(0), iModel(nullptr) { } public: std::string name; @@ -59,13 +60,11 @@ public: const G3D::Vector3& getPosition() const { return iPos;} - /** Enables\disables collision. */ - void disable() { phasemask = 0;} - void enable(uint32 ph_mask) { phasemask = ph_mask;} + /* Enables/disables collision */ + void enableCollision(bool enable) { _collisionEnabled = enable; } + bool isCollisionEnabled() const { return _collisionEnabled; } - bool isEnabled() const {return phasemask != 0;} - - bool intersectRay(const G3D::Ray& Ray, float& MaxDist, bool StopAtFirstHit, uint32 ph_mask) const; + bool intersectRay(G3D::Ray const& ray, float& maxDist, bool stopAtFirstHit, std::set<uint32> const& phases) const; static GameObjectModel* Create(std::unique_ptr<GameObjectModelOwnerBase> modelOwner, std::string const& dataPath); @@ -74,7 +73,7 @@ public: private: bool initialize(std::unique_ptr<GameObjectModelOwnerBase> modelOwner, std::string const& dataPath); - uint32 phasemask; + bool _collisionEnabled; G3D::AABox iBound; G3D::Matrix3 iInvRot; G3D::Vector3 iPos; |
