projectile collisions

The problem: I have up to 20 ish npcs and 20 ish projectiles on screen at a time; how do i check for collisions between any of them?

Some info: I’m extremely inexpierenced in the game dev world and i only just figured out a haxy way to make and draw a tile map platformer. My current ‘collision detection’ is literally just having player only add x_speed to xpixelposition if Main.map.getMapArray[ytile][xtile+ 1] is greater then 2(cos 0 1 2 are all unwalkable). The same thing goes for projectile.

basically how would i check if if ANY npc was colliding with ANY bullet? that would take way to long with my current method, yet i always see people doing collision detection like this, hell even mario did this without trouble.

The way im doing it now is something like:


Projectile bullet[20] = new Projectile[20];
NPC n[] = new NPC[20];

for(blah){
spawn npcs based on grid
}
if spacebar is pressed{
for(blah){
if(!bullet[i].isActive){
 set it to active and change it's coords to player.xtile,player.ytile;
 break;
}
}
main loop{
for(blah){
//update bullet[i] if bullet[i].isActive is true
if(bullet[i].isActive){
bullet[i].update();
}
}
for(blah){
npc[i].update();
}
p.move();
if(mapNeedsUpdating){
map.update();
}
repaint();
}

then in bullet I would have something like



if(isActive) 
for(blah){
check if I'm colliding with npc[i];
if i am, destroy the npc and me
}
else move me over one

so i have a feeling that that would be EXTREMELY inefecient. If i had all 20 bullets on screen, they would EACH check if they were colliding with ANY npc every time they moved at all(so that’s 20 loops each containing 20 loops plus the player updating and the map updating and the npc updating and the drawing all that stuff). but idk how im supposed to do it if i cant do that…
Like I said I’m very new to this, in fact I only really got into this on monday when I was informed that I signed up to make a game for my senior project. Up until now the only ideas I’ve been able to get are ones that I think of during calculus and write down before i forget, it’s a miracle I made it this far.

[quote]EXTREMELY inefecient. If i had all 20 bullets on screen, they would EACH check if they were colliding with ANY npc every time they moved at all
[/quote]
thats usually how you do it with small arrays
when you need more performance use quad trees or similar

meaning you divide parts of the screen / map into areas
can reduce the amount of checks

example: if all NPCs are in area 2 and the bullets are in like 5 and 6, you just have to check the areas

From what I learned


for (i=0; i<size; i++)
   for (r=i; r<size; r++)
      //check collision of element[i] and element[r]

works well on small amount (below 1000).


for (i=0; i<size; i++)
   for (r=i+1; r<size; r++)
      //check collision of element[i] and element[r]

Thanks Riven, miss that one :stuck_out_tongue: