Random position without overlapping

Hi i try to find a way to place objects on my world but without overlapping and i cant find a way.

here’s my code:

public class World2 {
	PApplet parent;
	
	public int worldW = 2000;
	public int worldH = 2000;
	
	Player player;
	
	Prairie prairie;
	
	ArrayList<Grass> grass;
	
	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);
		///////////////////
		
		
		/////grass/////////
		int nombGrass = 1;
		grass = new ArrayList<Grass>();
		for(int i = 0; i < nombGrass; i++){
			PVector l = new PVector(p.random(worldW-1750 , worldW-250), p.random(worldH-1750 , worldH-250));
			grass.add(new Grass(p, l));
		}
		///////////////////
		
	}
	
	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);
		/////////////////////////
		
		/////grass///////////////
		Iterator<Grass> G = grass.iterator();
		while(G.hasNext()){
			Grass g = G.next();
			g.display(parent);
			g.grow();
		}
		/////////////////////////
		
		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 my grass (the object):

public class Grass {
	PApplet parent;
	ArrayList<PVector> grass;
	public PVector position;
	
	int r = 32;
	
	@SuppressWarnings("deprecation")
	public Grass(PApplet p,PVector l){
		position = l.get();
		parent = p;
		grass = new ArrayList<PVector>();
	}
	
	@SuppressWarnings("deprecation")
	public void add(PVector l){
		grass.add(l.get());
	}
	
	public void display(PApplet p){
		for(PVector g : grass){
			p.ellipseMode(PConstants.CENTER);
			p.fill(0);
			p.ellipse(g.x, g.y, r, r);
		}
	}
	
	public void grow(){
		int totalgrass = 25;
		if(parent.random(1) < 0.1 && grass.size() < totalgrass){
			int nombGrass = 1;
			for(int a = 0; a < nombGrass; a++){
				grass.add(new PVector(parent.random(250, 1750),parent.random(250, 1750)));
			}
		}
	}
	
	public ArrayList<PVector> getGrass(){
		return grass;
	}
	
	public PVector position(){
		return position;
	}
	
	public float getR(){
		return r;
	}

}

can someone exmplain to me how to do it ?

Thanks for yours times

1 Like

the basic idea could be to make a set of random data
like for a rectangle x,y,w,h
and before you add that rect to the arraylist
check if it would collide with any already existing rects.
( and as long that is the case make a new set ( to test with ) )

and for that collision check see

proof of concept:

// array list of class 
class MyRect {
  int x, y, w, h;
  color rf = color(0, 200, 0);
  color rs = color(0, 0, 200);
  MyRect(int x, int y, int w, int h) {
    this.x=x;
    this.y=y;
    this.w=w;
    this.h=h;
  }
  void draw() {
    push();
    fill(rf);
    stroke(rs);
    rect(x, y, w, h);
    pop();
  }
}

ArrayList<MyRect> myrects = new ArrayList<MyRect>();
int x, y, w, h;
int many =10;
boolean avoid_collide = true; //false;

void random_set() {
  // random rects
  w = int(random(10, 30));
  h = int(random(10, 30));
  x = int(random(0, width-w));
  y = int(random(0, height-h));
}

void make_myrects() {
  myrects = new ArrayList<MyRect>();                                    // reset
  for (int i=0; i<many; i++) {
    random_set();                                                       // init random rect settings
    if ( avoid_collide ) while ( collide(x, y, w, h) ) random_set();    // try again until not collide with existing
    myrects.add(new MyRect(x, y, w, h));                                // ok, make it
    println("i "+i+" x "+x+" y "+y+" w "+w+" h "+h);
  }
}

// RECTANGLE/RECTANGLE // http://www.jeffreythompson.org/collision-detection/rect-rect.php
boolean collide(float r1x, float r1y, float r1w, float r1h) {      // new random rect to test before we make it!!
  if ( myrects.size() > 0) {                         // check on all existing rects if that NEW ONE would collide
    for ( MyRect onerect : myrects) {
      if (r1x + r1w >= onerect.x &&                  // r1 right edge past r2 left
        r1x <= onerect.x + onerect.w &&              // r1 left edge past r2 right
        r1y + r1h >= onerect.y &&                    // r1 top edge past r2 bottom
        r1y <= onerect.y + onerect.h)                // r1 bottom edge past r2 top
        return true;
    }
  }
  return false;
}

void show_myrects() {
  for ( MyRect onerect : myrects) onerect.draw();
}

void setup() {
  size(200, 200);
  make_myrects();
  println("array size: "+myrects.size());
}

void draw() {
  background(200, 200, 0);
  show_myrects();
}

void mousePressed() {
  make_myrects();
}

1 Like

Thanks i gonna try that way :slight_smile:

and thank you for the link

Hi, i tried your way, it work but i have a bug, it never stop trying to place the objects. Its like the loop never stop.

here is my Class, did you see whats i do wrong ?

public class Grass2 {
	PApplet parent;
	public PVector position;
	
	int  w, h;
	float x,y;
	
	ArrayList<Grass2> grass2 = new ArrayList<Grass2>();
	int many = 10;
	boolean avoidCollide = true;
	
	
	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));
		}
	}
	
	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();
	}
}
1 Like

-a- did you try my code and did it work?
-b- yes, up to now there is no limit build in,
so if like the canvas is full it would try new rectangles forever.
could make a limit retry counter or a timeout function.
-c- but you should first try to set

avoidCollide = false;

to check if the rest is ok,
-d- well, diagnostic prints for
x,y,w,h and all onegrass.x,y,w,h
might show where you problem is??

p.s.what environment you work under?
because your code can not be tested as is in processing IDE??
and that makes it very difficult to test.

also my example used rectangles,
you make circles? did you change the collision code to 2 circles?

a: yes it work
b: ok
c: i tried no effect
d: ok i gonna try

ps respond: i use Eclipse with java

what do you mean by change the collision code to 2 circles ? i gonna try with rectangle just to check.

ok i found my problem, i didnt use your moussePressed function so its run infinitely

so if use collision check or not, your code not adds any objects ?
so that is not related to this.

-b- i made a timeout and try 120 rects what will never fit in canvas
add i allow toggle “avoid_collide” by key [c]

// array list of class 
class MyRect {
  int x, y, w, h;
  color rf = color(0, 200, 0);
  color rs = color(0, 0, 200);
  MyRect(int x, int y, int w, int h) {
    this.x=x;
    this.y=y;
    this.w=w;
    this.h=h;
  }
  void draw() {
    push();
    fill(rf);
    stroke(rs);
    rect(x, y, w, h);
    pop();
  }
}

ArrayList<MyRect> myrects = new ArrayList<MyRect>();
int x, y, w, h;
int many =120;  // 100 OK
boolean avoid_collide = true; //false;
long stime, dtime=1000;        // timeout on while

void random_set() {
  // random rects
  w = int(random(10, 30));
  h = int(random(10, 30));
  x = int(random(0, width-w));
  y = int(random(0, height-h));
}

void make_myrects() {
  boolean goon=true;
  myrects = new ArrayList<MyRect>();                                    // reset
  for (int i=0; i<many; i++) {
    stime = millis();
    random_set();                                                      // init random rect settings
    if ( avoid_collide ) while ( collide(x, y, w, h) && goon ) { 
      random_set();                                                     // try again until not collide with existing
      if ( millis() > stime + dtime ) {      // check timeout
        println("timeout"); 
        goon = false;                        // stop while
        i = many ;                           // stop for loop
      }
    }
    if (goon ) {
      myrects.add(new MyRect(x, y, w, h));                                // ok, make it
      println("i "+i+" x "+x+" y "+y+" w "+w+" h "+h);
    }
  }
}

// RECTANGLE/RECTANGLE // http://www.jeffreythompson.org/collision-detection/rect-rect.php
boolean collide(float r1x, float r1y, float r1w, float r1h) {      // new random rect to test before we make it!!
  if ( myrects.size() > 0) {                         // check on all existing rects if that NEW ONE would collide
    for ( MyRect onerect : myrects) {
      if (r1x + r1w >= onerect.x &&                  // r1 right edge past r2 left
        r1x <= onerect.x + onerect.w &&              // r1 left edge past r2 right
        r1y + r1h >= onerect.y &&                    // r1 top edge past r2 bottom
        r1y <= onerect.y + onerect.h)                // r1 bottom edge past r2 top
        return true;
    }
  }
  return false;
}

void show_myrects() {
  for ( MyRect onerect : myrects) onerect.draw();
}

void setup() {
  size(200, 200);
  make_myrects();
  println("array size: "+myrects.size());
  println("use mouseclick for regenerate, key [c] toggle collision check");
}

void draw() {
  background(200, 200, 0);
  show_myrects();
}

void mousePressed() {
  println("make_myrects:");
  make_myrects();
}

void keyPressed() {
  if ( key == 'c' ) { 
    avoid_collide = ! avoid_collide;
    println("avoid_collide "+avoid_collide);
  }
}


1 Like

well it add the objects but even if the positions are correct, the loop continue.

can i put a break in makeGrass() ?

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);
	}
}

but i dont know where

if the makeGrass
( how i know from where you start it )
repeats you have to look where you start it.

if the loop
0 … many works and makes “many” objects OK

if the loop stucks because it hangs inside

while(collide(x,y,w,h)) randomSet();

need to do my above given new version what includes timeout and break loop many
so pls just run my latest version code as is
and see it can not create 120 rects because canvas too small so it stops between 95 … 105 about.

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

sorry i can not help because your code can not be tested in my environment
but current version also with random color:

code:
//https://discourse.processing.org/t/random-position-without-overlapping/7904/8
//-1- make a class for a rectangle
//-2- make a arraylist for many rectangles
//-3- create "many" rectangles as array of this class
//    using random x y w h
//-4- but after make that random set and before adding that rectangle to the list
//    check if it would overlap a existing one ( collision rect rect )
//-5- but if that "while overlap make new set " not finds a good new set
//    like when canvas is full...
//    check on a time out / break the "while" loop / break the "for many" loop /
//-6- also random color

// array list of class 
class MyRect {
  int x, y, w, h;
  color rstroke, rfill;

  MyRect(int x, int y, int w, int h, color rstroke, color rfill) {
    this.x=x;
    this.y=y;
    this.w=w;
    this.h=h;
    this.rstroke=rstroke;
    this.rfill=rfill;
  }
  void draw() {
    push();
    fill(rfill);
    stroke(rstroke);
    rect(x, y, w, h);
    pop();
  }
}

ArrayList<MyRect> myrects = new ArrayList<MyRect>();
int x, y, w, h;
int many =120;                          // about 100 OK
boolean avoid_collide = true; //false;
long stime, dtime=1000;        // timeout on while

color rf = color(0, 200, 0);
color rs = color(0, 0, 200);

void random_col() {
  int R_low = 100, R_high = 255; 
  int G_low = 100, G_high = 255; 
  int B_low = 100, B_high = 255; 
  rf = color(random(R_low, R_high), random(G_low, G_high), random(B_low, B_high));
  rs = color(random(R_low, R_high), random(G_low, G_high), random(B_low, B_high));
}

void random_set() {
  // random rects
  w = int(random(10, 30));
  h = int(random(10, 30));
  x = int(random(0, width-w));
  y = int(random(0, height-h));
  random_col();
}

void make_myrects() {
  boolean goon=true;
  myrects = new ArrayList<MyRect>();                                    // reset
  for (int i=0; i<many; i++) {
    stime = millis();
    random_set();                                                      // init random rect settings
    if ( avoid_collide ) while ( collide(x, y, w, h) && goon ) { 
      random_set();                                                     // try again until not collide with existing
      if ( millis() > stime + dtime ) {      // check timeout
        println("timeout"); 
        goon = false;                        // stop while
        i = many ;                           // stop for loop
      }
    }
    if (goon ) {
      myrects.add(new MyRect(x, y, w, h, rs, rf));                                // ok, make it
      println("i "+i+" x "+x+" y "+y+" w "+w+" h "+h);
    }
  }
}

// RECTANGLE/RECTANGLE // http://www.jeffreythompson.org/collision-detection/rect-rect.php
boolean collide(float r1x, float r1y, float r1w, float r1h) {      // new random rect to test before we make it!!
  if ( myrects.size() > 0) {                         // check on all existing rects if that NEW ONE would collide
    for ( MyRect onerect : myrects) {
      if (r1x + r1w >= onerect.x &&                  // r1 right edge past r2 left
        r1x <= onerect.x + onerect.w &&              // r1 left edge past r2 right
        r1y + r1h >= onerect.y &&                    // r1 top edge past r2 bottom
        r1y <= onerect.y + onerect.h)                // r1 bottom edge past r2 top
        return true;
    }
  }
  return false;
}

void show_myrects() {
  for ( MyRect onerect : myrects) onerect.draw();
}

void setup() {
  size(200, 200);
  make_myrects();
  println("array size: "+myrects.size()+ " of "+many);
  println("use mouseclick or key [r] for regenerate, key [c] toggle collision check");
}

void draw() {
  background(200, 200, 0);
  show_myrects();
}

void mousePressed() {
  println("make_myrects:");
  make_myrects();
}

void keyPressed() {
  if ( key == 'r' ) {
    println("make_myrects:");
    make_myrects();
  }
  if ( key == 'c' ) { 
    avoid_collide = ! avoid_collide;
    println("avoid_collide: "+avoid_collide);
  }
}


1 Like

no problem i was affraid you said that, did you have an idea how can i stop your make-myrects inside of it ?
Like :

void make_myrects() {
  myrects = new ArrayList<MyRect>();                                    // reset
  for (int i=0; i<many; i++) {
    random_set();                                                       // init random rect settings
    if ( avoid_collide ) while ( collide(x, y, w, h) ) random_set();    // try again until not collide with existing
    myrects.add(new MyRect(x, y, w, h));                                // ok, make it and stop trying
    /////stop//////
    println("i "+i+" x "+x+" y "+y+" w "+w+" h "+h);
  }
}

pls look my code above:
for stop the for loop only use like

  for (int i=0; i<many; i++) {
    random_set();                                                       // init random rect settings
    if ( avoid_collide ) while ( collide(x, y, w, h) ) random_set();    // try again until not collide with existing
    myrects.add(new MyRect(x, y, w, h));                                // ok, make it and stop trying
    println("i "+i+" x "+x+" y "+y+" w "+w+" h "+h);
i = many;
  }

to stop the while need above shown timer and add boolean
( possibly there are better ways, just what i made here for this test )

and again i can not follow your 2 class things,
at the first look i have the idea that you do the grass2 array from a parent class and as a function of its own class, looks both very confusing for me.

thanks for your help i gonna continue to shearch how to stop my makeGrass()

I did not follow the whole chat but I wanted to shared this link of a previous discussion that is relevant to your opening question: https://forum.processing.org/two/discussion/comment/90483/#Comment_90483

Kf

2 Likes

hi i manage to find a way to translate the processing code into java but im stuck,

so the code in processing is that :

ArrayList<FloatDict> circles;

void setup(){
  size(640,360);
  background(255);
  
  circles = new ArrayList<FloatDict>();
  
  

/////////////that's the important part (i think)/////////////////
///////////////////////////////////////////////////////
  while(circles.size() < 25){  
    
    FloatDict circle = new FloatDict();
    circle.set("x", random(width));
    circle.set("y", random(height));
    circle.set("r", 32);
    
    boolean overlapping = false;
    for(int j = 0; j < circles.size(); j++){
      FloatDict other = circles.get(j);
      float d = dist(circle.get("x"), circle.get("y"), other.get("x"), other.get("y"));
      
      if(d < circle.get("r") + other.get("r")){
        ////overlapp
        overlapping = true;
        break;
      }
    }
    
    if(!overlapping){
      circles.add(circle);
      
    }
        
  }
//////////////////////////////////////////
/////////////////////////////////////////
  
  for(FloatDict c : circles){
    fill(255,0,150,100);
    noStroke();
    ellipse(c.get("x"),c.get("y"), c.get("r")*2,c.get("r")*2 );
    
  }
  
  
}

and here is my code:

	public class Grass {
	PApplet parent;
	ArrayList<PVector> grass;
	public PVector position;
	
	int r ;
	
	@SuppressWarnings("deprecation")
	public Grass(PApplet p,PVector l){
		position = l.get();
		parent = p;
		r = 32;
		grass = new ArrayList<PVector>();
	}
	
	@SuppressWarnings("deprecation")
	public void add(PVector l){
		grass.add(l.get());
	}
	
	public void display(PApplet p){
		for(PVector g : grass){
			//p.ellipseMode(PConstants.CENTER);
			p.fill(0);
			p.ellipse(g.x, g.y, r, r);
			p.text("x " + g.x + " y " + g.y, g.x+16 , g.y);
		}
	}

        ///////////first try with overlapping/////////
        public void grow(){
		int totalgrass = 25;
		if(parent.random(1) < 0.1 && grass.size() < totalgrass){
			int nombGrass = 1;
			for(int a = 0; a < nombGrass; a++){
				grass.add(new PVector(parent.random(250, 1750),parent.random(250, 1750)));
			}
		}
	}
	
	//////////////////my translation/////////////////////	
	public void grow2(){
		int totalgrass = 12;
		
		while(grass.size() < totalgrass){
			PVector grasses = new PVector();
			grasses.x = parent.random(750, 1250);
			grasses.y = parent.random(750, 1250);
			
			boolean overlapping = false;

			for(int i = 0; i < grass.size(); i++){
				PVector other = grass.get(i);
				float d = PApplet.dist(grasses.x, grasses.y, other.x, other.y);
				
				if(d < 64){
					overlapping = true;
					break;
				}
			}
			if(!overlapping ){
                                ///the first one dont care about the overlapping am i right ?/////
				grass.add(new PVector(parent.random(750, 1250),parent.random(750, 1250)));
                                ///the second dont work, nothing appeard and my progam "crash" without stopping/////
				//grass.add(grasses);
			}	
		}
	}
	//////////////////////////////////////////
	
	public ArrayList<PVector> getGrass(){
		return grass;
	}
	
	public PVector position(){
		return position;
	}
	
	public float getR(){
		return r;
	}

}

my class world2 to launch everything:

public class World2 {
	PApplet parent;
	
	public int worldW = 2000;
	public int worldH = 2000;
	
	Player player;
	
	Prairie prairie;
	
	ArrayList<Grass> grass;
	
	
	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);
		///////////////////
		
		
		/////grass/////////
		int nombGrass = 1;
		grass = new ArrayList<Grass>();
		for(int i = 0; i < nombGrass; i++){
			PVector l = new PVector(p.random(worldW-1750 , worldW-250), p.random(worldH-1750 , worldH-250));
			grass.add(new Grass(p, l));
		}
		///////////////////
		
		
	}
	
	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);
		/////////////////////////
		
		/////grass///////////////
		Iterator<Grass> G = grass.iterator();
		while(G.hasNext()){
			Grass g = G.next();
			g.display(parent);
			//g.grow();
			g.grow2();
		}
		/////////////////////////
		
		
		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);
		
		
	}
}

so i know something is wrong with my code but i cant find what.
Is there a problem if im using PVector instead of FloatDict ? all my other class use PVector.
Sorry if i ask dumb or simple questions, im new in coding.

1 Like

ok i found it !

if i put my ellipse on ellipseMode(CENTRE) it work.

public void display(PApplet p){
		for(PVector g : grass){
			p.ellipseMode(PConstants.CENTER);
			p.fill(0);
			p.ellipse(g.x, g.y, r, r);
			p.text("x " + g.x + " y " + g.y, g.x+16 , g.y);
		}
	}

but why ?