We are about to switch to a new forum software. Until then we have removed the registration on this forum.
In my 3D game, there are trees, lots of them. To make it easier, I want to make them in a row, with multiple rows. I have the code for the trees and placing them into the game but it takes up a lot of line the add them in. I was thinking of a for() loop for adding many but I'm not sure of how to do this. My trees code:
Peastcam camera;
TREE tree1;
TREE tree2;
TREE tree3;
TREE tree4;
TREE tree5;
TREE tree6;
TREE tree7;
TREE tree8;
TREE tree9;
TREE tree10;
TREE tree11;
TREE tree12;
TREE tree13;
TREE tree14;
TREE tree15;
TREE tree16;
TREE tree17;
TREE tree18;
TREE tree19;
TREE tree20;
void setup(){
//fullScreen(P3D);
size(683,384,P3D);
frameRate(60);
camera = new PeasyCam(this,width/2, height-100, width/2, 50);
tree1 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,115,0,230);
tree2 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,230);
tree3 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,220);
tree4 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,210);
tree5 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,200);
tree6 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,190);
tree7 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,180);
tree8 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,170);
tree9 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,160);
tree10 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,150);
tree11 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,140);
tree12 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,130);
tree13 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,120);
tree14 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,110);
tree15 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,100);
tree16 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,240);
tree17 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,250);
tree18 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,260);
tree19 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,270);
tree20 = new TREE(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,175,0,280);
}
void draw(){
background(105);
//directionalLight(255,255,255,0,1,0);
translate(width/2,height+1,0);
fill(0,255,0); //(height/2)/tan(PI/6)
stroke(0); //translate(x,y,z);
box(width,-1,width); //box(width,height,depth)
fill(255,0,0);
tree1.display();
tree2.display();
tree3.display();
tree4.display();
tree5.display();
tree6.display();
tree7.display();
tree8.display();
tree9.display();
tree10.display();
tree11.display();
tree12.display();
tree13.display();
tree14.display();
tree15.display();
tree16.display();
tree17.display();
tree18.display();
tree19.display();
tree20.display();
}
class TREE {
float h;
float tS;
float lS;
color lC;
color tC;
float treeX,treeY,treeZ;
TREE(float trunkHeight, float trunkSize, float leafSize, color leafColor, color trunkColor, int treex, int treey, int treez){
h = trunkHeight;
tS = trunkSize;
lS = leafSize;
lC = leafColor;
tC = trunkColor;
treeX = treex;
treeY = treey;
treeZ = treez;
}
void display(){
fill(tC);
beginShape();
vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
endShape(CLOSE);
beginShape();
vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
endShape(CLOSE);
fill(lC);
beginShape();
vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
endShape(CLOSE);
beginShape();
vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
endShape(CLOSE);
beginShape();
vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
endShape(CLOSE);
}
}
As you can see, there is a lots of code just for 20 trees, but I need a lot more trees in the game (basically to act as the boarder). I have seem people use for loops to generate a row or grid of squares but I'm not sure of how to do it for custom created objects (trees). Please help.
Answers
you've misspelt PeasyCam. and you need to read
https://forum.processing.org/two/discussion/8082/from-several-variables-to-arrays
Classes traditionally start with an uppercase letter - Tree rather than TREE.
if those are all quads that you are drawing then you can do that all as one shape using
beginShape(QUADS);
and create it in the constructor, save it as a PShape - no need to re-define it every frame.In my previous example to you, I had the method show() associated to your class. The constructor, which you call in setup(), initializes the object. Then in draw(), you call the drawing function associated to that class, as I demonstrated in your other post.
I am assuming you are moving your camera and not your trees.
Kf
My questions has nothing to do with the camera. My QUESTION is how can I make an easy row of a bunch of trees rather than doing 20 lines to initialize 20 trees, then 20 line for data for each of the twenty trees and THEN another twenty line for drawing them. That's 60 lines for twenty trees, I have a least 100 trees and I need the be able to easily make a row of trees. I think the way I can do this is like in the video: but just with a row instead of a grid or full cube.
is there a pattern to this:
(yes, yes there is... - they are all the same apart from the last parameter, which ranges from 100 to 280 in steps of 10)
read the link i posted, and the one linked at the bottom of that (AND kf's previous answer), convert your tree1 - tree20 to a list or an array and then write a loop that calls new Tree with the correct value.
So I created an area of trees with a for() loop with this code:
It works great, except for one issue, because I have the trees size being randomly generated, the trees constantly change size, which, also causes it to lag a bit. Is there I way I can get the trees to be different sizes without them changing constantly throughout the sketch?
Check https://processing.org/reference/ArrayList.html
ArrayList<Tree> aForrest; //Global scope
In setup
In draw
If you do this, the trees are generated all in setup and then they are drawn in draw (). Since the constructor is called only in setup, then their heights will not vary when draw() cycles, unless you explicitly modify any of its properties.
Kf
Thanks, that works great, still a bit of lag though.