Jump to content

camera.speed with BABYLON.Ray.CreateNewFromTo bug?


Hagop
 Share

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

Link to comment
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

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...