代码之家  ›  专栏  ›  技术社区  ›  karamazovbros

如何在处理过程中围绕另一点旋转三维对象?

  •  0
  • karamazovbros  · 技术社区  · 6 年前

    这里有一个非常简单的处理程序,它绕X轴旋转立方体。在处理过程中,box函数的参数是框的大小,而不是位置。所以为了移动盒子,我翻译了它;但是,我想知道如何能够移动盒子到任何地方,但是围绕另一个点旋转。在这种情况下,假设我想将长方体旋转(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);
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   karamazovbros    6 年前

    我再看了一下,在用户Chrisir回答的处理论坛上找到了一个很好的资源: 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
      }
      //
    }