游戏的空间数据结构

发布于 2024-10-02 01:22:27 字数 191 浏览 0 评论 0原文

我需要实现一个空间数据结构来存储矩形,然后能够找到与给定矩形相交的所有矩形。这将在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮了一弯 2024-10-09 01:22:27

哈希表作为近似交集测试效果相当好。哈希表用作更复杂算法的一部分,用于检测 中的冲突常微分方程

从逻辑上讲,这个测试将空间划分为规则的网格。每个网格单元都标有与该单元相交的对象列表。通过扫描所有对象来初始化网格。我不懂 javascript,所以我将使用 python-ish 伪代码。

for each ob in objects:
  for each x in [floor(ob.x_min / grid_size) .. floor(ob.x_max / grid_size)]:
    for each y in [floor(ob.y_min / grid_size) .. floor(ob.y_max / grid_size)]:
      hashtable[hash(x, y)].append(ob)

要查找与给定对象的碰撞,请在哈希表中查找接近碰撞,然后对每个对象应用精确的碰撞测试。

near_collisions = []
for each x in [floor(ob.x_min / grid_size) .. floor(ob.x_max / grid_size)]:
  for each y in [floor(ob.y_min / grid_size) .. floor(ob.y_max / grid_size)]:
    near_collisions = near_collisions ++ hashtable[hash(x, y)]

remove duplicates from near_collisions

for each ob2 in near_collisions:
  if exact_collision_test(ob, ob2):
    do_something

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.

for each ob in objects:
  for each x in [floor(ob.x_min / grid_size) .. floor(ob.x_max / grid_size)]:
    for each y in [floor(ob.y_min / grid_size) .. floor(ob.y_max / grid_size)]:
      hashtable[hash(x, y)].append(ob)

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.

near_collisions = []
for each x in [floor(ob.x_min / grid_size) .. floor(ob.x_max / grid_size)]:
  for each y in [floor(ob.y_min / grid_size) .. floor(ob.y_max / grid_size)]:
    near_collisions = near_collisions ++ hashtable[hash(x, y)]

remove duplicates from near_collisions

for each ob2 in near_collisions:
  if exact_collision_test(ob, ob2):
    do_something
想你的星星会说话 2024-10-09 01:22:27

即使有移动的对象,您仍然可以使用四叉树 - 只需在每次移动或每次穿过区域边界时删除并重新插入对象即可。

但是四叉树不太擅长存储矩形,我建议使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文