Arraylist removing when 2 of the same object collide!!!

so what i do is i spawn in 1000 flowers with different Colors
and the flowers goes to the block its colliding with!

what i want to do is to check if there is a flower already colliding with that block and remove it from the list if this 2 diffrent flowers
are colliding!

			for(Block b : PlayingState.blocks){
				if(getObjectRect().intersects(b)){
					
					if(this.xpos < b.x){
						xpos = b.x;
					}
					if(this.xpos > b.x){
						xpos = b.x;
					}
					if(this.ypos < b.y){
						ypos = b.y;
					}
					if(this.ypos > b.y){
						ypos = b.y;
					}
					
					if(xpos == b.x && ypos == b.y){
						//LOOK IF THERE IS 2 IN THE SAME PLACE
						//THEN REMOVE THE FIRST ONE AND MOVE SECOND ONE THERE INSTEAD
						choosedLocation = true;
					}
				}
			}
		}

If this is in a tile based generation, you are better off storing the objects in a 2D array like so:


Tile[] [] tiles;

Fill it with say grass tiles. Then you can iterate over it and replace the grass tiles with flower tiles or you can set a flag in that tile so that you know it is occupied by some object.


for(int x = 0; x < tiles.len; x++) {
    for(int y = 0; y < tiles[0].len; y++) {
        Tile tile = tiles[x] [y] ;
        // do something 

}
    } 

For generation you can iterate a for loop n times, then use a random number generator to pick an x and y value and grab that tile, check if it is occupied or is already a flower tile and go from there.

here!

all flowers has

	protected double xpos;
	protected double ypos;

and

	private Rectangle objectRect;

the objectRect is init like this in tick

objectRect = new Rectangle((int)xpos,(int)ypos,width,height);

so if 2 flowers x and y are = and that flower objectRect is intersecting with the other objectRect then remove it

I’ve not seen this “intersect” before, I only have “overlap” for collision detection ???

That is pointless. There is not much point in even adding the flower in the first place, from what I understand you are adding a flow and THEN checking to see if there is one there already, then removing the previous one?

yeah to remove flowers that are on top off etch other!

Why don’t you just check during the generation of the flowers? The is no point in generating them, then going over the list again to remove overlapping flowers.

Why not do something like this:


public class FlowerTest
{
	
	Array<Flower> flowers = new Array<Flower>();
	
	public FlowerTest(){
		// we try to generate 500 flowers
		for(int x = 0; x < 500; x++){
			// create a new flower at a random positions
			Flower flower = new Flower(MathUtils.random(0,200), MathUtils.random(0, 200));
			for(int i = 0; i < flowers.size - 1; i++){
				// get a flower from the list
				Flower flower2 = flowers.get(i);
				// compare positions, if they are tge dame just jump to tge mext iteration
				if(flower2.comparePos(flower)) continue;
				// we got this far, so good to go
				flowers.add(flower);
			}
		}
	}
	
	public class Flower{

		float x;
		float y;

		public Flower(float x, float y){
			this.x = x;
			this.y = y;

		}

		public boolean comparePos(Flower otherFlower){
			return (this.x == otherFlower.x && this.y == otherFlower.y);
		}


	}
	
}

Wrote using aide so probably full or errors.

it does not generate any flowers what so ever!

solved it another way by setting the block to choosed and if the block was already choosed the flower that wanted to go there was removed from the list =D