Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using three.js functions with camera. #15

Open
CodeLuca opened this issue Nov 8, 2015 · 4 comments
Open

Using three.js functions with camera. #15

CodeLuca opened this issue Nov 8, 2015 · 4 comments

Comments

@CodeLuca
Copy link

CodeLuca commented Nov 8, 2015

So I'm trying to get the camera towards where I am looking at. There is a method that easily allows me to do this, built into three.js:

VR.camera.object.translateZ(delta);

This works fine when I put it in the animate function, but obviously I am going backwards. When I try to make delta negative the frames start to go strange and the whole thing breaks.

VR.camera.object.translateZ(-delta);

I know that the camera object isn't really meant to be manipulated but this is really strange.

@skompc
Copy link

skompc commented Nov 8, 2015

I had this problem at first... Use this to go forward:
+ VR.camera.moveZ(-(Math.PI * delta * 2));

And this to go backwards (away from where ur looking at):
+ VR.camera.moveZ(-(Math.PI * delta * 2));

@skompc
Copy link

skompc commented Nov 8, 2015

Ignore the pluses

@skompc
Copy link

skompc commented Nov 8, 2015

Darn!... This to go back: VR.camera.moveZ(Math.PI * delta * 2);

@brianchirls
Copy link
Contributor

Hey guys,

First of all, never move the camera, because it will conflict with the headset. Move the body instead. And, of course, be careful about doing so because this is a great way to cause simulator sickness.

Multiplying delta by 2 PI doesn't change anything except to speed up the movement by a factor of a little more than 6.

This solution seems to work. See step-by-step explanation in comments.

VR.floor();
var camera = VR.camera.object;

// create a single scratch vector to re-use so we don't have to allocate
// one on every frame. Garbage collection can cause barf-inducing judder.
var lookDirection = new THREE.Vector3();
VR.animate(function(delta){
  // get direction camera is looking relative to the world
  camera.getWorldDirection(lookDirection);

  // set y to zero so we only move on the horizontal plane, not up or down
  lookDirection.y = 0;

  // don't move or try to normalize if length is very small,
  // because it can cause a divide-by-zero error
  // or otherwise create a weird/terrible experience
  if (lookDirection.lengthSq() > 0.01) {
    // normalize so we can get a constant speed
    lookDirection.normalize();
    lookDirection.multiplyScalar(delta);

    // move body, not the camera
    VR.body.position.add(lookDirection);
  }
});

live example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants