How to rotate a cube diagonally?

edited March 2016 in Questions about Code

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

  • Answer ✓

    Apparently I should only rotate on one axis, not two. I think this works :)

    void setup() {
      size(500, 500, P3D);
    
      noStroke();
    }
    
    float deg = 45.0;
    float rad = radians(deg);
    float angle = 0.1;
    float rad1 = radians(angle);
    
    void draw() {
      background(0);
    
      camera(mouseX*2, height/2, (height/2) / tan(PI/6), width/2, height/2, 0, 0, 1, 0);
      pointLight(200, 200, 200, width/2, height/2, 200);
    
      translate(width/2, height/2, -50);
    
      rotateZ(rad);
      pushMatrix();
      rotateX(rad1);
      pushMatrix();
      rotateZ(-rad);
    
      scale(100);
    
      TexturedCube();
      popMatrix();
      popMatrix();
      rad1 = rad1 + 0.01;
    
    }
    
    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();
    }
    
Sign In or Register to comment.