【问题标题】:P3D camera orientationP3D 相机方向
【发布时间】:2017-11-21 02:18:39
【问题描述】:

我有一个大球体。有一个红点在球体周围移动。当它移动到球体时,我想跟随那个红点。所以我的相机必须随着红点移动。但有一个问题。现在,我正在体验的是展览 B 中显示的内容。我希望我的动画能够实现展览 A 中显示的观点。

这是我到目前为止的代码。我认为这很简单。我有 3 个变量控制我的眼睛在哪里,我有 3 个变量控制目标在哪里。红点也位于目标位置。我在 x-y 中添加了 2 个平面,这有助于我在物体旋转时不会太困惑。

这是一个小提琴:

https://jsfiddle.net/da8nza6y/

float radius = 1000;
float view_elevation = 1500;
float target_elevation = 300;

float x_eye;
float y_eye;
float z_eye;

float x_aim;
float y_aim;
float z_aim;
float h;
float theta;

void setup() {
  size(600, 600, P3D);
  theta = 0;
  h = 30;
}

void draw() {

  theta += 0.5;
  theta = theta%360;

  x_eye = (radius+view_elevation)*cos(theta*PI/180);
  y_eye = 0;
  z_eye = (radius+view_elevation)*sin(theta*PI/180);

  x_aim = (radius+target_elevation)*cos((theta+h)*PI/180);
  y_aim = 0;
  z_aim = (radius+target_elevation)*sin((theta+h)*PI/180);

  camera(x_eye, y_eye, z_eye, x_aim, y_aim, z_aim, 0, 0, -1);

  background(255);

  // the red dot
  pushMatrix();
    translate(x_aim, y_aim, z_aim);
    fill(255, 0, 0, 120);
    noStroke();
    sphere(10);
  popMatrix();

  // the big sphere
  noStroke();
  fill(205, 230, 255);
  lights();
  sphere(radius);

  // the orange plane
  pushMatrix();
    translate(0, 0, 10);
    fill(255, 180, 0, 120);
    rect(-2000, -2000, 4000, 4000);
  popMatrix();

  // the green plane
  pushMatrix();
    translate(0, 0, -10);
    fill(0, 180, 0, 120);
    rect(-2000, -2000, 4000, 4000);
  popMatrix();

}

所以泡菜是这样的,似乎红点(其在 xz 平面中的位置由角度 (theta+h) 和距原点的距离 (radius+target_elevation) 给出)穿过 xy 平面,一切都会颠倒过来。

现在,我尝试控制 camera() 函数中的最后 3 个变量,但我感到困惑。该函数的文档在这里:

https://processing.org/reference/camera_.html

谁能看到这个问题的解决方案?

另外,我确信我可以只旋转球体(我可以这样做)而不会遇到这些问题,但我确定我将使用这个动画去哪里,我觉得会有事情发生使用这种方法会更容易。虽然我可能弄错了。

【问题讨论】:

    标签: processing processing.js


    【解决方案1】:

    我相信我已经解决了自己的问题。

    在调用camera() 函数之前,我在draw 中添加了以下几行:

      if ((x_eye- x_aim) < 0) {
        z_orientation = 1;
      } else {
        z_orientation = -1;
      }
    

    我注意到触发翻转的不是(theta+h),而是视图和目标的相对位置。

    这是一个更新的小提琴:

    https://jsfiddle.net/da8nza6y/1/

    【讨论】:

    • 你应该accept this answer 这样这个问题就不会被标记为需要回答。
    • 我会这样做的。当我点击接受时,我会收到一条提示,告诉我必须等待 2 天才能接受我对自己帖子的回答。
    猜你喜欢
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多