[PeasyCam]How to get Up Vector

edited September 3 in Library Questions

Hi, it's two days that I'm trying to get the up vector of the peasycam with some problem.

I have the pitch, yaw and roll of the camera

float[] rotations = camera.getRotations(); // x, y, and z rotations required to face camera in model space
camera.setRotations(double pitch, double yaw, double roll); // rotations are applied in that order

so x = pitch, y = yaw, z = roll

and following this Stackoverflow question the vector should be:

x = -cos(yaw)sin(pitch)sin(roll)-sin(yaw)cos(roll)
y = -sin(yaw)sin(pitch)sin(roll)+cos(yaw)cos(roll)
z =  cos(pitch)sin(roll)

The problem is that when I try to rotate the camera around the y axis the Up Vector change.

If I have the camera at (0,0,400) watching (0,0,0) the Up Vector given by the formula is (0,1,0) while if I rotate the camera at (0,0,-400) watching (0,0,0) the formula give (0,-1,0).

Am I doing something wrong?

Tagged:

Answers

  • edited September 3

    so x = pitch, y = yaw, z = roll

    i don't think those are positions though, they are angles, angles around the 3 axes.

    If I have the camera at (0,0,400)

    but these are positions.

    what yaw, pitch, roll are you using in the calculations in the two cases?

  • edited September 3

    runnable example based on the above. press a key, rotate the cube 180 degrees (around y) and press a key again.

    import peasy.*;
    
    PeasyCam cam;
    
    void setup() {
      size(300, 300, P3D);
      cam = new PeasyCam(this, 400);
      strokeWeight(2);
    }
    
    void draw() {
      background(255);
      // axes (x,y,z -> r,g,b)
      stroke(255, 0, 0);
      line(-100, 0, 0, 100, 0, 0);
      stroke(0, 255, 0);
      line(0, -100, 0, 0, 100, 0);
      stroke(0, 0, 255);
      line(0, 0, -100, 0, 0, 100);
      // box
      stroke(0);
      box(50);
    }
    
    void keyPressed() {
      float[] rotations = cam.getRotations(); // x, y, and z rotations required to face camera in model space
      float pitch = rotations[0];
      float yaw = rotations[1];
      float roll = rotations[2];
      println("Rot: ", yaw, pitch, roll);
      float x = -cos(yaw) * sin(pitch) * sin(roll) - sin(yaw) * cos(roll);
      float y = -sin(yaw) * sin(pitch) * sin(roll) + cos(yaw) * cos(roll);
      float z =  cos(pitch)* sin(roll);
      println("X,Y,Z", x, y, z);
      println();
    }
    
Sign In or Register to comment.