Processing 3D指挥棒圆圈围绕太阳移动

Processing 3D指挥棒圆圈围绕太阳移动,processing,Processing,如何使用translate和rotate在画布上定位球体和线条,并使用Java处理旋转整个场景 我需要能够做到这一点,以便我能够: 为三维指挥棒创建一个类,该指挥棒包含两个大小相等的球体和一条连接两个球体中心的线。Baton类必须具有以下字段变量: float x, y, z; // the x, y, z coordinates of the center of one baton sphere // the other baton sphere should b

如何使用translate和rotate在画布上定位球体和线条,并使用Java处理旋转整个场景

我需要能够做到这一点,以便我能够:

为三维指挥棒创建一个类,该指挥棒包含两个大小相等的球体和一条连接两个球体中心的线。Baton类必须具有以下字段变量:

float x, y, z; // the x, y, z coordinates of the center of one baton sphere
               // the other baton sphere should be (-x, -y, -z)

float angle; // rotation angle

float speed; //rotational speed

float radius; //radius of the baton sphere
在草图的主选项卡中,我需要创建一个包含以下内容的场景:

窗口中心的半径为50的黄色球体。黄色球体不动。 6根棍棒绕y轴旋转,穿过黄色球体。 每个指挥棒以0.01到0.04之间的随机速度旋转。 所有指挥棒与黄色球体中心的距离都不同。 每个指挥棒球体的半径是15到30之间的随机数

这是我的代码:

Baton[] batons;
void setup() {
  size(600, 600);
  batons = new Baton[4];
  for(int i = 0; i < batons.length; i++)
  batons[i] = new Baton(100, 100, 100, 45, 2, 25, 2);
}

void draw() {
  background(255);
  stroke(0);
  translate(width/2, height/2);
  fill(255, 200, 50);
  sphere(50);

  for(int i = 0; i < batons.length; i++) {
    batons[i].update();
    batons[i].display();
  }
}

class Baton {
  float x;
  float y;
  float z;
  float angle;
  float speed;
  float radius;
  float theta;

  Baton(float x, float y, float z, float angle, float speed, float radius, float theta) {
    this.x = x;
    this.y = y;
    this.z = y;
    this.angle = angle;
    this.speed = speed;
    this.radius = radius;
    theta = 0;
  }

   void update() {
      theta = theta + speed;
    }

  void display() {
    pushMatrix();
      rotate(theta);
      translate(radius/2, 0);
      fill(51, 51, 51);
      noStroke();
      sphere(radius);
      popMatrix();

      line(x, y, -x, -y);

      pushMatrix();
      rotate(theta);
      translate(radius/2, 0);
      fill(51, 51, 51);
      noStroke();
      sphere(radius);
      popMatrix();

    }
  }
Baton[]指挥棒;
无效设置(){
大小(600600);
警棍=新警棍[4];
for(int i=0;i

< Baton >必须通过< <代码> >位于中间的太阳。这意味着两个圆和连接它的线必须绕太阳旋转。为了更容易解释,这条线将穿过太阳并以两个圆圈旋转。请参见上面的图片链接。

使代码呈现方式与所附图像不同的主要原因是,Baton对象被放置在画布的中心,并最终被中心的球体隐藏

在这里,我将指挥棒球体从中心移出,并降低了
帧速率
速度
,以便更容易看到它们是如何移动的

Baton[] batons;
void setup() {
  size(600, 600, P3D);
  batons = new Baton[4];
  frameRate(1); // slowed down the frame rate to 1 frame per second
  for(int i = 0; i < batons.length; i++){
    // changed speed from 2 to 0.1 so that the batons move in smaller increments
    batons[i] = new Baton(100, 100, 100, 45, 0.1, 25, 2);
  }
}

void draw() {
  background(255);
  stroke(0);
  translate(width/2, height/2);
  fill(255, 200, 50);
  sphere(50);

  for(int i = 0; i < batons.length; i++) {
    batons[i].update();
    batons[i].display();
  }
}

class Baton {
  float x;
  float y;
  float z;
  float angle;
  float speed;
  float radius;
  float theta;

  Baton(float x, float y, float z, float angle, float speed, float radius, float theta) {
    this.x = x;
    this.y = y;
    this.z = y;
    this.angle = angle;
    this.speed = speed;
    this.radius = radius;
    theta = 0;
  }

   void update() {
      theta = theta + speed;
    }

  void display() {
    pushMatrix();
    // since we want the entire configuration to rotate we will rotate the entire canvas
    rotate(theta);
    // for a more interesting rotation we could do this instead:
    // rotateX(theta);
    // rotateY(theta);
    // rotateZ(theta); 
      float distanceBetweenBatonSpheres = radius + 300;
      translate(distanceBetweenBatonSpheres/2, 0);
      fill(0, 200, 200);
      sphere(radius);
      // now we undo the previous translate and also move back out to the other side of the central sphere
      translate(distanceBetweenBatonSpheres/-1.0, 0);
      sphere(radius);
      // and finally.. draw a line to connect the two spheres
      line(0,0,distanceBetweenBatonSpheres, 0);
      popMatrix();
    }
  }
Baton[]指挥棒;
无效设置(){
尺寸(600、600、P3D);
警棍=新警棍[4];
帧速率(1);//将帧速率降低到每秒1帧
for(int i=0;i
请注意,我们如何旋转整个画布,以便球体的配置沿连接接力棒球体的线旋转为一个实体


另请参见有关通过在x、y和z方向旋转使草图更有趣的注释。

使代码呈现方式不同于所附图像的主要原因是,Baton对象放置在画布的中心,并最终被中心的球体隐藏

在这里,我将指挥棒球体从中心移出,并降低了
帧速率
速度
,以便更容易看到它们是如何移动的

Baton[] batons;
void setup() {
  size(600, 600, P3D);
  batons = new Baton[4];
  frameRate(1); // slowed down the frame rate to 1 frame per second
  for(int i = 0; i < batons.length; i++){
    // changed speed from 2 to 0.1 so that the batons move in smaller increments
    batons[i] = new Baton(100, 100, 100, 45, 0.1, 25, 2);
  }
}

void draw() {
  background(255);
  stroke(0);
  translate(width/2, height/2);
  fill(255, 200, 50);
  sphere(50);

  for(int i = 0; i < batons.length; i++) {
    batons[i].update();
    batons[i].display();
  }
}

class Baton {
  float x;
  float y;
  float z;
  float angle;
  float speed;
  float radius;
  float theta;

  Baton(float x, float y, float z, float angle, float speed, float radius, float theta) {
    this.x = x;
    this.y = y;
    this.z = y;
    this.angle = angle;
    this.speed = speed;
    this.radius = radius;
    theta = 0;
  }

   void update() {
      theta = theta + speed;
    }

  void display() {
    pushMatrix();
    // since we want the entire configuration to rotate we will rotate the entire canvas
    rotate(theta);
    // for a more interesting rotation we could do this instead:
    // rotateX(theta);
    // rotateY(theta);
    // rotateZ(theta); 
      float distanceBetweenBatonSpheres = radius + 300;
      translate(distanceBetweenBatonSpheres/2, 0);
      fill(0, 200, 200);
      sphere(radius);
      // now we undo the previous translate and also move back out to the other side of the central sphere
      translate(distanceBetweenBatonSpheres/-1.0, 0);
      sphere(radius);
      // and finally.. draw a line to connect the two spheres
      line(0,0,distanceBetweenBatonSpheres, 0);
      popMatrix();
    }
  }
Baton[]指挥棒;
无效设置(){
尺寸(600、600、P3D);
警棍=新警棍[4];
帧速率(1);//将帧速率降低到每秒1帧
for(int i=0;i