I am developing a game and I’m having problems. Basically the idea is:
- Player uses a spell at area.
- Engine selects all units in the area and applies an effect on each.
The problem is that the game theoretically possess thousands of units, and run a loop to check if each is within the specified region spend much processing.
Is there any way to index and speed up this process?
Here’s an example (its THEORETICAL!!):
// Create the unit struct
typedef struct UNIT{
int ID;
int x;
int y;
}unit;
// Initialize the global unit struct
unit* allunits;
// Do some code, expand the array and add more units
void addUnits(){
...
}
// Search for the number of units that are inside a random region using for and the unit array
int unitsInRegion(x1, x2, y1, y2){
// Spend alot of processing because there is like 10.000 units and we need to look at each X and Y
}
I want to mix this kind of task with DirectX as a way to streamline the process of rendering excluding models that are not within sight of view (camera) from the buffers.
Sorry for my bad English.
As mentioned in the comments the usual way to do this, are trees like Quadtrees, Octrees or BSP-Trees.
I think youre looking for View Frustum Culling (Tutorial). You can mix this approach with one of the in the comments mentioned trees to get an efficient culling to all objects in your scene.