处理中的 3D 运动

3D Movement in Processing

我正在制作一个使用 Oculus Rift Dk1 和库 SimpleOculusRift 的处理草图。总的来说,我的问题是,我似乎无法想出一种方法来加入裂缝上的运动和眼睛位置。所以无论你朝哪个方向(在裂缝中)看,都是你向前击球时移动的方向。

目前,我已将其设置为让您在 x 轴和 z 轴上移动。 (使用 translate() 和上下左右箭头键。)

我使用图书馆示例中的这段代码来获取我需要的数字,这些数字显示了我正在看的方向。

 PMatrix3D headOrientationMatrix = oculusRiftDev.headOrientationMatrix();
  headOrientationMatrix.translate( 50, 0, -10 );

  PVector eyePos = new PVector(0, 0, 0);
  PVector dir = headOrientationMatrix.mult(eyePos, null);

当我添加 println(dir);我得到一对 3 个数字,看起来像这样:

[ 1.7912731, -11.410956, -49.66469 ]
[ 1.7770205, -13.271997, -49.20057 ]
[ 2.1940613, -14.491295, -48.838394 ]
[ 2.7236567, -14.90203, -48.687893 ]
[ 3.203349, -14.766355, -48.700035 ]
[ 3.7733526, -14.435215, -48.758453 ]
[ 4.232833, -13.569878, -48.96878 ]
[ 4.137532, -13.105787, -49.103153 ]
[ 4.211087, -12.630456, -49.22132 ]
[ 4.1642704, -11.712718, -49.451702 ]
[ 3.9711514, -10.351211, -49.770298 ]
[ 6.6861124, -7.4438334, -49.998856 ]
[ 7.1228933, -7.5860662, -49.917095 ]

如果我直视前方,我会得到类似于 50,0,0 的东西,而在我身后,我会得到 -50,0,0。当我向右看时,我得到 0,0,50,向左看时得到 0,0,-50。其他一切都只是不断更新的介于两者之间的数字。

对于网格移动,在一个简化的定义中 - 我使用多个 PVectors,当您按下向上或向下或向左或向右箭头键时,它们会添加一个小数字,这使我移动。

这是我在网格上移动的代码:

void moveX() {
  //movement for x axis
  if (keyPressed == true) {
    if (key == CODED) {

      if (keyCode == LEFT) {
        walk.x += 1;
      } 
      if (keyCode == RIGHT) {
        walk.x -= 1;
      } 
    }
  }
  if(keyPressed == false){
    walk.x = 0;
  }
}


void moveZ() {
  //movement for z axis
  if (keyPressed == true) {
    if (key == CODED) {
      if (keyCode == DOWN) {
        walk.z -= .5;
      }  
      if (keyCode == UP) {
        walk.z += .5;
      }
    }
  }

  if(keyPressed == false){
    walk.z = 0;

  }
}

然后我将 PVector walk 添加到 PVector spawn。生成 PVector 是开始时草图的 3 'spawn' 点。 walk PVector 只是添加一个数字(或力)使其移动。

translate( spawn.x, spawn.y, spawn.z);

总而言之,我似乎无法弄清楚如何朝着使用 Oculus 头戴设备的方向前进。希望这已经足够清楚了。提前感谢任何可能解决我一整天都在挑选的这个问题的人。

由于我无法 post 我的素描照片,它基本上是一个 3D 网格框,您可以在处理过程中在其中四处移动。

据我了解,您希望始终朝着用户看到的方向前进。

使用向量是第一步。您使用 .add 添加 vectors 的值。所以基本上对于每一帧:新位置=位置+应用到当前位置的速度。假设位置向量和速度

location.add(velocity)

由于您将在 draw/update 中添加 'velocity',因此您需要在加速度上设置一些 limit,与按下限制为次数的按钮相反点击次数。

关于 3D,如果你的意思是向前,即朝向屏幕的深度(如在电脑游戏中),那么处理中将是 -Z。 Check out the 3D transformations if needed.

从你的代码中我可以看出你已经有了 PVector 目录。但是,您还需要一个位置向量,并用位置减去输入,而不是相乘。比你可能需要 normalize() 的方向,添加速度和最后的变化到位置:

  PVector eyePos = new PVector(0, 0, 0);
  PVector location = new PVector(0, 0, 0);
  PVector dirOculus = headOrientationMatrix.mult(eyePos, null); //Double check, why you need to mult by null vector.
  PVector dir = PVector.sub(dirOculus, location);

  dir.normalize();
  acceleration = dir;
  velocity.add(acceleration);
  location.add(velocity);
  ----

我希望你明白了,并且可以把它放在上下文中。