Changing in between figures with a key pressed

Hello
I am currently working on a coding assignment, where i am supposed to make 3 different figures, a Cube, a Cone and a Zylinder. Now i am stuck with a problem tho, a part of the assignment is to make the figures change to the other figure when the spacebar is pressed (so i start with the cube, when i press the spacebar the cube dissapears and the Cone appears, then when i press again the Cone dissapears and the Cylinder appears and when i press again, the Cube appears again and the Cylinder dissapears).
I tried doing it with keypressed and an if loop, but that doesnt work because if i press it once the figure will just dissapear when i release the button.
How can i implement this?
This is my Code

float rotatex, rotatey, rotatez;
float radiusz, hz;
int segments, segmentsz, rz;
float radius, h;

void setup() {
 background(255, 255, 255);
 size(600, 600, P3D);
}

void draw() {
 background(255, 255, 255);
 shapeRotation();

 translate(width/2, height/2);
 scale(100);
 rotateX(rotatex);
 rotateY(rotatey);
 rotateZ(rotatez);
 
 
 drawZylinder(2,3,5,2);  
}

void drawCone(float radius, float h, int segments) {
 stroke(0, 0, 0);
 strokeWeight(0.03);
 fill(255, 255, 255);

 float angle;
 float[] x = new float[segments+1];
 float[] z = new float[segments+1];
 h = h/2;

 for (int i=0; i < x.length; i++) {
   angle = TWO_PI / (segments) * i;
   x[i] = sin(angle) * radius;
   z[i] = cos(angle) * radius;
 }

 beginShape(TRIANGLE_FAN);
 vertex(0, h, 0);
 for (int i = 0; i < x.length; i++) {
   vertex(x[i], h, z[i]);
 }
 endShape();

 beginShape(TRIANGLE_FAN);
 vertex(0, -h, 0);
 for (int i = 0; i < x.length; i++) {
   vertex(x[i], h, z[i]);
 }
 endShape();
}

void drawZylinder(float radiusz, float hz, int segmentsz, int rz) {
 stroke(0, 0, 0);
 strokeWeight(0.03);
 fill(255, 255, 255);

 float anglez;
 float[] xz = new float[segmentsz+1];
 float[] zz = new float[segmentsz+1];
 hz = hz/2;

 for (int i=0; i < xz.length; i++) {
   anglez = TWO_PI / (segmentsz) * i;
   xz[i] = sin(anglez) * radiusz;
   zz[i] = cos(anglez) * radiusz;
 }

 beginShape(TRIANGLE_FAN);
 vertex(0, hz, 0);
 for (int i = 0; i < xz.length; i++) {
   vertex(xz[i], hz, zz[i]);
 }
 endShape();
 
 beginShape(TRIANGLE_STRIP);
 for (int j = 0; j < rz+1; j++) {
   for (int i = 0; i < xz.length; i++) {
     vertex(xz[i], hz*(j/rz), zz[i]);
     vertex(xz[i], hz*((j-1)/rz), zz[i]);
   }
 }
 endShape();

 beginShape(TRIANGLE_FAN);
 vertex(0, -hz, 0);
 for (int i = 0; i < xz.length; i++) {
   vertex(xz[i], -hz, zz[i]);
 }
 endShape();
}


void drawCube() {
 stroke(0, 0, 0);
 strokeWeight(0.03);
 fill(255, 255, 255);

 box(1);
}

void shapeRotation() {
 if (keyPressed) {
   if (key == 'W' || key == 'w') {
     rotatex = rotatex + 0.05;
   } else if (key == 'S' || key == 's') {
     rotatex = rotatex - 0.05;
   }
   if (key == 'A' || key == 'a') {
     rotatey = rotatey + 0.05;
   } else if (key == 'D' || key == 'd') {
     rotatey = rotatey - 0.05;
   }
   if (key == 'Q' || key == 'q') {
     rotatez = rotatez + 0.05;
   } else if (key == 'E' || key == 'e') {
     rotatez = rotatez - 0.05;
   }
 }
}

*note that the drawZylinder() method doesnt work yet, because i have problems with correctly dividing the shell into different rings

hi @Katsucchi,

check this out … Hope it helps you to get the logic …

Cheers
— mnse

int which    = 0;
int maximum  = 3;

void setup() {
  size(200, 200);
}

void keyReleased() {
  if (key == ' ') {
    which = (which + 1) % maximum;
  }
}

void draw() {
  background(0);
  switch(which) {
  case 0:
    text("box", width/2, height/2);
    break;
  case 1:
    text("cone", width/2, height/2);
    break;
  case 2:
    text("cylinder", width/2, height/2);
    break;
  }
}
1 Like