Hi, i have 2 almost identical classes of objects (the color change) they have the same action : a random position without overlapping. Its work, but now i wonder how do i manage to place every objects with a random position without overlapping with the other classe.
so here is my world (where everything is lanch):
public class World2 {
PApplet parent;
public int worldW = 2000;
public int worldH = 2000;
Player player;
Prairie prairie;
ArrayList<Grass> grass;
ArrayList<Champi> champi;
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));
}
///////////////////
//////champi///////
int nombChampi = 1;
champi = new ArrayList<Champi>();
for(int i = 0; i < nombChampi; i++){
PVector l = new PVector(p.random(worldW-1750 , worldW-250), p.random(worldH-1750 , worldH-250));
champi.add(new Champi(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();
}
/////////////////////////
//////champi/////////////
Iterator<Champi> C = champi.iterator();
while(C.hasNext()){
Champi c = C.next();
c.display(parent);
c.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);
}
}
my first class:
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);
}
}
public void grow(){
int totalgrass = 25;
while(grass.size() < totalgrass){
PVector grasses = new PVector();
grasses.x = parent.random(250, 1750);
grasses.y = parent.random(250, 1750);
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 < 32){
overlapping = true;
break;
}
}
if(!overlapping ){
grass.add(grasses);
}
}
}
public ArrayList<PVector> getGrass(){
return grass;
}
public PVector position(){
return position;
}
public float getR(){
return r;
}
}
and the second:
public class Champi {
PApplet parent;
ArrayList<PVector> champi;
public PVector position;
int r;
@SuppressWarnings("deprecation")
public Champi(PApplet p, PVector l){
position = l.get();
parent = p;
r = 32;
champi = new ArrayList<PVector>();
}
@SuppressWarnings("deprecation")
public void add(PVector l){
champi.add(l.get());
}
public void display(PApplet p){
for(PVector c : champi){
p.ellipseMode(PConstants.CENTER);
p.fill(255,0,0);
p.ellipse(c.x, c.y, r, r);
}
}
public void grow(){
int totalchamp = 25;
while(champi.size() < totalchamp){
PVector champis = new PVector();
champis.x = parent.random(250, 1750);
champis.y = parent.random(250, 1750);
boolean overlapping = false;
for(int i = 0; i < champi.size(); i++){
PVector other = champi.get(i);
float d = PApplet.dist(champis.x, champis.y, other.x, other.y);
if(d < 32){
overlapping = true;
break;
}
}
if(!overlapping){
champi.add(champis);
}
}
}
public ArrayList<PVector> getChampi(){
return champi;
}
public PVector position(){
return position;
}
public float getR(){
return r;
}
}
i tried to add the ArrayList of one classe to the other but it doenst work, i have this message:
The method add(PVector) in the type ArrayList is not applicable for the arguments (Class)
when i tried this:
public void grow(){
int totalchamp = 25;
///////////////what i tried////////////////////
champi.addAll((Collection<? extends PVector>) new Grass(parent, position));
////////////////////////////////////////////////////
while(champi.size() < totalchamp){
PVector champis = new PVector();
champis.x = parent.random(250, 1750);
champis.y = parent.random(250, 1750);
boolean overlapping = false;
for(int i = 0; i < champi.size(); i++){
PVector other = champi.get(i);
float d = PApplet.dist(champis.x, champis.y, other.x, other.y);
if(d < 32){
overlapping = true;
break;
}
}
if(!overlapping){
champi.add(champis);
}
}
}
i have this exception :
java.lang.ClassCastException: entity.Grass cannot be cast to java.util.Collection
what do i do wrong ?
Thanks for yours times.