i have enemy class. and i am createing bunch of them and storing them in arraylist.

i am printing them in random places.

for(...){
    enemy_class = new Enemy((WINDOW_WIDTH/2)+r.nextInt(WINDOW_WIDTH/2), r.nextInt(WINDOW_HEIGHT)); 
    //add in arraylist
}

the problem is that some times they get create on top of each other. so my plan is to set collision on enemy so that they cant go through each other.

i set a Rectange getbound in enemy class. but not sure how to set collision on all the enemies in arraylist.

   /*** enemy dont draw on top of each other ***/
    public void enemy_enemy_collision(ArrayList<Enemy> enemy_store_temp) 
    {
        for(int i = 0; i < enemy_store_temp.size(); i++){
            Enemy enemy_class_temp = (Enemy)enemy_store_temp.get(i); 
            if(this.getBounds().intersects(this.getBounds()))          
            {

            }
        }
    }

the if statment is not right bc it is not checking collison with all the enemies in arraylist.

i was thinking some thing like:

for(int i = 0; i < enemy_store_temp.size(); i++){
            Enemy enemy_class_temp = (Enemy)enemy_store_temp.get(i);
            for(int x = i+1 ; x < enemy_store_temp.size(); x++){
                Enemy enemy_class_temp_next = (Enemy)enemy_store_temp.get(x);
                if(this.getBounds().intersects(enemy_class_temp_next.getBounds()))
                {
                    x+=30;
                }
            }
        }

so this way i am check collison enemy1 with all the enemies but not enemy 1. than enemy 2 with all the enemy but not enemy 2.

but this way x+= 30 can still move enemy on top of each other.

Recommended Answers

All 2 Replies

Comment #1:

for(int x = i+1

but

x+=30;

I don't think that will do what you think it will do... are you trying to increase an x-coordinate there?

Comment #2:

but this way x+= 30 can still move enemy on top of each other.

So the next thing to do is check to see if the enemies are not actually overlapping, but would collide if you moved them like this.

you do not want your enemies to collide , but you havent mentioned what you do want them to do when a collision happens or aboout to happen... maybe if you fix that , its will be easier to comeup with algorithms.

one possible way is the particle collision model .. what it does it that it uses a priority queue using a minimum binary heap as the underlying datastructure, which contains time to hit factors between all possible particle (in this case enemies) pairs , and operates on the pair that is the soonest to occur. it sounds complicated , and it is.. its physics part makes me feel like a fish out of water sometimes(mainly because its been many years since iv touched anything physics) but it gets the job done really well.

if your interested in this , you can read this and this here to get a feel for how they can work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.