Why is my "If" not working?

edited March 2018 in Questions about Code

Hey there,

Im pretty new to software coding (even tho i come from PLC programming) i ran into a problem using a simple if function. This code creates a mesh out of boxes and you can move through it with your keys W and S. I wanted to loop the cameraposition somewhere around camZ=9000 using a the If in line 29 but it just wont work and i dont know why. It is supposed to substract one box size, once the camZ value goes over the bounce value. Can someone explain why it doesnt work?

Here is my code:

void setup() {

  fullScreen(P3D);

  translate(width/2, height/1, 0);
  stroke(255);
  noFill();
}

int size = 14;
int boxes = 1000;
float camX = boxes*size/2;
float camY = boxes*size/2*-1;
float camZ = boxes*size/30*-1;
float camM = 0;
float loopval = 9000.0;

void draw() {

  int size = 14;
  int boxes = 1000;
  float camP = camM+camZ;
  println(camP);
  println(frameRate);
  background(120);

  if (camZ > loopval) {
    camZ = loopval - boxes*3;
  } else {
  }

  if (keyPressed) {
    if (key == 'w' || key == 'W') {
      camM = camM-50;
    }
  } 

  if (keyPressed) {
    if (key == 's' || key == 'S') {
      camM = camM+50;
    }
  }

  camera(camX, camY, camZ+camM, // eyeX, eyeY, eyeZ
    camX, camY, 0.0, // centerX, centerY, centerZ
    0.0, 1.0, 0.0); // upX, upY, upZ

  for (int i3 = 0; i3 < size; i3 = i3+1) 
  {
    for (int i2 = 0; i2 < size; i2 = i2+1) //Y row
    {
      for (int i = 0; i < size; i=i+1) //X row
      {
        box(boxes);
        translate(boxes, 0, 0); //X
      }
      translate(boxes*size*-1, 0, boxes); //Y
    }
    translate(0, boxes*-1, boxes*size*-1); //Z
  }
}
Tagged:

Answers

  • Answer ✓

    where are you increasing camZ?

  • edited March 2018

    Thx, i am not increasing camZ but camM, that solved it! :)

    void setup() {
    
      fullScreen(P3D, 2);
    
      translate(width/2, height/1, 0);
      stroke(255);
      noFill();
    }
    
    int size = 14;
    int boxes = 1000;
    float camX = boxes*size/2;
    float camY = boxes*size/2*-1;
    float camZ = boxes*size/30*-1;
    float camM = 0;
    float loopval = 11000.0;
    
    void draw() {
    
      int size = 14;
      int boxes = 1000;
       println(camM);
      println(frameRate);
      background(120);
    
      if (camM > loopval) {
        camM = loopval - boxes;
      } else {
      }
    
    
    
      if (keyPressed) {
        if (key == 's' || key == 'S') {
          camM = camM+50;
        }
      }
    
      camera(camX, camY, camZ+camM, // eyeX, eyeY, eyeZ
        camX, camY, 0.0, // centerX, centerY, centerZ
        0.0, 1.0, 0.0); // upX, upY, upZ
    
      for (int i3 = 0; i3 < size; i3 = i3+1) 
      {
        for (int i2 = 0; i2 < size; i2 = i2+1) //Y row
        {
          for (int i = 0; i < size; i=i+1) //X row
          {
            box(boxes);
            translate(boxes, 0, 0); //X
          }
          translate(boxes*size*-1, 0, boxes); //Y
        }
        translate(0, boxes*-1, boxes*size*-1); //Z
      }
    }
    

    This actually works now :)

Sign In or Register to comment.