0

Dearest A-frame-community,

For a recent project I tried to use devicemotion-data to calculate the movement of the camera. Here is the code:

let time = new Date().getTime()/1000;

window.addEventListener( "devicemotion", (e) => {
                      props.setMotionData(e.acceleration);
                      const newTime = new Date().getTime()/1000;
                      const timeDiff = newTime-time;
                      applyMotion({
                        x: e.acceleration.x*Math.pow(timeDiff,2)/2,
                        y: e.acceleration.y*Math.pow(timeDiff,2)/2,
                        z: e.acceleration.z*Math.pow(timeDiff,2)/2
                      });
                      time = newTime;
                  });

const applyMotion = (motionData) =>{

  const camera = document.getElementById('aframe-camera').object3D;
  const cameraRotation = camera.rotation;
  let cameraPosition = camera.position;

  const motionContainer = document.getElementById('motionContainer').object3D;
  motionContainer.position.set(motionData.y, motionData.x, motionData.z);
  motionContainer.rotation.set(cameraRotation._x, cameraRotation._y, cameraRotation._z);

  let cameraMove = new THREE.Vector3();
  motionContainer.getWorldPosition(cameraMove);

  let finalPosition = new THREE.Vector3(cameraPosition.x, cameraPosition.y , cameraPosition.z);
      finalPosition.add(cameraMove);

  cameraPosition.set(finalPosition.x, finalPosition.y, finalPosition.z);
}

It sort of worked when I subtracted ~0.25 from acceleration data and multiplied the results by 1000, but it was very inaccurate and ugly. Haggled a lot with it and didn't get satisfying results so now I am using ar.js and a marker for orientation, but that has its limits and is useless without the marker of course. Did someone try this before? Any ideas or thoughts are welcome. :)

Regards Orys


Found this older post about acceleration data, but I think the data I am getting is already sonsor-fused, or is it not?

OrysB
  • 5
  • 2

0 Answers0