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