Hello, I wrote a sketch. Which displays rotating cube which consist of small boxes and these boxes fly away from cube. When number of cubes by one side is 10, everything is ok, if I increase number of cubes all animation begin to freeze.
I`m new in processing and programming at all, so please help me optimize my code.
import processing.opengl.*;
int initFrame = 20;
int boxSize = 60; //
int boxCount = 10; //number of boxes on one axis
int wndW = screen.width; //
int wndH = screen.height; //
Cube[][][] cubes = new Cube[boxCount][boxCount][boxCount]; //инициализируем 3х мерный массив из 100 кубиков
ArrayList freeCubes = new ArrayList(); //list for the cubes which can be moved
boolean freeXp, freeYp, freeZp; //свободна ли ось в сторону положительных значений
boolean freeXn, freeYn, freeZn; //свободна ли ось в сторону отрицательных значений
boolean inList;
boolean mov; //кубик или движется или нет
//цвет кубика
int r=round(random(0,255));
int g=round(random(0,255));
int b=round(random(0,255));
//Конструктор
Cube(int x, int y, int z, int nx, int ny, int nz, int boxSize, boolean mov, boolean freeXp, boolean freeXn, boolean freeYp, boolean freeYn, boolean freeZp, boolean freeZn, boolean inList)
{
//Задаем начальные координаты кубика исходя из его положения
this.x = nx*boxSize;
this.y = ny*boxSize;
this.z = nz*boxSize;
this.mov = mov;
this.freeXn = freeXn;
this.freeYn = freeYn;
this.freeZn = freeZn;
this.freeXp = freeXp;
this.freeYp = freeYp;
this.freeZp = freeZp;
this.nx = nx;
this.ny = ny;
this.nz = nz;
this.inList = inList;
this.boxSize = boxSize;
}
void drawCube()
{
fill(r,g,b);
if (mov == true) {
if (freeXn == true) { setXMov(1); } else if ( freeXp == true ) { setXMov(-1); }
else if (freeYn == true) { setYMov(1); } else if ( freeYp == true ) { setYMov(-1); }
else if (freeZn == true) { setZMov(1); } else if ( freeZp == true ) { setZMov(-1); }
I wrote a sketch which draw a box consist of boxes, here is my code (do not pay attention to comments in russian)
import processing.opengl.*;
int boxSize = 40; //размер кубика
int boxCount = 10; //Кол-во кубиков на сторону
int[] k = {0,0,1,2,3,4,5,6,7,8,9}; //хитрые коэф для ебаного транслэйта
int wndW = 800; //Ширина окошка
int wndH = 800; //Высота окошка
void setup()
{
size(wndW, wndH,OPENGL);
background(0);
smooth();
frameRate(10);
noFill();
stroke(255);
// noStroke();
}
void draw()
{
background(0);
translate (wndW/2-boxSize*boxCount/2,wndH/2-boxSize*boxCount/2,0); //Центруе кубик в окне
for (int x=0;x<boxCount;x=x+1)
{
translate(x*boxSize-k[x]*boxSize,0,0);
for (int y=0;y<boxCount;y=y+1)
{
translate(0,y*boxSize-k[y]*boxSize,0);
box(boxSize);
}
translate(0,-1*(boxSize*boxCount-boxSize),0);
}
//boxSize=boxSize+1;
}
Next I want animate random box in this big box, for example it will fly away in any direction. I can`t imagine how I can do so only with translate function, is there any way how I can draw boxes setting it coordinates like a 2D rectangle? I really stuck.