简体   繁体   English

Java处理3(2D)中更好的碰撞检测算法?

[英]better algorithm for collision detection in java processing 3 (2D)?

my program includes increasing number of objects "n", each object has a custom-shape and coordinates . 我的程序包括越来越多的对象“ n”,每个对象都有自定义形状和坐标。 To detect collision for each object i check the distance between it and all other objects to check if its close enough for collision. 为了检测每个物体的碰撞,我检查了它与所有其他物体之间的距离,以检查其是否足够靠近碰撞。 however this would make complexity too high n^(n-1). 但是,这会使复杂度过高n ^(n-1)。 What would be a better algorithm for collision detection for : -simple 2D objects like circles and triangles ? 有什么更好的碰撞检测算法:-简单的2D对象,例如圆形和三角形? -costume object made by vertices and PShape ? 顶点和PShape构成的服装对象?

Short answer: split your world space up into spaces, then only check objects that are in the same space. 简短的答案:将您的世界空间分成多个空间,然后仅检查相同空间中的对象。 Think about it this way: if you have an object that's in the upper-left corner, you don't have to check that against objects in the lower-right corner. 可以这样考虑:如果您有一个对象位于左上角,则不必对照右下角的对象进行检查。 Just other objects in the upper-left corner. 左上角还有其他对象。

There are many ways to implement this. 有很多方法可以实现这一点。 One of the most common approaches is a data structure called a quadtree . 最常见的方法之一是称为四叉树的数据结构。

Another approach would be to use a physics library that does all of the collision detection for you. 另一种方法是使用物理库为您执行所有碰撞检测。

The above two approaches might be overkill if you're just doing something simple though, so it's entirely up to you. 如果您只是做一些简单的事情,则以上两种方法可能会显得过大,因此完全取决于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM