一个物体绕着某条线运动-方法1

https://playground.babylonjs.com/#N9IZ8M#1
1 .关键MovePov

const points = [];
    points.push(new BABYLON.Vector3(2, 0, 2));
    points.push(new BABYLON.Vector3(2, 0, -2));
    points.push(new BABYLON.Vector3(-2, 0, -2));
    points.push(points[0]); //close the triangle;

    BABYLON.MeshBuilder.CreateLines("triangle", {points: points})

    const slide = function (turn, dist) { //after covering dist apply turn
        this.turn = turn;
        this.dist = dist;
    }
    
    const track = [];
    track.push(new slide(Math.PI / 2, 4));
    track.push(new slide(3 * Math.PI / 4, 8));
    track.push(new slide(3 * Math.PI / 4, 8 + 4 * Math.sqrt(2)));

    let distance = 0;
    let step = 0.05;
    let p = 0;

    scene.onBeforeRenderObservable.add(() => {
        sphere.movePOV(0, 0, step);
        distance += step;
              
        if (distance > track[p].dist) {        
            sphere.rotate(BABYLON.Axis.Y, track[p].turn, BABYLON.Space.LOCAL);
            p +=1;
            p %= track.length;
particleSystem.emitter.x=sphere.position.x
        particleSystem.emitter.y=sphere.position.y
        particleSystem.emitter.z=sphere.position.z
            if (p === 0) {
                distance = 0;
                sphere.position = new BABYLON.Vector3(2, 0, 2); //reset to initial conditions
                sphere.rotation = BABYLON.Vector3.Zero();//prevents error accumulation
            }
        }
    });

结论,所以这样就实现了我们要的效果,注意这里的线段,一个三角形,不是一个直线

你可能感兴趣的:(一个物体绕着某条线运动-方法1)