简体   繁体   中英

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 . 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). What would be a better algorithm for collision detection for : -simple 2D objects like circles and triangles ? -costume object made by vertices and 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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