yusufabi said:
Because there are no tutorials on the web for beginners.
Tutorials don't cover everything that is possible, so at some point, you will have to learn how to proceed without someone telling you their solution in full detail. Solutions become more global, often in the form of worked-out ideas how to approach a problem.
For the solution this is better, as it becomes more generally applicable (You can encode an idea in any programming language, rather than only the one that a tutorial happens to use.) For you, it's also a step forward. Programming is about not copy/pasting ready-made solutions from others. (If that was the case, we'd be done by now with programming.) Programming is about inventing solutions for your problems, often by looking at what others have done (reading their ideas), and then cooking something that works good for your specific problem. In other words, this is your first step on a life-long discovery journey. Scary at first, but so much fun to do. Finding answers to problems you thought were impossible, and then someone just shows an idea how to do it. It's awesome!
A quadtree is a good solution, although it may seem strange and complicated at first. Understanding how and why it works takes some effort.
As a first step in that direction, consider what you're doing. Say we have some area with enemies, say 10. How many comparisons are you doing? You test each enemy with all others, so 10 * 9 but that includes testing both (A, B) and (B, A) enemy pairs, so actually you need 10 * 9 / 2 = 45 checks. For 30 enemies, that's 30 * 29 / 2 = 435. For a 1000 enemies, you end up with 1000 * 999 / 2, which is 499500. As you can see the number checks grows very fast.
What you want are the colliding pairs. So everything you check that doesn't collide is a waste of time, right? How many of the checks you're doing give a collision as answer? I don't know your game, but my guess is a handful at most. For 10 enemies you may find 2 collisions. So 43 of the 45 checks you're doing is wasted, about 95%. For more enemies, this 95% waste is going up towards near 99.9%.
So how to improve this? One way to do it is to avoid checking collisions between enemies that are miles apart. If one enemy is at one side of the area and the other is at the other side, no point in ever checking whether they collide eh?
There are lots of ways to do this. A simple one is adding a grid to the area. Conceptually, each enemy is in a cell of the grid. If you make a cell larger than the size of an enemy, then you don't need to compare enemies that are not in neighbouring grid-cells. That should reduce your wasted effort in collision checking!
So collision detection becomes a 2-step process. First assign each enemy to a cell, then compare enemies in a non-empty cell with each other (They are all in the same cell, so likely to collide), and with enemies in directly neighbouring cells. (To understand, imagine an enemy being at the edge of a cell. You assign it to one cell, but since it has a non-zero size, it sticks out into the other cell(s) as well.)
If all is well, this improves the performance, as you have eliminated some of the 95% wasted effort. On the other hand, you added an extra processing step by assigning enemies to cells, which also takes time.
So this is kind of the reasoning process. You find a problem, and think why it happens. Then you invent a way to avoid the problem by changing what you're computing. If you do it right, you reduce the problem (typically it never completely disappear, the best you can hope for is a reduction to make it irrelevant). For complicated problems, you may need to do the above several times.
Solutions like quadtrees are the result of many such improvement cycles. These solutions need time to digest to see why they work, by doing some reasoning like above for yourself. Ask yourself, “what should I do different to make it faster?” Tutorials normally don't even mention down-sides of their solution, you have to learn to see beyond what a tutorial provides.