- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我知道您可能在考虑二维数组或二维 vector ,但请听我说完。
我实际上已经在使用 2D 阵列来制作瓷砖 map ,效果很好。但是,我正在尝试开发一种数据结构,可以将游戏对象 放置在此类 map 的顶部。
我的要求如下:
对象映射必须允许多个对象位于同一个单元格
对象映射必须可以按行和列进行迭代,这样我就可以将迭代空间剔除到特定坐标(这样我就可以只渲染实际在屏幕上的对象等)
最好,对象映射还应该提供快速查找以确定1:给定单元格中有哪些对象,2:给定对象当前是否在在对象 map 上的某处和3:给定对象在对象 map 上的位置
我已经起草了一个使用两个 STL 容器的基本数据结构:
3D std::map<int, std::map<int, std::vector<Object*>>>
用于提供可迭代、易于剔除的容器。使用 std::vector 以便可以在同一个单元格中包含多个对象。也可以通过 _map[x][y] 访问单元格。
此外,我正在使用一维 std::map<Object*, Vec2<int>*>
,它包含与 3D std::map 完全相同的对象,但我认为它是一维的,因此可以更快地进行搜索。原因Vec2<int>*
是一个指针,因此 GameObject 可以向 ObjectMap 询问它在 map 上的位置,可能会保存它,然后在将来无需搜索即可立即访问它。
根据我的要求,是否有比我使用过的容器更合适的容器可用?
如果有帮助,我已将我的 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;
}
最佳答案
您可能想查看二进制空间分区,它提供非常快的查找时间,例如屏幕上的对象。
对于网格,一个好的空间结构是四叉树。
关于c++ - 使用什么数据结构来存储基于 2D 单元格的 GameObjects map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42949547/
我目前正在尝试基于哈希表构建字典。逻辑是:有一个名为 HashTable 的结构,其中包含以下内容: HashFunc HashFunc; PrintFunc PrintEntry; CompareF
如果我有一个指向结构/对象的指针,并且该结构/对象包含另外两个指向其他对象的指针,并且我想删除“包含这两个指针的对象而不破坏它所持有的指针”——我该怎么做这样做吗? 指向对象 A 的指针(包含指向对象
像这样的代码 package main import "fmt" type Hello struct { ID int Raw string } type World []*Hell
我有一个采用以下格式的 CSV: Module, Topic, Sub-topic 它需要能够导入到具有以下格式的 MySQL 数据库中: CREATE TABLE `modules` ( `id
通常我使用类似的东西 copy((uint8_t*)&POD, (uint8_t*)(&POD + 1 ), back_inserter(rawData)); copy((uint8_t*)&PODV
错误 : 联合只能在具有兼容列类型的表上执行。 结构(层:字符串,skyward_number:字符串,skyward_points:字符串)<> 结构(skyward_number:字符串,层:字符
我有一个指向结构的指针数组,我正在尝试使用它们进行 while 循环。我对如何准确初始化它并不完全有信心,但我一直这样做: Entry *newEntry = malloc(sizeof(Entry)
我正在学习 C,我的问题可能很愚蠢,但我很困惑。在这样的函数中: int afunction(somevariables) { if (someconditions)
我现在正在做一项编程作业,我并没有真正完全掌握链接,因为我们还没有涉及它。但是我觉得我需要它来做我想做的事情,因为数组还不够 我创建了一个结构,如下 struct node { float coef;
给定以下代码片段: #include #include #define MAX_SIZE 15 typedef struct{ int touchdowns; int intercepti
struct contact list[3]; int checknullarray() { for(int x=0;x<10;x++) { if(strlen(con
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Empty “for” loop in Facebook ajax what does AJAX call
我刚刚在反射器中浏览了一个文件,并在结构构造函数中看到了这个: this = new Binder.SyntaxNodeOrToken(); 我以前从未见过该术语。有人能解释一下这个赋值在 C# 中的
我经常使用字符串常量,例如: DICT_KEY1 = 'DICT_KEY1' DICT_KEY2 = 'DICT_KEY2' ... 很多时候我不介意实际的文字是什么,只要它们是独一无二的并且对人类读
我是 C 的新手,我不明白为什么下面的代码不起作用: typedef struct{ uint8_t a; uint8_t* b; } test_struct; test_struct
您能否制作一个行为类似于内置类之一的结构,您可以在其中直接分配值而无需调用属性? 前任: RoundedDouble count; count = 5; 而不是使用 RoundedDouble cou
这是我的代码: #include typedef struct { const char *description; float value; int age; } swag
在创建嵌套列表时,我认为 R 具有对列表元素有用的命名结构。我有一个列表列表,并希望应用包含在任何列表中的每个向量的函数。 lapply这样做但随后剥离了列表的命名结构。我该怎么办 lapply嵌套列
我正在做一个用于学习目的的个人组织者,我从来没有使用过 XML,所以我不确定我的解决方案是否是最好的。这是我附带的 XML 文件的基本结构:
我是新来的 nosql概念,所以当我开始学习时 PouchDB ,我找到了这个转换表。我的困惑是,如何PouchDB如果可以说我有多个表,是否意味着我需要创建多个数据库?因为根据我在 pouchdb
我是一名优秀的程序员,十分优秀!