We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to rotate a cube diagonally. I think I need to use pushMatrix / popMatrix, but not sure.
int rotx;
int roty;
int counter;
int dir = 1;
void setup() {
size(500, 500, P3D);
noStroke();
}
void draw() {
background(0);
pointLight(200, 200, 200, width/2, height/2, 200);
translate(width/2, height/2, -50);
rotateX(radians(rotx));
rotateY(radians(roty));
scale(100);
rotx+=dir;
roty+=dir;
if (rotx > 360) {
dir = -1;
} else if (rotx < 0) dir = 1;
if (roty > 360) {
dir = -1;
} else if (roty < 0) dir = 1;
TexturedCube();
}
void TexturedCube() {
// +Z "front" face
beginShape(QUADS);
vertex(-1, -1, 1, 0, 0);
vertex( 1, -1, 1, 1, 0);
vertex( 1, 1, 1, 1, 1);
vertex(-1, 1, 1, 0, 1);
endShape();
// -Z "back" face
beginShape(QUADS);
vertex( 1, -1, -1, 0, 0);
vertex(-1, -1, -1, 1, 0);
vertex(-1, 1, -1, 1, 1);
vertex( 1, 1, -1, 0, 1);
endShape();
// +Y "bottom" face
beginShape(QUADS);
vertex(-1, 1, 1, 0, 0);
vertex( 1, 1, 1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
// -Y "top" face
beginShape(QUADS);
vertex(-1, -1, -1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, -1, 1, 1, 1);
vertex(-1, -1, 1, 0, 1);
endShape();
// +X "right" face
beginShape(QUADS);
vertex( 1, -1, 1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex( 1, 1, 1, 0, 1);
endShape();
// -X "left" face
beginShape(QUADS);
vertex(-1, -1, -1, 0, 0);
vertex(-1, -1, 1, 1, 0);
vertex(-1, 1, 1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
}
void mouseDragged() {
float rate = 1.01;
rotx += (pmouseY-mouseY) * rate;
roty += (mouseX-pmouseX) * rate;
}
Answers
Apparently I should only rotate on one axis, not two. I think this works :)