Processing: View side of cube when pressing Key

こ雲淡風輕ζ 提交于 2019-12-24 16:46:12

问题


I have to view the side of the cube when pressing the key:

• The user sees different views of the cube when presses the following keys:

• The 1 key: the front view of the cube (red face)

• The 2 key: the back view of the cube (yellow face)

• The 3 key: the top view of the cube (blue face)

• The 4 key: the bottom view of the cube (magenta face)

• The 5 key: the right view of the cube (green face)

• The 6 key: the left view of the cube (cyan face)

I have the code. It works without the answer; however, I do not know where to start. Down below the comments, I was thinking of translating to the side of the cube only that would not work because it may change each time it is turned from the other keys that are turning it x,X,y,Y,z, and Z?

Edit: I updated it. It appears that it works when pressing 1 key, but it moves over a distance.

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:


The solution extends that one from your previous question Processing: Cube rotating stay in one place while moving.

The first thing you have to do is to rotate the cube, in that way, that the side which you want to show faces the view. Then the rotation animation has to be performed and finally the cube has t be moved to its position.
This results in the following transformation for each point of the cube:

P' = translation * rotationAnimation * rotationToSide *  P

This means you have to do the the instructions in this order:

e.g. for the yellow face and a rotation around the Z-axis:

translate(x, y, z); rotateZ(theta); rotateX(radians(90.0));

Create a global variable (actSide) which notice the current side of the cube and change the variable in the function pressNum:

char actSide = '1';

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

Create a new function (showSide), which performs a single rotation, dependent on the state of actSide, which rotates the corresponding side to the view:

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));
   }
}

Call the function showSide in cubeBox, immediately before the cube is drawn:

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

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

    beginShape(QUADS);

    // [...]

    endShape(CLOSE);
}



来源:https://stackoverflow.com/questions/55290954/processing-view-side-of-cube-when-pressing-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!