Why hello there! How nice of you to come by. Have a seat.
My game is 2D-topdown and made in C++. Zeldaish. Love that game. ANYHO, I am trying to figure out how to make a vector contain only relevant Units. You might wanna skip this part and answer my question at once. No f*** you, read my wall of text. I'll try to explain with collisiondetection as example (have in mind this issue is not collision-specific, that's not what I am getting at..). I want to check if any Unit has collided with another Unit. I currently achieve this by doing this:
1. Create a vector which contain all Units.
2. Iterate through it so that all Units have checked collision with each other only once.
Now what if I have 30 Units in my world? If all 30 are to compare with each other only once, we got a total of 435 collisionchecks per frame, y = (x2 - x) / 2. This is inefficient. If there is 40 we get 780 collision checks.. It scales terribly. For some calculations the terrible scale isn't to much of an issue since the operation isn't complicated, but overall this will slow down my game if I don't come up with another way of comparing objects.
Instead I figured I'd like to create a member vector of relevant entities within the Unit - not a vector containing all Entities. This would greatly reduce the need of collisionchecks. Relevant entities would be entities that are close to the unit. I can take advantage of indexing, storing pointers to all Units in a 2-dimensional array that would represent and be identical to my 2-dimensional worldmap-array. Then it would be easy to grab some pointers to relevant, physically close, Units via indexing by coordinates and push them into a member vector of the Unit. Now this vector only contains other Units that is physically close to my unit and the result is that each unit only need to check collision with at most 4-5 Units, instead of all present Units.
In theory this would work out, assuming that creating the index isn't more inefficient than iterating through the vector of all Units (or items, or enemies or whatever object I have to access), but creating the index and maintaining it with moving objects etc is always slower when I try to implement it.
This issue persists with all kind of stuff within my game - not only collisiondetection. I actually got to write this thread since I'm currently at making Items storable within an inventory. When the Unit, my character, should pick up an item that is in front of him I only iterate through a vector of all items and check which one are within range. It'd be unealistic if my character could pick up an item from 100 meters. Well Slenderman has long arms, but this guy is more of a Gnome Urist McTransracialGnome. Same issue here - I want a vector that consists of relevant objects, not all of them. When combating enemies I iterate through a vector containing all enemies to check if the spoon sword hit any, instead of iterating through a vector containing all enemies that are likely to get hit. It's oblivious! Perhaps I shouldn't even iterate through a vector?
If you still haven't grasped my issue I'll make a final try: In Mount&Blade there is lots of players. When I shoot an arrow I can't imagine it iterates through all players every single frame and check if the arrow has collided with any of them? Surely there must be a more efficient way?! What way would that be? What alternatives to that way is there? Any design-pattern I should look into? (I did read GoF: Design Patterns but I grasped approx 10% of it the English language is inferior and nothing could be explained properly. Perhaps anyone have a Swedish translation of it?)
Seriously now, thanks for reading and I'd be REALLY happy if someone could point me in a good direction !