Jump to content

Device Orientation with Object Direction


tranlong021988
 Share

Recommended Posts

hi everyone , I'm very new with BabylonJS , I 'm building my first game with this engine. The gameplay, is a little bit with Assansin Creed demo : http://race.assassinscreedpirates.com/

My game have difference: it's target mobile browser with VR technology. Player will use CardBoard to navigate their jet. Thanks to VRDeviceOrientationFreeCamera , I can set it up easily to visualize 3d viewer in my game.

The only problem is I did set jet rotation same with camera rotation, everything work fine on PC but on mobile browser, if I (and my card board) look higher, the jet direction come "flip" so it make my jet go wrong way. 

This is my code:

 scene.registerBeforeRender(function(){
                    // make jet go base on its rotation.
                    var deg = jet.rotation.y*180/Math.PI - 90;
                    jet.position.x+=0.1*Math.cos(deg*Math.PI/180);
                    jet.position.z-=0.1*Math.sin(deg*Math.PI/180);

                   // place camera behind the jet ("always").
                    scene.activeCamera.position.x = jet.position.x+1*Math.cos((deg-180)*Math.PI/180);
                    scene.activeCamera.position.z = jet.position.z-1*Math.sin((deg-180)*Math.PI/180);
                    
         // set jet rotation same with camera rotation. 
                    jet.rotation.y = scene.activeCamera.rotation.y;
               
                    
                
                })},false);  

I think because of activeCamera.roation.y input by mobile device orientation is different with PC. But I have no idea about device orientation , so anyone can help me ?  

this is what I get til now: https://dl.dropboxusercontent.com/u/86585940/BabylonWave/index.html  , just try it on mobile browser with landscape mode. 

Sorry for my poor English.

Link to comment
Share on other sites

Hi,

I had the exact same problem when trying to move the camera in the direction it was looking. Axes were flipping when the head is looking up.

I made an ugly (but working fix). Hope it helps a bit.

if (Math.abs(camera.rotation.x)>2 && Math.abs(camera.rotation.z)>2) {
      camSpeed = camSpeed*-1;
      camera.rotation.z = Math.abs(camera.rotation.z*-1);
}

Link to comment
Share on other sites

6 hours ago, Flomotion said:

Hi,

I had the exact same problem when trying to move the camera in the direction it was looking. Axes were flipping when the head is looking up.

I made an ugly (but working fix). Hope it helps a bit.

if (Math.abs(camera.rotation.x)>2 && Math.abs(camera.rotation.z)>2) {
      camSpeed = camSpeed*-1;
      camera.rotation.z = Math.abs(camera.rotation.z*-1);
}

thanks for answering me,

I did firgure out that flipped issue happened by rotation of camera if value > 1 , so I make a condiction code:

if(scene.activeCamera.rotation.x<1){
                    jet.rotation.y = scene.activeCamera.rotation.y;
                }else{
                    jet.rotation.y = scene.activeCamera.rotation.y+Math.PI;
                } 

now it work well.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...