Search code examples
javascriptbabylonjs

babylon.js camera back to initial point when trying to forward a path


I create a function that move a camera through a specific goal :

var createScene2=function()
{
        var MyCurve;
        var MyGoal = new BABYLON.Vector3(0,10,5);
        var scene = new BABYLON.Scene(engine);
        var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 5, new BABYLON.Vector3(0,0,0), scene);
        camera.attachControl(canvas, true);
        var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);
        var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 1, -1), scene);
        var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {}, scene);
        MyCurve= MyPath(camera.position, MyGoal);
        MoveCameraThrough(scene, camera , MyCurve);
        return scene
}

when I call the render :

        var scenee= createScene2();

      engine.runRenderLoop(function () {

           scenee.render();

        });   

It works fine, but when the camera position arrives to the specific goal, it restarts from the initial point

any Idea ?

thanks

Anes


Solution

  • I solved the problem. it was in MoveCameraThrough() function

    function MoveCameraThrough( scene , camera, MyCurve)
    {
    const path3d = new BABYLON.Path3D(MyCurve.getPoints());
    const tangents = path3d.getTangents(); // array of tangents to the curve
    const normals = path3d.getNormals(); // array of normals to the curve
    const binormals = path3d.getBinormals(); // array of binormals to curve
    const speed = 50*Math.floor(Math.random() * (7 - 3 + 1)) + 3; // const speed = 1
    const animationPosition = new BABYLON.Animation('animPos', 'position', speed, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
    const animationRotation = new BABYLON.Animation('animRot', 'rotation', speed, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
    const keysPosition = [];
    const keysRotation = [];
    
    for (let p = 0; p < MyCurve.getPoints().length; p++) {
    keysPosition.push({
    frame: p,
    value: MyCurve.getPoints()[p]
    });
    keysRotation.push({
      frame: p,
      value: BABYLON.Vector3.RotationFromAxis(normals[p], binormals[p], tangents[p])
    });
    }
    animationPosition.setKeys(keysPosition);
    animationRotation.setKeys(keysRotation);
    
    camera.animations=[
    animationPosition,
    animationRotation
    ];
    scene.beginAnimation(camera, 0, 200, false);
    }

    and exactly in

    scene.beginAnimation(camera, 0, 200, false);

    the proprety "false" was "true" and then it loops the scene.

    Thank you

    Anes