import processing.opengl.*;
float [][][][] pointList;
int cubeLength=600;
int num = 40;
float distance = cubeLength / num;
float cubeRotate=0;
void setup() {
size(800, 800, OPENGL);
//create a cube made by points, use array
pointList = new float [num][num][num][3];
frameRate(30);
pointList[0][0][0][0] = -300;
pointList[0][0][0][1] = -300;
pointList[0][0][0][2] = -800;
for (int i=0; i<num; i++) {
for (int j=0; j<num; j++) {
for (int t=0; t<num; t++) {
pointList[i][j][t][0] = pointList[0][0][0][0] + i * distance;
pointList[i][j][t][1] = pointList[0][0][0][1] + j * distance;
pointList[i][j][t][2] = pointList[0][0][0][2] + t * distance;
}
}
}
}
void draw() {
translate(width/2, height/2, 0);
//translate point of rotation on the z axis
//then we can rotate
translate(0,0,-500);
// rotateX(0.1*PI);
rotateY(cubeRotate);
//then here we translate back to the same point
translate(0,0,500);
background(0);
//make the colours of this cube changed, and control the speed of changing colours
if(frameCount%60==0){
stroke(random(255), random(255), random(255));
}
for (int i=0; i<num; i++) {
for (int j=0; j<num; j++) {
for (int t=0; t<num; t++) {
point(pointList[i][j][t][0], pointList[i][j][t][1], pointList[i][j][t][2]);
}
}
}
//this is a trigger use mousePressed, when press mouse button, points spill out with the explosion effect
if (mousePressed) {
for (int i=0; i<num; i++) {
for (int j=0; j<num; j++) {
for (int t=0; t<num; t++) {
//here instead of adding a speed lerp between this position and the position you want to go to (the centre of the screen for example) write over this position with the new position
pointList[i][j][t][0] = lerp(pointList[i][j][t][0], pointList[i][j][t][0] + i * distance * 10 – 3000, 0.05);
pointList[i][j][t][1] = lerp(pointList[i][j][t][1], pointList[i][j][t][1] + j * distance * 10 – 3000, 0.05);
pointList[i][j][t][2] = lerp(pointList[i][j][t][2], pointList[i][j][t][2] + t * distance * 10 – 3000, 0.05);
}
}
}
}
// release mouse button, cube back to normal, here re-draw the cube
else {
pointList[0][0][0][0] = -300;
pointList[0][0][0][1] = -300;
pointList[0][0][0][2] = -800;
for (int i=0; i<num; i++) {
for (int j=0; j<num; j++) {
for (int t=0; t<num; t++) {
pointList[i][j][t][0] = pointList[0][0][0][0] + i * distance;
pointList[i][j][t][1] = pointList[0][0][0][1] + j * distance;
pointList[i][j][t][2] = pointList[0][0][0][2] + t * distance;
}
}
}
}
//make cube spanning
cubeRotate+=0.01;
}