I'm trying to make a simple 3d game in Processing but I've run into a problem. I tried to make an array to keep track of my environment objects and to make it easier to modify. However, when I try to run the program, it won't work.
Main code:
//arrays
BoxCl[] envArr;
void setup() {
size(640, 360, P3D);
envArr[0] = new BoxCl(1,1,-1,1); //it shows the error here
envArr[envArr.length] = new BoxCl(2,1,-1,1);
}
void draw() {
background(0);
camera(mouseX, height/2, (height/2) / tan(PI/6), width/2, height/2, 0, 0, 1, 0);
Update();
Draw();
}
void Update(){
}
void Draw(){
for(BoxCl i : envArr){
i.Draw();
}
}
BoxCl Class:
class BoxCl{
float x, y, z;
int s;
BoxCl(float x, float y, float z, int size){
this.x = x;
this.y = y;
this.z = z;
this.s = size;
}
void Draw(){
translate(scale*x, scale*y, scale*z);
stroke(255);
fill(255);
box(s * scale);
translate(scale*-x, scale*-y, scale*-z);
}
}
I've tried looking it up (here for example) but I think I'm too inexperienced to understand what I am supposed to do.
Please help.
edit: I am aware that a variable/array/object should be defined before being used. But how do I define the envArr in a way that it can still change? (i.e. increase or decrease in size when I have to create or delete an object)