简体   繁体   中英

Collision detection matrix - fast array removal and addition

I am creating a custom collision detection matrix. When my objects fall within a certain matrix I place them in a category and only check collision within that category. That is because I am creating hundreds of thousands of objects. Right now I am using two to three ArrayLists to add and remove things. What would be the most efficient class to use for this kind of behavior

//master object list
//matrix list
//checks all the master object list and if something meets criteria place in the specific matrix list
//remove from matrix list when no longer meets criteria

as you can imagine I am doing thousands upon thousands of loops so what datatype/class can allow me to quickly add and remove items from an array like object.

It seems like just a simple LinkedList would suffice for you here.

You just want to iterate through the "master" list, and if it meets a criteria add to the "matrix" list? Iteration would be O(n) and adding to the end of a linked list is O(1).

Then, you want to iterate through the "matrix" list and remove any that no longer meet the criteria? Again, you can iterate through this list in O(n) and removals are O(1) for a linked list.

Of course if you do not want to iterate through the whole matrix list to find the ones that need to be removed, then you can get fancier depending on the criteria, how it is updated, etc.

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