简体   繁体   中英

3D Movement in Processing

I am making a processing sketch which uses the Oculus Rift Dk1, and the library SimpleOculusRift. Overall, what my problem is, is that I cannot seem to figure out a way to join movement and eye position on the rift. So whichever way you are looking(in the rift) is the direction you will move when you hit forward.

Currently, I have it set up so you move on ax and z axis. ( Using translate() and the up down left and right arrow keys. )

I used this code from the library's example to get the numbers I need which show the direction I am looking at.

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

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

When I add println(dir); I get a pair of 3 numbers looking something like this:

[ 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 ]

If I am looking straight ahead I get something similar to 50,0,0 and behind me I get -50,0,0. When I look to my right, I get 0,0,50, and to my left 0,0,-50. Everything else is just numbers in between that are constantly updating.

For grid movement, in a simplified definition - I am using multiple PVectors which add a small number when you hit a up or down or left or right arrow key, which gives me movement.

Here is the code for my movement on the grid:

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;

  }
}

I then add the PVector walk, to the PVector spawn. The spawn PVector are the 3 'spawn' points for the sketch when you start. The walk PVector simply adds a number (or force) to make it move.

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

In conclusion, I just can't seem to figure out how to move forward in the direction I am looking towards using the Oculus Head Set. Hope this was clear enough. Thank you in advance to anyone that could maybe solve this problem that I have been picking at all day.

Since I cannot post a photo of my sketch, its basically a 3D grid box that you can move around in, within processing.

As far as I understood, you would like to move forward always in direction, which the user look.

Using vectors is the first step. You add the value of the vectors with .add. So basically for every frame: New Location = Location + the applied velocity to the current location. Assuming location vector and velocity

location.add(velocity)

Since you will be adding 'velocity' in draw/update, you will need to set up some limit on the acceleration, in contrast of pressing a button where the limit is number of clicks.

About the 3D, if you mean forward as in going towards the depth of the screen (as in computer games) that would be -Z in processing. Check out the 3D transformations if needed.

From your code I can see that you are having already PVector dir. However you will need a location vector as well and to subtract the input with the location, instead of multiplying. Than you will probably need to normalize() the direction, add the velocity and the changes finally to the location:

  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);
  ----

I hope you got the idea and can put it yourself in context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM