makhras
makhras

Reputation: 23

Is there a way to recenter / reset orientation?

I just wanted to know if there is a buit-in function to reset head rotation. Most VR SDKs I've seen (Oculus, Carboard, etc) have a way to make current rotation the default forward rotation. Is there a simple way to do this in a-frame?

Upvotes: 1

Views: 1916

Answers (1)

ngokevin
ngokevin

Reputation: 13233

You can add a wrapper entity around the camera, and on an event, set the entity wrapper's rotation about the Y-axis to be negative the camera's rotation about the Y-axis.

Here's an example (but in React) using shake.js by @jhsu:

import React from 'react';
import Camera from './Camera';
import Shake from 'shake.js';

class Player extends Component {
  componentDidMount() {
    this.shaker = new Shake({
      threshold: 5,
      timeout: 250,
    });
    this.shaker.start();
    window.addEventListener('shake', this.reorient, false);
  }

  reorient() {
    if (this.camera) {
      const rotation = this.camera.getAttribute('rotation');
      this.setState({
        orientation: { x: 0, y: -1 * rotation.y, z: 0 }
      });
    }
  }

  cameraMounted = (camera) => {
    this.camera = camera;
  }

  render() {
    return <Camera
      rotation={this.state.orientation}
      onMount={this.cameraMounted}
    />
  }
}

A component would be:

AFRAME.registerComponent('resetorientation', {
  init: function () {
    var el = this.el;
    el.addEventListener('resetorientation', function () {
      var y = el.querySelector('[camera]').getAttribute('rotation').y;
      el.setAttribute('rotation', {y: -1 * y});
    });
  }
});

<a-entity id="cameraWrapper" resetorientation><a-camera></a-camera></a-entity>

document.querySelector('#cameraWrapper').emit('resetorientation');

Upvotes: 2

Related Questions