2

我正在尝试使用 Jonathan Feinberg 的 Peasycam 库进行处理,以围绕对象的 Z 轴旋转相机。文档指定旋转是围绕主题,而不是对象。这似乎很难实现。peasycam 旋转围绕主题(即相机)而不是对象,尽管在控制意义上 peasycam 的重点是面向对象的。并且设置camera()似乎也有问题,因为我无法让 peasycam 记住分配的相机的位置。还有一个差异,即 peasycam 的 y 轴映射到数据空间的 z 轴。

我已经制作了一个基本模型来帮助以交互方式解释这一点。如果一些新鲜的眼睛可以帮助解开这个问题,我将不胜感激。Peasycam 可能需要安装到应用程序的库文件夹中。总体目标是为了动画目的能够在 Z 轴上旋转 3D 数据图。提前致谢。

import peasy.*;
PVector camPos;
PVector[] points = new PVector[50];
float angleXY, d;
PeasyCam cam;

void setup() {
  size(300,300,P3D);
  cam = new PeasyCam(this, 100);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(150);

  for(int i=0; i<50; i++) points[i] = new PVector(random(-15,15),random(-15,15),random(-15,15));
}

void draw() {
  background(250);
  noFill();
  box(30);

  // axes for frame of reference
  stroke(255,0,0);         // red = Z
  line(0,0,-100,0,0,100);
  stroke(0,255,0);         // green = Y 
  line(0,-100,0,0,100,0);
  stroke(0,0,255);         // blue = X
  line(-100,0,0,100,0,0);

  // points on axes to denote positive orientation
  strokeWeight(3);
  for(PVector p:points) point(p.x, p.y, p.z);
  strokeWeight(5);
  point(40,0,0);
  point(0,40,0);
  point(0,0,40);
  strokeWeight(1);

  stroke(0);

  camPos = new PVector(cam.getPosition()[0], cam.getPosition()[1], cam.getPosition()[2]);  
  angleXY = degrees(atan2(camPos.z, camPos.x));  // camera XY angle from origin
  d = sqrt(pow(camPos.z, 2) + pow(camPos.x, 2)); // camera-object XY distance (compare to cam.getDistance())

  // ZX campera slots map to XY data plane:
  println("campos: " + camPos + " " + ", ang: " + angleXY + ", dist:" + d); 
}

void keyPressed(){
  if(key=='r') setup(); // restart
  if(key==' ') camera(camPos.x, camPos.y, camPos.z, 0, 0, 0, 0, 0, 1); // stabilise image on Z axis

  if(key=='d') {
    angleXY += radians(1);
    camera(sin(angleXY)*d, camPos.y, cos(angleXY)*d, 0, 0, 0, 0, 1, 0);
  }

  // peasycam's rotations work around the subject:
  if(key=='p') cam.rotateY(radians(2));
}
4

2 回答 2

3

您误解了 Peasycam 文档中的“主题”一词。“主题”仅仅是“观察”点。在您要查看的对象内部选择一个点,然后使用该点构建 Peasycam,或者稍后设置它。

PeasyCam(PApplet parent, double lookAtX, double lookAtY, double lookAtZ, double distance);

camera.lookAt(double x, double y, double z);
camera.lookAt(double x, double y, double z, double distance);

Peasycam 不太适合程序控制。如果您想自己操作视图,最好使用优秀的ProsceneOCD库。

编辑:如果您想将 Peasycam 的运动限制在一个轴或另一个轴上,您可以使用以下方法:

// By default, the camera is in "free rotation" mode, but you can
// constrain it to any axis, around the look-at point:
camera.setYawRotationMode();   // like spinning a globe
camera.setPitchRotationMode(); // like a somersault
camera.setRollRotationMode();  // like a radio knob
camera.setSuppressRollRotationMode();  // Permit pitch/yaw only.

// Then you can set it back to its default mode:
camera.setFreeRotationMode();
于 2013-07-17T13:37:12.977 回答
1

不是这个问题的确切答案,而是类似的东西 - 我想简单地设置位置(移动)相机,但这似乎很复杂peasycam。所以我想尝试下一个最简单的库,OCD。

所以 - 由于 OCD 目前显然不包含安装中的示例(仅在Obsessive Camera Direction API 参考中) - 下面是.pde基本示例的代码peasycam,修改为与 OCD 库一起使用(草图实际上是指两个库,并允许您通过设置USEPEASY变量轻松比较两者的行为)。大多数peasycam交互都是为 OCD 复制的 - 但是,您不会像使用 PeasyCam 那样使用 OCD 获得动画“补间”。

// modification of example on http://mrfeinberg.com/peasycam/
// sdaau, 2014

private static final boolean USEPEASY = false;

// cannot do conditional import in Java! (expecting EOF, found 'if')
// http://stackoverflow.com/questions/11288083/javaconditional-imports
//if(USEPEASY) {
  import peasy.*;
//} else {
  import damkjer.ocd.*;
//}

// ... nor conditional globals! (expecting EOF, found 'if')
//if(USEPEASY) { 
  PeasyCam cam;
//} else {
  Camera cam1;
//}

void setup() {
  size(200,200,P3D);
  if(USEPEASY) {
    cam = new PeasyCam(this, 100); // (PApplet parent, double distance); // look at 0,0,0
    cam.setMinimumDistance(50);
    cam.setMaximumDistance(500);
  } else {
    cam1 = new Camera(this, // (parent,
      0, 0, 100,            // cameraX, cameraY, cameraZ, 
      0, 0, 0,              // targetX, targetY, targetZ
      50, 500               // nearClip, farClip) //(doesn't clip as peasycam!)
    ); 
  }
}
void draw() {
  if(!USEPEASY) {
    cam1.feed(); //"send what this camera sees to the view port"
  }
  // these rotates seem just to set initial view, in either case:
  rotateX(-.5);
  rotateY(-.5);
  // actual drawing:
  background(0);
  fill(255,0,0);
  box(30);
  pushMatrix();
  translate(0,0,20);
  fill(0,0,255);
  box(5);
  popMatrix();
}

// ... nor conditional methods! (expecting EOF, found 'if')
//if(!USEPEASY) {
void mouseDragged() {
  if(!USEPEASY) {
    if (mouseButton == LEFT) {
      // http://www.airtightinteractive.com/demos/processing/bezier_ribbon_p3d/BezierRibbons.pde
      cam1.arc(radians(-(mouseY - pmouseY))/4);
      cam1.circle(radians(-(mouseX - pmouseX))/4);
    } else if (mouseButton == RIGHT) {
      cam1.zoom(radians(mouseY - pmouseY) / 2.0);
    } else if (mouseButton == CENTER) {
      // peasycam calls this .pan(); damkjer.ocd calls it .track()
      cam1.track(-(mouseX - pmouseX), -(mouseY - pmouseY));
    }
  } // end if(!USEPEASY)
}
void mouseWheel(MouseEvent event) {
  if(!USEPEASY) {
    float e = event.getCount();
    cam1.zoom(e/3.0);
  } // end if(!USEPEASY)
}
//} // end if(!USEPEASY)
于 2014-11-05T10:58:55.250 回答