diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common/Collision/DynamicTree.cpp | 7 | ||||
-rw-r--r-- | src/common/Collision/DynamicTree.h | 1 | ||||
-rw-r--r-- | src/common/Collision/RegularGrid.h | 51 |
3 files changed, 28 insertions, 31 deletions
diff --git a/src/common/Collision/DynamicTree.cpp b/src/common/Collision/DynamicTree.cpp index 1c6e856e46d..38e10318a57 100644 --- a/src/common/Collision/DynamicTree.cpp +++ b/src/common/Collision/DynamicTree.cpp @@ -92,7 +92,7 @@ struct DynTreeImpl : public ParentTree/*, public Intersectable*/ void update(uint32 difftime) { - if (!size()) + if (empty()) return; rebalance_timer.Update(difftime); @@ -135,11 +135,6 @@ void DynamicMapTree::balance() impl->balance(); } -int DynamicMapTree::size() const -{ - return impl->size(); -} - void DynamicMapTree::update(uint32 t_diff) { impl->update(t_diff); diff --git a/src/common/Collision/DynamicTree.h b/src/common/Collision/DynamicTree.h index cbb7c7fdaef..49458393906 100644 --- a/src/common/Collision/DynamicTree.h +++ b/src/common/Collision/DynamicTree.h @@ -55,7 +55,6 @@ public: void insert(const GameObjectModel&); void remove(const GameObjectModel&); bool contains(const GameObjectModel&) const; - int size() const; void balance(); void update(uint32 diff); diff --git a/src/common/Collision/RegularGrid.h b/src/common/Collision/RegularGrid.h index 2f3819c872c..edc1ab6c36b 100644 --- a/src/common/Collision/RegularGrid.h +++ b/src/common/Collision/RegularGrid.h @@ -1,13 +1,12 @@ #ifndef _REGULAR_GRID_H #define _REGULAR_GRID_H - +#include "Containers.h" +#include "Errors.h" #include <G3D/Ray.h> -#include <G3D/Table.h> #include <G3D/BoundsTrait.h> #include <G3D/PositionTrait.h> - -#include "Errors.h" +#include <unordered_map> template<class Node> struct NodeCreator{ @@ -17,7 +16,7 @@ struct NodeCreator{ template<class T, class Node, class NodeCreatorFunc = NodeCreator<Node>, - /*class BoundsFunc = BoundsTrait<T>,*/ +class BoundsFunc = BoundsTrait<T>, class PositionFunc = PositionTrait<T> > class TC_COMMON_API RegularGrid2D @@ -31,16 +30,18 @@ public: #define HGRID_MAP_SIZE (533.33333f * 64.f) // shouldn't be changed #define CELL_SIZE float(HGRID_MAP_SIZE/(float)CELL_NUMBER) - typedef G3D::Table<const T*, Node*> MemberTable; + typedef std::unordered_multimap<const T*, Node*> MemberTable; MemberTable memberTable; Node* nodes[CELL_NUMBER][CELL_NUMBER]; - RegularGrid2D(){ + RegularGrid2D() + { memset(nodes, 0, sizeof(nodes)); } - ~RegularGrid2D(){ + ~RegularGrid2D() + { for (int x = 0; x < CELL_NUMBER; ++x) for (int y = 0; y < CELL_NUMBER; ++y) delete nodes[x][y]; @@ -48,18 +49,27 @@ public: void insert(const T& value) { - G3D::Vector3 pos; - PositionFunc::getPosition(value, pos); - Node& node = getGridFor(pos.x, pos.y); - node.insert(value); - memberTable.set(&value, &node); + G3D::AABox bounds; + BoundsFunc::getBounds(value, bounds); + Cell low = Cell::ComputeCell(bounds.low().x, bounds.low().y); + Cell high = Cell::ComputeCell(bounds.high().x, bounds.high().y); + for (int x = low.x; x <= high.x; ++x) + { + for (int y = low.y; y <= high.y; ++y) + { + Node& node = getGrid(x, y); + node.insert(value); + memberTable.insert(MemberTable::value_type(&value, &node)); + } + } } void remove(const T& value) { - memberTable[&value]->remove(value); + for (MemberTable::value_type& p : Trinity::Containers::MapEqualRange(memberTable, &value)) + p.second->remove(value); // Remove the member - memberTable.remove(&value); + memberTable.erase(&value); } void balance() @@ -70,8 +80,8 @@ public: n->balance(); } - bool contains(const T& value) const { return memberTable.containsKey(&value); } - int size() const { return uint32(memberTable.size()); } + bool contains(const T& value) const { return memberTable.count(&value) > 0; } + bool empty() const { return memberTable.empty(); } struct Cell { @@ -87,13 +97,6 @@ public: bool isValid() const { return x >= 0 && x < CELL_NUMBER && y >= 0 && y < CELL_NUMBER;} }; - - Node& getGridFor(float fx, float fy) - { - Cell c = Cell::ComputeCell(fx, fy); - return getGrid(c.x, c.y); - } - Node& getGrid(int x, int y) { ASSERT(x < CELL_NUMBER && y < CELL_NUMBER); |