Moving camera

Hi, I am using the camera() function to rotate around a box in 3D, but I have found an unexpected camera behavior when the z axis reaches 0 and minus values.

The rotation starts at y=height/2 and z= 200 (always looking at x=width/2, y=height/2, z=0), and it rotates properly upwards, until the camera is supposed to be located just vertically over the box (which is located at z=0). Then, it seems that the camera rotates over the z-axis or something that I don't understand.

I see the values of y and z with println, and I think they are correct. Anybody can see where the error is...?

Thank you,

Tagged:

Answers

  • edited March 2014

    // Here is the code:

    // -----------------------------------------------------------------

    float eyeX, eyeY, eyeZ;

    float ang = 0;

    int d = 200;

    // -----------------------------------------------------------------

    void setup(){

    size(1000, 600, P3D);

    eyeX = width/2;

    eyeY = height/2;

    eyeZ = d;

    }

    // -----------------------------------------------------------------

    void draw(){

    // background in the draw loop to make it animate rather than draw over itself

    background(0);

    lights();

    stroke(255);

    // CAMERA:

    camera(eyeX, eyeY, eyeZ, width/2, height/2, 0, 0, 1, 0);

    pushMatrix();

    translate(width/2, height/2, 0);

    box(100);

    popMatrix();

    }

    // -----------------------------------------------------------------

    void keyPressed(){

    switch(key){

    // Move camera

    case CODED:

    if (keyCode == UP) {

    ang += 5;

    eyeY = (height/2)-d*(sin(radians(ang)));

    eyeZ = d*cos(radians(ang));

    }

    if (keyCode == DOWN) {

    ang -= 5;

    eyeY = (height/2)-d*(sin(radians(ang)));

    eyeZ = d*cos(radians(ang));

    }

    println(eyeX+" / "+eyeY+" / "+eyeZ);

    break;

    }

    }

    // -----------------------------------------------------------------

  • Answer ✓

    reason is that the cam is upside down when going over the top

    you can solve this by changing upY to -1

    // -----------------------------------------------------------------
    float eyeX, eyeY, eyeZ;
    float ang = 0;
    int d = 200;
    // -----------------------------------------------------------------
    void setup() {
      size(1000, 600, P3D);
      eyeX = width/2;
      eyeY = height/2;
      eyeZ = d;
    }
    // -----------------------------------------------------------------
    void draw() {
      // background in the draw loop to make it animate rather than draw over itself
      background(0);
      lights();
      stroke(255);
    
      // CAMERA:
      if (eyeZ<0)
        camera(eyeX, eyeY, eyeZ, 
        width/2, height/2, 0, 
        0, -1, 0);
      else
        camera(eyeX, eyeY, eyeZ, 
        width/2, height/2, 0, 
        0, 1, 0);
    
      pushMatrix();
      translate(width/2, height/2, 0);
      fill(255);
      box(100);
      popMatrix();
    
      pushMatrix();
      translate(width/2-50, height/2-50, 0);
      fill(255, 0, 0);
      box(10);
      popMatrix();
    }
    
    // -----------------------------------------------------------------
    
    void keyPressed() {
      switch(key) {
        // Move camera
      case CODED:
        if (keyCode == UP) {
          ang += 5;
        }
        if (keyCode == DOWN) {
          ang -= 5;
        }
        break;
    
      default:
        // !CODED:
        break;
      } // switch
    
      if (ang>=360)
        ang=0;
      eyeY = (height/2)-d*(sin(radians(ang)));
      eyeZ = d*cos(radians(ang));
      println("Angle "+ang+": "+eyeX+" / "+eyeY+" / "+eyeZ);
    }
    // --------------------------------------------------
    
  • Great...!

    I have implemented your suggestion and it works. Now I understand better the concept of the up-axis. Thank you.

    Besides that, I have also completed the code with your suggested improvements.

    Thank you very much, Chrisir!

  • edited March 2014

    Hi again,

    Imagine I implement the same piece of code to rotate horizontally (x and z-axis) along with the previous code. This horizontal rotation goes well without having to change the up-axis. So, the "if (z<0)" condition wouldn't work right.</p>

    So without using the "if (z<0)" condition, which would be the condition to change the direction of the up-axis? Maybe something related to the value of y?</p>

  • edited March 2014 Answer ✓

    when only rotate horizontally you don't need to change the up-axis

    just say

     camera(eyeX, eyeY, eyeZ,
        width/2, height/2, 0,
        0, 1, 0);
    

    Or do you want to combine both rotations (vertically and horizontally)?

  • edited March 2014 Answer ✓

    here

    // -----------------------------------------------------------------
    float eyeY, eyeX, eyeZ;
    float angVertical = 0;
    int d = 400;
    // -----------------------------------------------------------------
    void setup() {
      size(1000, 600, P3D);
      eyeX = 0;
      eyeY = height/2;
      eyeZ = 0;
    
      eyeX = d*(sin(radians(angVertical)));
      eyeZ = d*cos(radians(angVertical));
    }
    // -----------------------------------------------------------------
    void draw() {
      // background in the draw loop to make it animate rather than draw over itself
      background(0);
      lights();
      stroke(255);
    
      // CAMERA:
      camera(eyeX, eyeY, eyeZ, 
      0, height/2+10, 0, 
      0, 1, 0);
    
      pushMatrix();
      translate(0, height/2, 0);
      fill(255);
      box(100);
      popMatrix();
    
      pushMatrix();
      translate(0, height/2-50, 0);
      fill(255, 0, 0);
      box(10);
      popMatrix();
    }
    
    // -----------------------------------------------------------------
    
    void keyPressed() {
      switch(key) {
        // Move camera
      case CODED:
        if (keyCode == UP) {
          angVertical += 5;
        }
        if (keyCode == DOWN) {
          angVertical -= 5;
        }
        break;
    
      default:
        // !CODED:
        break;
      } // switch
    
      if (angVertical>=360)
        angVertical=0;
      eyeX = d*(sin(radians(angVertical)));
      eyeZ = d*cos(radians(angVertical));
    
      println("With angVertical:  "+angVertical+
        " the cam pos is : "+
        eyeX+" / "+eyeY+" / "+eyeZ);
    }
    // -------------------------------------------------
    
  • edited March 2014

    Thank you again, Chrisir.

    Another odd behavior I see in the code is that, for example in the first code you have posted, the box disappears when z=0; I have tried to see if the problem is in the up-axis, so that I have tried with 'if (eyeZ <= 0)' instead of 'if (eyeZ < 0)' in line 20. But it keeps on disappearing... Is it due to the fact of the up-axis as well?</p>

    Thank you,

  • edited March 2014

    ...

  • edited March 2014

    ...

  • Answer ✓

    sorry I don't know...

    maybe start a new thread?

  • Did you try eyeZ <= 1 For that above?

    Also an angle of 360 is = 0

  • edited March 2014

    Yes, I have tried this, and the same happens. My conclusion is that something odd happens when the camera is located just in the up-axis, but I'm not sure...

Sign In or Register to comment.