The usual technique to improve efficiency is to put all objects in some kind of space partition data structure in order to limit the number of pairs to something approaching O(n) rather than O(n^2), which is what you have now.
(提高效率的常用技术是将所有对象置于某种空间分区数据结构中,以将对的数量限制为接近O(n)而不是O(n ^ 2)(这就是您现在拥有的)。)
Quadtrees and kd trees are two candidates. (四叉树和kd树是两个候选对象。)
Nodes in these trees represent regions of space. (这些树中的节点代表空间区域。)
Only pairs of objects contained in or intersecting the same region need to be checked. (仅需要检查包含在同一区域中或相交于同一区域的成对对象。)
To avoid missing collisions, comparing points isn't sufficient.
(为了避免丢失碰撞,比较点是不够的。)
Rather, check for intersections of line segments from previous to current positions for each particle. (而是检查每个粒子从先前位置到当前位置的线段的交点。)
Consequently, you want a "edge" quadtree or kd-tree . (因此,您需要“边缘” 四叉树或kd-tree 。)
One other thing to consider is whether to build one space partition and update it between frames or rebuild the partition from scratch for each frame.
(要考虑的另一件事是是构建一个空间分区并在帧之间更新它,还是为每个帧从头开始重建分区。)
The former is much more complicated and may not result in better speed. (前者要复杂得多,可能无法提高速度。)
Try the latter first. (首先尝试后者。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…