Search code examples
javascriptbabylonjs

In Babylon.js how can I get/set a FreeCamera's rotation or pan angle?


I am trying to capture a FreeCamera's location and pan angle or rotation so I can reposition the camera later with the exact same view.

(I am working with an altered version of the Collision example at http://www.babylonjs-playground.com/)

I seem to be able to get camera.position.x, camera.position.y and camera.position.z ok but camera.cameraRotation.y always yields zero.


Solution

  • The method I've come up with that seems to work reliably is to first collect the various camera stats:

    var firstPosition=camera.position.x.toFixed(2)+"/"+camera.position.z.toFixed(2)+"/"+camera.rotation.x.toFixed(2)+"/"+camera.rotation.y.toFixed(2)+"/"+camera.rotation.z.toFixed(2);
    

    which I store in a database. Then, when needed, I get the camera position by:

    var firstArray=firstPositionL.split("/");
    var firstX=Number(firstArray[0]);
    var firstZ=Number(firstArray[1]);
    var firstRx=Number(firstArray[2]);
    var firstRy=Number(firstArray[3]);
    var firstRz=Number(firstArray[4]);
    

    Then when recreating the scene I add this just at the last of the scene creation. I put it into a delay function to give the scene time to establish itself:

    var myVar = setInterval(function(){ myTimer() }, 1000);
    function myTimer() {            
      camera.position.x=firstX;
      camera.position.z=firstZ;
      camera.rotation.x=firstRx;
      camera.rotation.y=firstRy;
      camera.rotation.z=firstRz;
      clearInterval(myVar);
    }