游戏的空间数据结构
我需要实现一个空间数据结构来存储矩形,然后能够找到与给定矩形相交的所有矩形。这将在 JavaScript 中实现。
到目前为止,我正在开发一个四叉树来减少搜索空间,但因为它是用于游戏的,所以所有移动的对象都需要更新其在树中的位置。回到第一点。
有什么数据结构或方法可以提供帮助吗?它需要处理大约 10,000 个对象,因此暴力是不够的。
I need to implement a spatial data structure to store rectangles then be able to find all rectangles that intersect a given rectangle. This will be implemented in JavaScript.
So far I am developing a Quad Tree to cut down the search space but because it is for a game, all objects that move will need to update its position in the tree. Back to square one.
Are there any data-structures or methods to help? It will need to process around 10,000 objects so brute force isn't good enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哈希表作为近似交集测试效果相当好。哈希表用作更复杂算法的一部分,用于检测 中的冲突常微分方程。
从逻辑上讲,这个测试将空间划分为规则的网格。每个网格单元都标有与该单元相交的对象列表。通过扫描所有对象来初始化网格。我不懂 javascript,所以我将使用 python-ish 伪代码。
要查找与给定对象的碰撞,请在哈希表中查找接近碰撞,然后对每个对象应用精确的碰撞测试。
A hash table works fairly well as an approximate intersection test. Hash tables are used as part of a more sophisticated algorithm for detecting collisions in ODE.
Logically, this test divides the space into a regular grid. Each grid cell is labeled with a list of objects that intersect that cell. The grid is initialized by scanning all objects. I don't know javascript, so I'll use python-ish pseudocode.
To find collisions with a given object, look up near-collisions in the hash table and then apply an exact collision test to each one.
即使有移动的对象,您仍然可以使用四叉树 - 只需在每次移动或每次穿过区域边界时删除并重新插入对象即可。
但是四叉树不太擅长存储矩形,我建议使用 R-tree反而。
You can still use quadtree even if you have moving objects – just remove and reinsert an object every time it moves or every time it crosses region boundary.
But quadtrees aren't very good at storing rectangles and I would recommend using an R-tree instead.