处理:按键时立方体的一面

时间:2019-03-22 00:00:54

标签: processing

按下键时,我必须查看立方体的侧面:

•按下以下键,用户将看到多维数据集的不同视图:

•1键:立方体的正视图(红色面)

•2键:立方体的后视图(黄色面)

•3键:立方体的顶视图(蓝色面)

•4键:立方体的底视图(洋红色面)

•5键:立方体的右视图(绿色面)

•6键:立方体的左视图(青色的脸)

我有代码。它没有答案就可以工作;但是,我不知道从哪里开始。在注释下方,我想到的是仅翻译到不起作用的多维数据集一侧,因为每次从其他将其旋转的键(x,X,y,Y,z和Z)中旋转时,它都可能会更改?

编辑:我更新了它。似乎在按1键时可以使用,但是会移动一段距离。

float thetax = 0;
float thetaX = 0;
float thetay = 0;
float thetaY = 0;
float thetaz = 0;
float thetaZ = 0;
char actKey = 0;
boolean red = true;
void setup() {
  size(600, 600, P3D);
}

void draw() {
  background(255);
  fill(127, 127);
  String s1 = "Press x for counterclockwise of x axis, X for clockwise of x axis"; 
  String s2 = "Press y for counterclockwise of y axis, Y for clockwise of y axis ";
  String s3 = "Press z for counterclockwise of z axis, Z for for clockwise for z axis";
  text(s1, 0, width/2 + 100);
  text(s2, 0, width/2 + 125);
  text(s3, 0, width/2 + 150);
  pressButtons();
  pressNum();
  translate(width/2, height/2);
  cubeBox(.5, .5, .5);
}


void cubeBox(float x, float y, float z) {
  translate(x, y, z);
  addRotation();
  beginShape(QUADS);

  fill(255, 0, 0);
  vertex(100, 100, 100);
  vertex(-100, 100, 100);
  vertex(-100, -100, 100);
  vertex(100, -100, 100);

  fill(255, 255, 0);
  vertex(-100, -100, -100);
  vertex(100, -100, -100);
  vertex(100, 100, -100);
  vertex(-100, 100, -100);

  fill(0, 255, 0);
  vertex(100, 100, 100);
  vertex(100, -100, 100);
  vertex(100, -100, -100);
  vertex(100, 100, -100);

  fill(0, 255, 255);
  vertex(-100, -100, 100);
  vertex(-100, -100, -100);
  vertex(-100, 100, -100);
  vertex(-100, 100, 100);

  fill(0, 0, 255);
  vertex(-100, -100, 100);
  vertex(-100, -100, -100);
  vertex(100, -100, -100);
  vertex(100, -100, 100);

  fill(255, 0, 255);
  vertex(100, 100, 100);
  vertex(-100, 100, 100);
  vertex(-100, 100, -100);
  vertex(100, 100, -100); 
  endShape(CLOSE);
}

void pressButtons() {
    if (key == 'x' || key == 'X' || key == 'y' || key == 'Y' || key == 'z' || key == 'Z')
        actKey= key;
}

void addRotation() {
    if (actKey == 'x') { 
        thetax = thetax - .05;
        rotateY(thetax);
    } else if (actKey == 'X') {
        thetaX = thetaX + .05;
        rotateY(thetaX);
    } else if (actKey == 'y') {
        thetay = thetay - .05;
        rotateX(thetay);
    } else if (actKey == 'Y') {
        thetaY = thetaY + .05;
        rotateX(thetaY);
    } else if (actKey == 'z') {
        thetaz = thetaz - .05;
        rotateZ(thetaz);
    } else if (actKey == 'Z') {
        thetaZ = thetaZ + .05;
        rotateZ(thetaZ);
    }
}

void pressNum() {
  if(key == '1') {
    pressToSeeSquare();
  } else if(key == '2') {
    pressToSeeSquare();
  }
}


void pressToSeeSquare() {
  if(red == true) {
   translate(width/2, height/2);
  fill(255, 0, 0);
  vertex(-100, -100, -100);
  vertex(100, -100, -100);
  vertex(100, 100, -100);
  vertex(-100, 100, -100);
  } 
  else if(yellow == true) {
    translate(width/2, height/2);
  fill(255, 255, 0);
  vertex(-100, -100, -100);
  vertex(100, -100, -100);
  vertex(100, 100, -100);
  vertex(-100, 100, -100);
  }
}

1 个答案:

答案 0 :(得分:1)

该解决方案将您先前的问题Processing: Cube rotating stay in one place while moving中的问题扩展了出来。

要做的第一件事就是旋转多维数据集,这样,您要显示的一面朝向视图。然后必须执行旋转动画,最后将多维数据集移到其位置。
这将对多维数据集的每个点进行以下转换:

P' = translation * rotationAnimation * rotationToSide *  P

这意味着您必须按照以下顺序进行操作:

例如黄色的脸和绕Z轴的旋转:

translate(x,y,z);   rotationZ(theta);   rotationX(radians(90.0));

创建一个全局变量(actSide),该变量会注意到多维数据集的当前面,并在函数pressNum中更改该变量:

char actSide = '1';

coid pressNum() {
    if (key >= '1' && key <= '6')  
        actSide = key;
}

创建一个新函数(showSide),该函数根据actSide的状态执行一次旋转,从而将视图的相应一侧旋转:

void showSide() {

   if (actSide == '1') {
       // The red side is the front side of the cube => no roation
   }
   else if (actSide == '2') {
       rotateX(radians(180.0));
   }
   else if (actSide == '3') {
       rotateX(radians(270.0));
   }
   else if (actSide == '4') {
       rotateX(radians(90.0));
   }
   else if (actSide == '5') {
       rotateY(radians(270.0));
   }
   else if (actSide == '6') {
       rotateY(radians(90.0));
   }
}

在绘制多维数据集之前立即在showSide中调用函数cubeBox

void cubeBox(float x, float y, float z) {

    translate(x, y, z);
    addRotation();
    showSide();

    beginShape(QUADS);

    // [...]

    endShape(CLOSE);
}