Hagop

camera.speed with BABYLON.Ray.CreateNewFromTo bug?

Recommended Posts

Hi all

I needed to know if my free camera is moving or looking forward (+ve z) or backward (-ve z) at any given time. So I wrote the following code. 

 var createScene = function () {
             var scene = new BABYLON.Scene(engine);
            var camera = new BABYLON.TouchCamera("TouchCamera1", new BABYLON.Vector3(0, 10, -60), scene);
            camera.speed = 6;
            scene.activeCamera = camera;
            camera.attachControl(canvas, true);
              var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
                var ground = BABYLON.Mesh.CreateGround("ground1", 460, 460, 2, scene);
        
            return scene;
        
        };
        
       var scene = createScene();
        
        var switchDirection = false;
        var rayZ;
        var previousRayZ;
        
        scene.registerBeforeRender(function () {
                
            if(scene.isReady())
            {    
              scene.activeCamera.position.y = 20;
               directionRay = BABYLON.Ray.CreateNewFromTo(scene.activeCamera.position, scene.activeCamera.getTarget()).direction;
               rayZ = directionRay.z;
            
                if ( (rayZ >0 && previousRayZ <0) || (rayZ <0 && previousRayZ >0) )
                       {
                         switchDirection = true;
                        console.log("rayZ " +rayZ);
                        console.log("previousRayZ " +previousRayZ);
                    }
                                                
                if (switchDirection == true)
                    {
                        switchDirection = false;
                        //do something
                    }
                previousRayZ = rayZ;
                
            }
    });

I noticed the following abnormality in the trace when moving forward with the keyboard (the error does not occur when rotating with the mouse)

rayZ -1
previousRayZ 1
This means scene.activeCamera.getTarget() is less that its position at some stage, whilst it should be always more...
After a lot of headache :wub: I found out that this error occurs only if the camera speed is  a large number ie: 5
The error does not occur
  • when I have a mesh in front of the camera parented to the camera
  • directionRay = scene.activeCamera.getFrontPosition(1).subtract(  scene.activeCamera.position)

Must be a bug

Share this post


Link to post
Share on other sites

Hello! Can you repro in the playground? It is tough to detect a bug here :)

I copy pasted your code here: https://www.babylonjs-playground.com/#986E06

 

Now regarding your question: This happens because you are using beforeRender event. At that time the camera position has been updated but not the target.

You may want to instead use afterRender to be sure all info are synchronized: https://www.babylonjs-playground.com/#986E06#1

Other option: You can force the cache to update with camera.getViewMatrix(true): https://www.babylonjs-playground.com/#986E06#2

Share this post


Link to post
Share on other sites

Well Delatosh, if the camera position is updated before the target, then why is it working when camera.speed = 1 ?

Anyhow afterRender  solves the issue.

Finally where do we specify camera.getViewMatrix(true)  ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.