处理:按键时查看立方体的一侧 [英] Processing: View side of cube when pressing Key

查看:16
本文介绍了处理:按键时查看立方体的一侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

·按下以下键时,用户会看到立方体的不同视图:

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

推荐答案

该解决方案扩展了您上一个问题的解决方案Processing: Cube rotating stay in one place while moving

您必须做的第一件事是旋转立方体,这样,您想要显示的那一侧朝向视图。然后必须执行旋转动画,并最终不必将立方体移动到其位置。
这将为多维数据集的每个点产生以下转换:

P' = translation * rotationAnimation * rotationToSide *  P

这意味着您必须按以下顺序执行说明:

例如,对于黄色面孔和绕Z轴旋转:

平移(x,y,z); RotateZ(θ); rotateX(弧度(90.0));

创建一个全局变量(actSide),它注意多维数据集的当前侧,并更改函数pressNum中的变量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));
   }
}

紧接绘制多维数据集之前,调用cubeBox中的函数showSide

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

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

    beginShape(QUADS);

    // [...]

    endShape(CLOSE);
}

这篇关于处理:按键时查看立方体的一侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆