Skip to content

Instantly share code, notes, and snippets.

@LadyScream
Created September 29, 2018 04:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LadyScream/2e1d86c74661d8dd01e092dccb4b7c2b to your computer and use it in GitHub Desktop.
Save LadyScream/2e1d86c74661d8dd01e092dccb4b7c2b to your computer and use it in GitHub Desktop.
I'm still learning Java and Processing so this is in no way the best way to achieve this, but this is the way I've done it!
float time = 0;
int frame = 1;
boolean record = false;
Cube cube;
void setup(){
size(600, 600, P3D);
cube = new Cube(new PVector(0,0,0), 150);
}
void draw(){
if(time < TWO_PI || !record){
background(28);
translate(width/2, height/2, 0);
rotateX(PI/4);
rotateY(PI/4);
noStroke();
fill(255, 200, 200);
sphere(50);
fill(28);
stroke(255, 200, 200);
strokeWeight(5);
cube.draw();
time += 0.025;
if (record) {
save(frame+".gif");
}
frame++;
}
}
class Cube{
float size;
PVector pos;
Face[] faces = new Face[6];
Cube(PVector posa, float sizea){
pos = posa;
size = sizea;
faces[0] = new Face(pos.x, pos.y, pos.z, size, new PVector(-1,0,0));
faces[1] = new Face(pos.x, pos.y, pos.z, size, new PVector(0,1,0));
faces[2] = new Face(pos.x, pos.y, pos.z, size, new PVector(1,0,0));
faces[3] = new Face(pos.x, pos.y, pos.z, size, new PVector(0,-1,0));
faces[4] = new Face(pos.x, pos.y, pos.z, size, new PVector(0,0,-1));
faces[5] = new Face(pos.x, pos.y, pos.z, size, new PVector(0,0,1));
}
void draw(){
for (Face face: faces){
face.draw();
face.move(sin(time)/0.5);
face.rotate(sin(time)/30);
}
}
}
class Face{
float x, y, z, size;
PVector dir, rotation;
Face(float xa, float ya, float za, float sizea, PVector dira){
x = xa + dira.y * sizea/2;
y = ya + dira.x * sizea/2;
z = za + dira.z * sizea/2;
size = sizea;
dir = dira;
rotation = new PVector(0, 0, 0);
}
void draw(){
pushMatrix();
rectMode(CENTER);
translate(x, y, z);
rotateX(dir.x*PI/2+rotation.x);
rotateY(dir.y*PI/2+rotation.y);
rotateZ(dir.z*PI/2+rotation.z);
rect(0, 0, size, size);
popMatrix();
}
void move(float speed){
x = x + dir.y * speed;
y = y + dir.x * speed;
z = z + dir.z * speed;
}
void rotate(float speed){
rotation.z += speed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment