Skip to content

Instantly share code, notes, and snippets.

@wedesoft
Created October 1, 2023 15:20
Show Gist options
  • Save wedesoft/87ac812ff210ea3456ebd4dafe4fe4f1 to your computer and use it in GitHub Desktop.
Save wedesoft/87ac812ff210ea3456ebd4dafe4fe4f1 to your computer and use it in GitHub Desktop.
Rubik's cube implemented in Java Processing
int t;
int n;
int dir;
int axis;
int grid = 100;
int size = 80;
void setup() {
size(854, 480, P3D);
rnd();
// fullScreen(P3D);
}
void rnd() {
t = millis();
n = int(random(0, 3)) - 1;
dir = int(random(0, 2)) * 2 - 1;
axis = int(random(0, 3));
}
void draw() {
lights();
background(127);
perspective(0.4 * PI, float(width) / float(height), 10, 10000.0);
translate(width/2, height/2, 0);
rotateX(PI*(0.5 - float(mouseY)/height));
rotateY(PI*mouseX/width);
if (millis() >= t + 1000) {
rnd();
};
float rotation = dir * (t - millis()) / 1000.0 * PI / 2;
for (int k=-1; k<2; k++) {
for (int j=-1; j<2; j++) {
for (int i=-1; i<2; i++) {
if (i == n && axis == 0) {
rotateX(rotation);
};
if (j == n && axis == 1) {
rotateY(rotation);
};
if (k == n && axis == 2) {
rotateZ(rotation);
};
translate(grid*i, grid*j, grid*k);
box(size);
translate(-grid*i, -grid*j, -grid*k);
if (i == n && axis == 0) {
rotateX(-rotation);
};
if (j == n && axis == 1) {
rotateY(-rotation);
};
if (k == n && axis == 2) {
rotateZ(-rotation);
};
};
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment