如何在 Processing 中围绕另一个点旋转 3D 对象?

How to rotate a 3D object around another point in Processing?

这是一个非常简单的 Processing 程序,它围绕 x 轴旋转一个立方体。在 Processing 中,框函数的参数是框的大小,而不是位置。所以为了移动盒子我翻译了它;但是,我想知道如何能够将盒子移动到任何地方,但围绕另一个点旋转。在这种情况下,假设我想围绕 (0,0,0) 旋转框而不是平移点,我应该怎么做?

float rot = 1;
PShape model;
void setup(){
  size(500,500,P3D);
}

void draw(){
  background(0);
  translate(width/2, height/2);
  rotateX(rot);
  rot+=.005;
  box(40);
  //shape(model);
}

我四处看了看,在用户 Chrisir 回答的 Processing 论坛上找到了一个很好的资源:https://forum.processing.org/one/topic/3d-rotation-logic-problem.html

这个程序描绘了旋转物体的不同例子。底部的最后一个示例(蓝色立方体)描述了如何创建一个围绕另一个点旋转的对象:

float myAngle=-90; // degree measurement

void setup () {
  // Init
  // 3D requires P3D or OPENGL as a parameter to size()
  size (600, 600, P3D);
}

void draw () {
  // repeated continously
  background(22);
  // switch on lights  
  lights();
  // color for lines
  stroke(111);
  // 
  // Box: A box with equal dimension on all sides is a cube.
  // red: in a distance around Y-axis
  pushMatrix();
  rotateY(radians(myAngle));
  translate(158, 148, -10); 
  fill (color(242, 2, 2));  // red
  box(40); // only one parameter: box(size);
  popMatrix();
  // 
  // Box: A box with equal dimension on all sides is a cube.
  // green: around itself 
  pushMatrix();
  translate(258, 448, -10); 
  rotateY(radians(myAngle));
  fill (color(2, 222, 2)); // green
  box(40); // only one parameter: box(size);
  popMatrix();
  //
  // Box: A box with equal dimension on all sides is a cube.
  // blue: around the green box
  translate(258, 0, 0); 
  pushMatrix();
  rotateY(radians(myAngle));
  translate(158, 448, -10); 
  fill (color(2, 2, 222)); // blue
  box(40); // only one parameter: box(size);
  popMatrix();
  // 
  myAngle+=3; // speed
  if (myAngle>=360) {
    myAngle=0; // keep in degree
  }
  //
}