Rotate Sphere on its Axis

Having trouble getting the bear to spin on its axis. Basically, I’m trying to get the bear to stand still but rotate around on its axis.

float angle = PI/200;
void setup() {
  size(1000,1000, P3D);
  frameRate(3);
}

void draw() {
  background(135,206,235);
  
  noStroke();
  rotateY(angle);
  //rotateX(angle);
  //rotateZ(angle);
  angle=angle+PI/50;
  
  
  
  //Head
  pushMatrix();
  translate(width/2, height/2, 100);
  fill(76,39,0);
  sphere(110);
  popMatrix();

  //Left Eye
  pushMatrix();
  fill(0);
  translate(width/2-25, height/2-12.5, 176);
  sphere(35);
  popMatrix();

  //Left Pupil
  pushMatrix();
  fill(255);
  translate(width/2-24, height/2-25, 193);
  sphere(15);
  popMatrix();

  //Right Eye
  pushMatrix();
  fill(0);
  translate(width/2+25, height/2-12.5, 176);
  sphere(35);
  popMatrix();

  //Right Pupil
  pushMatrix();
  fill(255);
  translate(width/2+33, height/2-25, 191);
  sphere(15);
  popMatrix();

  //Face
  pushMatrix();
  fill(204, 155, 102);
  translate(width/2, height/2+30, 155);
  sphere(60);
  popMatrix();

  //Nose
  pushMatrix();
  fill(0);
  translate(width/2, height/2+25, 200);
  sphere(20);
  popMatrix();

  

  //Mouth
  pushMatrix();
  fill(0);
  translate(width/2, height/2+49.9, 180.5);
  sphere(31);
  popMatrix();

  //Right Ear
  pushMatrix();
  fill(76,39,0);
  translate(width/2+65, height/2-55, 120);
  sphere(55);
  popMatrix();

  //Inner Right Ear
  pushMatrix();
  fill(102, 65, 25);
  translate(width/2+65, height/2-55, 127);
  sphere(50);
  popMatrix();

  //Left Ear
  pushMatrix();
  fill(76,39,0);
  translate(width/2-65, height/2-55, 120);
  sphere(55);
  popMatrix();

  //Inner Left Ear
  pushMatrix();
  fill(102, 65, 25);
  translate(width/2-65, height/2-55, 127);
  sphere(50);
  popMatrix();
}

When you want to rotate an object around a point, you should translate the origin to that point because you can only rotate things around the origin (0, 0, 0).

That means that you should move

translate(width/2, height/2, 100);

above

rotateY(angle);

Now your head will be located at 0, 0, 0, which will be at the center of the screen, rotating. Good so far.

Then there’s one thing left to do: all your bear parts were translated from to the top left corner to the center of the screen.
But you want them to rotate with the head around the center of the screen, so their positions should be relative to the head, not to the top left corner. And what that means is that you should subtract (width/2, height/2, 100) from each of the translates for the different eyes, ears, etc. An example:

//Left Eye
  pushMatrix();
  fill(0);
  translate(width/2-25, height/2-12.5, 176);
  sphere(35);
  popMatrix();

becomes

//Left Eye
  pushMatrix();
  fill(0);
  translate(-25, -12.5, 76);
  sphere(35);
  popMatrix();

which means that you are moving (-25, -12.5, 76) away from the origin, which is now at the center of the screen.

Does it make sense?

1 Like

Yes thank you I had to do some sketching on paper to understand it, but I now understand.

Consider checking the transformation tutorial as it will also help.

Kf