【问题标题】:What data structure to use for storing a 2D cell-based map of GameObjects?使用什么数据结构来存储基于 2D 单元格的 GameObjects 地图?
【发布时间】:2017-08-14 10:38:18
【问题描述】:

我知道您可能正在考虑 2D 数组或 2D 矢量,但请听我说。

我实际上已经在使用 2D 数组来制作瓷砖地图,效果很好。但是,我正在尝试开发一种数据结构,该数据结构可以存储 游戏对象,该对象位于此类瓷砖地图的顶部

我的要求如下:

  • 对象映射必须允许多个对象定位在同一个单元格中

  • 1234563 /p>
  • 优选地,对象映射还应提供快速查找以确定1:给定单元格中有哪些对象,2:给定对象当前是否在对象图上的某处和3:给定对象在对象图上的位置

我已经起草了一个使用两个 STL 容器的基本数据结构:

  • 3D std::map<int, std::map<int, std::vector<Object*>>> 用于提供可迭代、易于剔除的容器。使用 std::vector 以便在同一个单元格中可以包含许多对象。也可以通过 _map[x][y] 访问单元格。

  • 1234563 Vec2<int>* 是指针的原因是,GameObject 可以向 ObjectMap 询问它在地图上的位置,可能会保存它,然后在将来无需搜索即可立即访问它。

根据我的要求,有没有比我用过的更合适的容器?

如果有帮助,我已将我的 ObjectMap 代码粘贴在下面:

#pragma once
#include <vector>
#include <map>

template<typename T>
struct Vec2 {
    Vec2() { x = 0; y = 0; }
    Vec2(T xVal, T yVal) : x(xVal), y(yVal) {}
    void Set(T xVal, T yVal) { x = xVal; y = yVal; }
    T x, y;
    Vec2& operator+=(const Vec2& rhs) { x += rhs.x; y += rhs.y; return *this; }
    Vec2 operator+(const Vec2& rhs) { return Vec2<T>(x + rhs.x, y + rhs.y); }
};

/// <summary>
/// Represents a map of objects that can be layered on top of a cell-based map
/// Allows for multiple objects per map cell
/// </summary>
template <typename Object>
class ObjectMap {
public:
    /// <summary>
    /// Gets the objects located at the given map cell
    /// </summary>
    /// <param name="row">The row of the cell to inspect</param>
    /// <param name="column">The column of the cell to inspect</param>
    /// <returns>
    /// A pointer to a vector of objects residing at the given cell.
    /// Returns a nullptr if there are no objects at the cell.
    /// </returns>
    std::vector<Object*>* At(int row, int column);

    /// <summary>
    /// Checks whether the ObjectMap contains the given object
    /// </summary>
    /// <param name="object">A pointer to the object to check for</param>
    /// <returns>True if the ObjectMap contains the object</returns>
    bool Contains(Object* object);

    /// <summary>
    /// Adds the given object to the ObjectMap at the given cell
    /// </summary>
    /// <param name="object">The object to add to the map</param>
    /// <param name="row">The row of the cell to add the object to</param>
    /// <param name="column">The column of the cell to add the object to</param>
    /// <returns>True if successful, false if the object is already in the ObjectMap</returns>
    bool Add(Object* object, int row, int column);

    /// <summary>
    /// Moves the given object by some number of rows and columns
    /// </summary>
    /// <param name="object">The object to move</param>
    /// <param name="rows">The number of rows to move the object by</param>
    /// <param name="columns">The number of columns to move the object by</param>
    /// <returns>True if successful, false if the object does not exist in the ObjectMap</returns>
    bool MoveBy(Object* object, int rows, int columns);

    /// <summary>
    /// Moves the given object to the given cell
    /// </summary>
    /// <param name="object">The object to move</param>
    /// <param name="row">The row of the cell to move the object to</param>
    /// <param name="column">The column of the cell to move the object to</param>
    /// <returns>True if successful, false if the object does not exist in the ObjectMap</returns>
    bool MoveTo(Object* object, int row, int column);

    /// <summary>
    /// Gets the position of the given object
    /// </summary>
    /// <param name="object">A pointer to the object to check the position of</param>
    /// <returns>
    /// A pointer to the position of the object.
    /// Returns a nullptr if the object does not exist in the ObjectMap.
    /// </returns>
    Vec2<int>* GetPosition(Object* object);
private:
    /// <summary>
    /// A 3D container allowing object access via cell positions
    /// Provides the ability to iterate across sections of the map
    /// Useful for object culling and rendering
    /// Useful for object lookup when the position is known
    /// Example: _map[a][b] is a vector objects positioned at the map cell (x=a,y=b)
    /// </summary>
    std::map<int, std::map<int, std::vector<Object*>>> _map;

    /// <summary>
    /// A 1D container of all objects and pointers to their positions
    /// Useful for quickly checking whether an object exists
    /// Useful for quickly getting the location of an object
    /// </summary>
    std::map<Object*, Vec2<int>*> _objects;
};

/// 
/// ObjectMap.tpp
/// The implementation has not been separated into a .cpp file because templated 
/// functions must be implemented in header files.
/// 
/// See http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
/// 
#include <algorithm>

template <typename Object>
std::vector<Object*>* ObjectMap<Object>::At(int column, int row) {
    // Checks whether a key exists for the given column
    if (_map.find(column) != _map.end()) {
        // Checks whether a key exists for the given row
        if (_map.at(column).find(row) != _map.at(column).end()) {
            // Return the objects residing in the cell
            return &_map.at(column).at(row);
        }
    }
    return nullptr;
}

template <typename Object>
bool ObjectMap<Object>::Contains(Object* object) {
    return _objects.find(object) != _objects.end();
}

template <typename Object>
bool ObjectMap<Object>::Add(Object* object, int column, int row) {
    if (!Contains(object)) {
        _objects[object] = new Vec2<int>(column, row);
        _map[column][row].push_back(object);
        return true;
    }
    return false;
}

template <typename Object>
bool ObjectMap<Object>::MoveBy(Object* object, int columns, int rows) {
    Vec2<int> newPosition = *_objects[object] + Vec2<int>(columns, rows);
    return MoveTo(object, newPosition.x, newPosition.y);
}

template <typename Object>
bool ObjectMap<Object>::MoveTo(Object* object, int column, int row) {
    if (Contains(object)) {
        // Get the position reference of the object
        Vec2<int>* position = _objects[object];

        // Erase the object from its current position in the map
        auto *oldTile = &_map[position->x][position->y];
        oldTile->erase(std::remove(oldTile->begin(), oldTile->end(), object), oldTile->end());

        // Erase any newly-empty keys from the map
        if (oldTile->size() == 0) {
            _map[position->x].erase(_map[position->x].find(position->y));
            if (_map[position->x].size() == 0) {
                _map.erase(_map.find(position->x));
            }
        }

        // Add the object to its new position on the map
        _map[column][row].push_back(object);

        // Set the position of the object
        position->Set(column, row);

        return true;
    }

    return false;
}

template <typename Object>
Vec2<int>* ObjectMap<Object>::GetPosition(Object * object) {
    if (Contains(object)) {
        return _objects[object];
    }
    return nullptr;
}

【问题讨论】:

  • 您应该使用arrayvector 提供O(1) 按位置查找来存储地图。您可以使用另一个地图有效地进行反向查找。
  • 数组是不可能的,因为我希望能够在一个位置存储未知数量的对象。我也不知道向量是否合适,因为如果屏幕上的对象很少,我仍然需要遍历 每个 位置来渲染它们。使用地图可以让我遍历实际包含对象的位置。
  • 好吧,我的意思是 std::map&lt;int, std::map&lt;int, std::vector&lt;Object*&gt;&gt;&gt; _map 可能是 std::vector&lt;Object*&gt;&gt; _map[X][Y]
  • 对于您编辑的评论,数组提供了非常快速的逐个位置查找,您当然也可以使用其他方法进行逐个对象的快速查找(或者您可以将此信息存储在对象本身)
  • 谢谢 - 不过我不是必须使用 3D 矢量吗?可能用 nullptrs 填充空单元格?否则我将无法通过 [x][y] 访问

标签: c++ arrays c++11 dictionary stl


【解决方案1】:

您未指定问题的一个重要部分:您的地图单元中有多少百分比无法容纳对象?

  • 如果该百分比非常高(至少 95%),您的 map&lt;int, map&lt;int, vector&lt;&gt;&gt;&gt; 方法看起来不错。

  • 如果这个百分比刚刚高,vector&lt;map&lt;int, vector&lt;&gt;&gt;&gt; 会更好。

  • 如果该百分比适中(接近 50% 或更低),您应该选择vector&lt;vector&lt;vector&lt;&gt;&gt;&gt;

这背后的原因是,在实际使用大多数元素的情况下,std::vector&lt;&gt;std::map&lt;int, &gt; 高效得多。对std::vector&lt;&gt; 进行索引意味着只需一点指针运算和一次内存访问,而对std::map&lt;&gt; 进行索引意味着O(log(n)) 内存访问。对于 128 个条目,这已经是 7 倍。 std::map&lt;&gt; 仅具有在存在未使用索引的情况下减少内存消耗的优势,这可能会使稀疏使用的 std::vector&lt;&gt; 膨胀。

现在,如果您未使用的单元格数量不是很高,您必须预计 2D 数组的几乎每一行都被填充了一些东西。因此,保存这些行的容器应该是vector&lt;&gt;。如果您期望条目的密度相当大,则同样的论点也适用于对行本身使用 vector&lt;&gt;


如果您的对象在任何给定时间只能位于地图中的一个位置,您也可以考虑将它们链接到一个侵入式链接列表中。在这种情况下,您会将地图容器从 3D 拖放到 2D,然后遍历恰好位于同一 bin 中的对象的链接列表。

当然,这只是一个有效的选项,只要对象的链表预计平均非常小。

【讨论】:

  • 非常感谢!确实,正如您所说,只要我没有太多对象,我认为地图解决方案是最好的。由于我希望即使有许多对象也能灵活地进行快速查找,所以我可能会尝试您对 std::vector<:vector>> 的建议。我仍然需要一些方法来通过 Object 进行反向查找,因此我可能另外保留我的 std::map> 用于此目的。
  • 如果反向查找的效率是一个问题,您可能希望将Vec2&lt;int&gt; 移动到对象本身。这将避免另一个 O(log(n)) 树遍历来执行查找,以及更多内存效率。或者至少将std::map&lt;Object*, Vec2&lt;int&gt;&gt; 替换为std::unordered_map&lt;Object*, Vec2&lt;int&gt;&gt;unordered_map&lt;&gt; 是哈希表的标准库名称,它肯定会比map&lt;&gt; 快,并且它允许像任何其他容器一样迭代。
  • 我的经验法则是:如果需要考虑性能,请按照优先顺序使用vector&lt;&gt;unordered_set&lt;&gt;unordered_map&lt;&gt;。我还没有发现任何其他标准容器a)更适合该目的并且b)没有明显变慢的情况。所以我只是倾向于忘记那些其他容器......
【解决方案2】:

您可能想要研究二进制空间分区,它提供了非常快的查找时间,例如屏幕上的对象。

对于网格,一个好的空间结构是四叉树。

【讨论】:

  • 谢谢!我以前看过 QuadTrees,但更多的是为了简化我必须为地形进行的渲染调用的数量。我没想过将它应用于对象地图。
猜你喜欢
  • 1970-01-01
  • 2019-04-09
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 2015-11-19
  • 2013-03-11
相关资源
最近更新 更多