Hi everyone,
I'm making a maze game that contains a substantial amount of objects (trees, rocks, etc.). The map is quite big (4000 to 64000 pixels depending on the difficulty. Here's how I generate my map:
int[] mapx = new int[int(sq(mapSize/4000)*512 )]; //x coordinate of a tree int[] mapy = new int[mapx.length]; //y coordinate of a tree boolean[] mapi = new boolean[mapx.length]; // type of tree
void randomize() {
for ( int i=0; i < mapx.length; i++ ) { boolean out = true; while (out) { out = false; mapx[i] = int(random(mapSize)); mapy[i] = int(random(mapSize)); mapi[i] = boolean(int(random(2))); if (mapx[i] < pos.x+100 && mapx[i] > pos.x-100 && mapy[i] < pos.y+100 && mapy[i] > pos.y-100) { out = true; } if (mapx[i] < cp.x+500 && mapx[i] > cp.x-500 && mapy[i] < cp.y+500 && mapy[i] > cp.y-500) { out = true; } for (int j = 0; j < i; j++) { float td = sqrt(sq(abs(mapx[i] - mapx[j])) + sq(abs(mapy[i] - mapy[j]))); if (td < 80) { out = true; break; } } } }
}
Thing is, the program is quite slow and is pretty choppy. Every frame I'm basically looping through the whole array which is about 512 to a few thousand trees and I'm drawing an image if it's on the screen. I have to loop through this array when I'm moving and checking for collision. I'm about to add NPCs which means more loops through this array. How can I make this faster?
EDIT: On the topic of collision, How do I check collision for every non-transparent pixel of an image? My trees are basically circles to the collision algorithm but they're shown with a PImage and trees are of course lopsided so some clear areas are blocked and some covered areas are walkable.