Changing the centre of rotation of a 3D object

what function can be used in a programming code to move the centre of rotation from centre of sketch to centre of the 3D object. For eg A stick not rotating on its end but its centre

Answers

  • edited June 2017
    void setup() {
      size(440, 440, P3D);
    
    }
    
    void draw() {
      background(0);
      translate(width/2, height/2, 0);
      lights();
      rotateX(map(mouseY,0,height,-PI,PI));
      rotateY(map(mouseX,0,width,-PI,PI));
      fill(255);
      noStroke();
      box(30);
      stroke(255,0,0);
      line(0,0,0,100,0,0);
      stroke(0,255,0);
      line(0,0,0,0,100,0);
      stroke(0,0,255);
      line(0,0,0,0,0,100);
    
      translate(100,100,100);
      noFill();
      stroke(0,255,255);
      box(5);
      rotateX(map(millis()%5000,0,5000,0,TWO_PI));
      rotateY(map(millis()%7000,0,7000,0,TWO_PI));
      stroke(255);
      translate(-50,0,0);
      line(0,0,0,100,0,0);
    }
    
  • edited June 2017

    translate() is the general purpose way to solve these problems -- in addition, for built-in shapes, you can set the mode to centered for that shape type -- rectMode(), ellipseMode() etc.

  • You can isolate rotations with pushMatrix and popMatrix

  • translate moves the freedom of rotation to wherever u want on the screen but what if we want to rotate about the centre of the irregular body shape like a rock or something

  • Just draw that irregular object from the center, the same as a regular one. No matter how irregular, it has a center -- width/2, height/2, depth/2.

    To center everything / anything with respect to the 3D camera, use the PeasyCam library.

Sign In or Register to comment.