how to efficiently code the spawning and collision detection of a large amount / multiple enemies in java

StackOverflow https://stackoverflow.com/questions/22781713

Question

I got some difficulties because i dont know how to code efficient. I'm creating a vertical scrolling shooter & i wanna spawn a large number of enemies. At certain y-coordinates of the map & give them a certain x coordinate. If been doing testing this with a couple of enemies and in my Enemy-class the code i'm using now looks like this:

 if (Background.bgY == -3972 && active == 0) {
active = 1;
type = 1;
centerX = enemyXstart4;
KnightmareGame.activated = 1;
KnightmareGame.activatedatY = Background.bgY;
} 

Which basically looks for if the scrolled background is at coordinate Y, activate an enemy(bomb) of type 1 and give it starting coordinate centerX, activatedatY. This means that for every single enemy im writing this block of code...

Then in my KnightmareGame Class (which holds the routine where the enemies get drawn on screen) i'm drawing every enemy manually, which looks osmething like this:

    g.drawImage(currentBomb, bo1.getCenterX(), bo1.getCenterY(), this);
    g.drawImage(currentBomb, bo2.getCenterX(), bo2.getCenterY(), this);
    g.drawImage(currentBomb, bo3.getCenterX(), bo3.getCenterY(), this);

And in my collision detection method Im also checking every enemy 1 by 1:

if (projectileRect.intersects(KnightmareGame.bo1.bombRectangle)) {
        visible = false;            
        if (KnightmareGame.bo1.health > 0) {
            KnightmareGame.bo1.health -= 1;
        }
        if (KnightmareGame.bo1.health == 0) {
            Enemy.animateFire= true;

etc., etc.

The problem is that the total amount of enemiess i exceeds 50, so if i would continue coding them 1 by 1 the code would get real messy.

What i was thinking of was that I might make an array list of all the y coordinates where i wanna spawn enemies with their subsequent x coordinate. For example at -3900 i wanna spawn an enemy at x coordinate 200. At -3800 at x coordinate 300, etc. So an array list where every position holds 2 variables (x,y)

  1. I just dont know (first of all) how to code for this array list.
  2. And second. I have got no clue of how to automate my draw method like g.drawImage(currentBomb, bo1.getCenterX(), bo1.getCenterY(), this); I'm thinking of a loop, but dont know how the code...
  3. Collision detection: here i also collision detect in ONE block of code for every enemy instead of compare every enemy in a single seperate block of code with my projectile.

Does any1 know how to do this? Or maybe if I should do it differently then what i'm thinking of right now?

Help will be very much appreciated!

Was it helpful?

Solution

Handling multiple enemies

Create an ArrayList<Enemy> enemies;

Then use

for (Enemy bo : enemies) {
    // logic for each enemy.
}
  • Don't forget to remove old enemies after destroying or going out of screen.
  • Don't keep creating new objects every time. Use a object pool for efficiency.

Improving collision efficiency

For large number of entities to be checked for collision, generally a broadphase algorithm is used.

See if it is required in your case.

You might also try some spatial optimizations like quad-trees.


These might just be wiki links, but I suppose you can search further if you know what to search for.

Hope this helps.
Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top