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.