1

注意:这是在处理 IDE 中

我正试图让球形轨道下降,我几乎已经做到了。这是我到目前为止所拥有的:

float cameraTheta, cameraPhi, cameraRadius; //camera position in spherical coordinates
float camx, camy, camz;

void setup() {
  size(500, 500, P3D);
  background(255);
  cameraRadius = 200.0f;
  cameraTheta = 2.80;
  cameraPhi = 2.0;
  recomputeOrientation();
}


void draw() {
  background(255);
  lights();
  mouseMotion();
  camera(camx, camy, camz, 0, 0, 0, 0, -1, 0);
  sphereDetail(10);
  sphere(25);
}

void mouseMotion()
{
  if (mousePressed) {
    cameraTheta += (mouseX - pmouseX)*0.05;
    cameraPhi   += (mouseY - pmouseY)*0.05;
  }
  recomputeOrientation();     //update camera (x,y,z) based on (radius,theta,phi)
}

void recomputeOrientation()
{
  camx = cameraRadius * sin(cameraTheta)*sin(cameraPhi);
  camz = cameraRadius * -cos(cameraTheta)*sin(cameraPhi);
  camy = cameraRadius * -cos(cameraPhi);
  redraw();
}

x 旋转效果很好,但是 y 旋转有点从上到下翻滚,然后随着 mouseY 的变化一遍又一遍地备份,我需要的是只要鼠标移动它就可以继续向一个方向翻滚随着鼠标向下移动,向上和向后移动另一个方向。谁能帮我解决这个问题?

4

1 回答 1

1

问题与相机的向上矢量有关。如果你想象拿着相机,让它越来越靠近球体的极点,当你经过极点时,你是如何握住相机的?由于camera 函数的upY参数,处理知道该做什么。

在您当前的代码中,upY始终为 -1,这意味着当相机自行定向时,它将始终使用向量<0, -1, 0>作为up的参考。您需要对此进行更改,以便当它到达球体的极点时,它会颠倒过来。

boolean flip = false;
...
void draw() {
    ...
    camera(camx, camy, camz, 0, 0, 0, 0, flip ? 1.0 : -1.0, 0);
    ...
}

void mouseMotion()
{
    if (mousePressed) {
        cameraTheta += (mouseX - pmouseX) * 0.05;

        if (cameraTheta < 0) cameraTheta += 2 * PI;
        else if (cameraTheta >= 2 * PI) cameraTheta -= 2 * PI;

        if (flip)
            cameraPhi += (mouseY - pmouseY) * 0.05;
        else
            cameraPhi -= (mouseY - pmouseY) * 0.05;

        if (cameraPhi >= PI) {
            cameraPhi = PI - 0.01;
            cameraTheta += PI;
            flip = !flip;
        }
        else if (cameraPhi <= 0) {
            cameraPhi = 0.01;
            cameraTheta -= PI;
            flip = !flip;
        }
    }

    recomputeOrientation();
}

值得注意的事情:

  1. 我添加了代码以将cameraTheta保持在 [0, 2 * PI) 范围内,并将cameraPhi 保持在 [0.01, PI - 0.01) 范围内。在数学上,cameraPhi应该在 [0, PI) 范围内,但这会导致两极闪烁。对于这背后的数学,检查这里
  2. 当翻转为真时, cameraPhi反向递增。
于 2011-12-05T22:10:39.247 回答