how to get cube to rotate in place

I want to get the colored cube i made to rotate in the center of the screen the same way the white one dose. It rotates around the x and y axis but i cant seem to figure out how to get it to rotate in place. I want its position locked in the center of the screen.

thanks for the help.

PShape box;

void setup() {
  size(640, 360, P3D);
  box = createShape(GROUP);

  //z-axis front
  PShape box_front;
  box_front=createShape();
  box_front.beginShape();
  box_front.fill(0, 0, 255);
  box_front.vertex(50, 50, 0);
  box_front.vertex(100, 50, 0);
  box_front.vertex(100, 100, 0);
  box_front.vertex(50, 100, 0);
  box_front.endShape(CLOSE);

  //z-axis back
  PShape box_back;
  box_back=createShape();
  box_back.beginShape();
  box_back.fill(0, 0, 255);
  box_back.vertex(50, 50, -50);
  box_back.vertex(100, 50, -50);
  box_back.vertex(100, 100, -50);
  box_back.vertex(50, 100, -50);
  box_back.endShape(CLOSE);

  //y-axis top
  PShape box_top;
  box_top=createShape();
  box_top.beginShape();
  box_top.fill(0, 255, 0);
  box_top.vertex(50, 50, 0);
  box_top.vertex(50, 50, -50);
  box_top.vertex(100, 50, -50);
  box_top.vertex(100, 50, 0);
  box_top.endShape(CLOSE);

  //y-axis bottom
  PShape box_bottom;
  box_bottom=createShape();
  box_bottom.beginShape();
  box_bottom.fill(0, 255, 0);
  box_bottom.vertex(50, 100, 0);
  box_bottom.vertex(50, 100, -50);
  box_bottom.vertex(100, 100, -50);
  box_bottom.vertex(100, 100, 0);
  box_bottom.endShape(CLOSE);

  //x-axis right
  PShape box_right;
  box_right=createShape();
  box_right.beginShape();
  box_right.fill(255, 0, 0);
  box_right.vertex(100, 50, 0);
  box_right.vertex(100, 50, -50);
  box_right.vertex(100, 100, -50);
  box_right.vertex(100, 100, 0);
  box_right.endShape(CLOSE);

  //x-axis left
  PShape box_left;
  box_left=createShape();
  box_left.beginShape();
  box_left.fill(255, 0, 0);
  box_left.vertex(50, 50, 0);
  box_left.vertex(50, 50, -50);
  box_left.vertex(50, 100, -50);
  box_left.vertex(50, 100, 0);
  box_left.endShape(CLOSE);

  // Form faces into box
  box.addChild(box_front);
  box.addChild(box_back);
  box.addChild(box_top);
  box.addChild(box_bottom);
  box.addChild(box_right);
  box.addChild(box_left);
}

float x=1;
float y=100;

void draw() {

  background(50);
  x=x+0.01;
  y=y-0.01;
  translate(width/2, height/2, 50);
  rotateY(y);
  rotate(x);

  // I want the color box to rotate like the white one
  shape(box);
  box(50);
}
Tagged:

Answers

  • edited March 2017 Answer ✓

    the white box translates around itself because it's centre is the origin.

    the coloured box isn't centred so it rotates oddly. this is better.

    // before setup
    static final int SZ = 50;
    
      // in setup
      // replacement coloured cube definition
      //z-axis front
      PShape box_front;
      box_front=createShape();
      box_front.beginShape();
      box_front.fill(0, 0, 255);
      box_front.vertex(-SZ, -SZ, SZ);
      box_front.vertex(SZ, -SZ, SZ);
      box_front.vertex(SZ, SZ, SZ);
      box_front.vertex(-SZ, SZ, SZ);
      box_front.endShape(CLOSE);
    
      //z-axis back
      PShape box_back;
      box_back=createShape();
      box_back.beginShape();
      box_back.fill(0, 0, 255);
      box_back.vertex(-SZ, -SZ, -SZ);
      box_back.vertex(SZ, -SZ, -SZ);
      box_back.vertex(SZ, SZ, -SZ);
      box_back.vertex(-SZ, SZ, -SZ);
      box_back.endShape(CLOSE);
    
      //y-axis top
      PShape box_top;
      box_top=createShape();
      box_top.beginShape();
      box_top.fill(0, 255, 0);
      box_top.vertex(-SZ, -SZ, SZ);
      box_top.vertex(-SZ, -SZ, -SZ);
      box_top.vertex(SZ, -SZ, -SZ);
      box_top.vertex(SZ, -SZ, SZ);
      box_top.endShape(CLOSE);
    
      //y-axis bottom
      PShape box_bottom;
      box_bottom=createShape();
      box_bottom.beginShape();
      box_bottom.fill(0, 255, 0);
      box_bottom.vertex(-SZ, SZ, SZ);
      box_bottom.vertex(-SZ, SZ, -SZ);
      box_bottom.vertex(SZ, SZ, -SZ);
      box_bottom.vertex(SZ, SZ, SZ);
      box_bottom.endShape(CLOSE);
    
      //x-axis right
      PShape box_right;
      box_right=createShape();
      box_right.beginShape();
      box_right.fill(255, 0, 0);
      box_right.vertex(SZ, -SZ, SZ);
      box_right.vertex(SZ, -SZ, -SZ);
      box_right.vertex(SZ, SZ, -SZ);
      box_right.vertex(SZ, SZ, SZ);
      box_right.endShape(CLOSE);
    
      //x-axis left
      PShape box_left;
      box_left=createShape();
      box_left.beginShape();
      box_left.fill(255, 0, 0);
      box_left.vertex(-SZ, -SZ, SZ);
      box_left.vertex(-SZ, -SZ, -SZ);
      box_left.vertex(-SZ, SZ, -SZ);
      box_left.vertex(-SZ, SZ, SZ);
      box_left.endShape(CLOSE);
    
      // Form faces into box
      box.addChild(box_front);
      box.addChild(box_back);
      box.addChild(box_top);
      box.addChild(box_bottom);
      box.addChild(box_right);
      box.addChild(box_left);
    
  • Well done!!!

    ;-)

  • wow! thanks for the help. So then the reason its centered is because we made SZ static?

  • Answer ✓

    no.

    your sides of the cube were 50 or -50 or 100 or 0. so you had an off-centre cube. rotate a cube around 0, 0, 0 when the sides all stick out different amounts is just going to look wrong.

    i made it so that the centre of the cube is at 0, 0, 0, see how the values are all + or - SZ, not 0, 50, 100.

    defining a constant for something that you use a lot is just good practice. it means that all the values are all the same everywhere and it also means you can change all 72 values IN ONE GO when you decide, say, that you want the cube to be 25x25x25 rather than 50x50x50.

  • thanks again for taking the time to respond koogs. i appreciate the help

Sign In or Register to comment.