Random position without overlapping

ok now i see where is my issue, your code work perfectly but i launch it in a different class so it never stop.

my world class (where i launch it):

public class World2 {
	PApplet parent;
	
	public int worldW = 2000;
	public int worldH = 2000;
	
	Player player;
	
	Prairie prairie;
	
	ArrayList<Grass2> grass2;
	
	public World2(PApplet p){
		parent = p;
		
		//////player//////
		float px = worldW/2;
		float py = worldH/2;
		
		PVector playerVector = new PVector(px,py); 
		player = new Player(parent,playerVector);
		//////////////////
		
		/////prairie///////
		prairie = new Prairie(parent);
		///////////////////
		
		
		
		int nombGrass2 = 1;
		grass2 = new ArrayList<Grass2>();
		for(int i = 0; i < nombGrass2; i++){
			//grass2.add(new Grass2(p.random(worldW-1750 , worldW-250), p.random(worldH-1750 , worldH-250), 32, 32, parent));
			grass2.add(new Grass2(10, 10, 32, 32, parent));
		}
		
	}
	
	public void run(){
		
		parent.background(0); ///fond noir = le néant
		
		////// attention il faut pop les créature sur base du monde et non de l'écran !!!! 
		////// mtn il faut mettre dans le push les objets, ou il vont suivrent la camera
		
		parent.pushMatrix();
		
		
		//////fct pas correctement, a refaire avec une "camera"
		parent.translate(-player.position.x+worldW-parent.width-200, -player.position.y+worldH-parent.height-650);
		
		
		//////le fond blanc//////
		parent.fill(255); ///le monde en lui meme
		parent.rect(0, 0, worldW, worldH);
		/////////////////////////
		
		//////prairie////////////
		prairie.run(parent);
		/////////////////////////
		
		Iterator<Grass2> G2 = grass2.iterator();
		while(G2.hasNext()){
			Grass2 g2 = G2.next();
			g2.showGrass2();
			g2.makeGrass();
		}
			
		parent.popMatrix();
		
		player.PlayerMvt(parent);
		player.run(parent);
		
		parent.fill(255,0,0);
		parent.text("player x " + player.position.x + " " + "player y " + player.position.y, 10, 10);
		
		
	}
}

and the class Grass2() :

public class Grass2 {
	PApplet parent;
	public PVector position;
	
	int  w, h;
	float x,y;
	
	ArrayList<Grass2> grass2 = new ArrayList<Grass2>();
	int many = 120;
	boolean avoidCollide = false;
	long stime, dtime = 1000;
	
	
	public Grass2(float x,float y,int w,int h, PApplet p){
		this.x=x;
	    this.y=y;
	    this.w=w;
	    this.h=h;
	    parent = p;
	}
	
	
	void randomSet(){
		w = 32;
		h = 32;
		x = parent.random(250, 1750);
		y = parent.random(250, 1750);
	}
	
	
	public void makeGrass(){
		grass2 = new ArrayList<Grass2>();
		for(int i = 0; i < many; i++){
			randomSet();
			
			if(avoidCollide) while(collide(x,y,w,h)) randomSet();
			grass2.add(new Grass2(x,y,w,h,parent));
			//PApplet.println("i "+i+" x "+x+" y "+y+" w "+w+" h "+h);
		}
	}
	
	
	/*
	public void makeGrass(){
		boolean goon = true;
		grass2 = new ArrayList<Grass2>();
		for(int i = 0; i < many; i++){
			stime = parent.millis();
			randomSet();
			if(avoidCollide) while(collide(x,y,w,h) && goon){
				randomSet();
				if(parent.millis() > stime + dtime){
					goon = false;
					i = many;
				}
			}
			if(goon){
				grass2.add(new Grass2(x,y,w,h,parent));
			}
			
			//PApplet.println("i "+i+" x "+x+" y "+y+" w "+w+" h "+h);
		}
	}
	*/
	
	
	
	boolean collide(float g1x, float g1y, float g1w, float g1h){
		if(grass2.size() > 0){
			for(Grass2 onegrass : grass2){
				if(g1x + g1w >= onegrass.x && g1x <= onegrass.x + onegrass.w && g1y + g1h >= onegrass.y && g1y <= onegrass.y + onegrass.h){
					return true;
				}
			}
		}
		return false;
	}
	
	public void draw(){
		parent.fill(0);
		parent.ellipse(x, y, w, h);
		
	}
	
	public void showGrass2(){
		for(Grass2 onegrass : grass2) onegrass.draw();
	}
}

so i need to find a way to stop makeGrass when it found the proper locations.

sorry if i have expressed myself wrongly

1 Like