i’m working on this game where i’ll have a few rather large levels, so for my collision detection i’ll have to check a high number of walls and enemies to see if my character is colliding with them… currently the begining of my collision code looks something like this:
for(i=0;i<=number_of_enemies;i++)
{
dx=player._x-_root["enemy"+i]._x;
if(dx<200) // 200 is larger then twice the maximum width of a character
{
dy=player._y-_root["enemy"+i]._y;
if(dy<200)
{
// Do further collision checks...
}
}
}
the thing is, with the size of some of the maps i’m thinking of making, i’ll have ~100 enemies to check, and ~200 walls to check (in a similar fasion to how the enemies are checked), and i’m afraid my game could slow down using the above code. is that possible, or is the above algorithm simple enough that it won’t cause lag even if repeated over 300 times per loop?
if it will cause lag, i was thinking of maybe dividing my map up into 1000X1000 pixel sections, and only doing collision checks on the enemies and walls which are in the section my player is in and the sections surrounding that section. i’d keep track of which walls and enemies were in each section by listing their reference numbers in an array (one array for each section, so the entire thing would be a three dimensional array), and i’d run through the lists of the sections surrounding the player when doing my collision checks, only running checks on the reference numbers which are listed. one downside would be that i’d have to make sure my enemies are removed from one list and added to another when moving between sections, and another downside is it would be a little bit of a pain in the *** to impliment but would this section-based method of collision checking be more CPU efficient then checking every wall and enemy in the map?