如何移动一条沿着曲线路径行驶的汽车

时间:2015-10-19 10:33:41

标签: javascript three.js

我想让汽车沿着弯曲的路径移动/旋转。我想根据路径设置车轮和车的方向。

我可以设置一个初始方向,但其余的路径呢?

(这是我试过的

var agl = Math.atan2(
  this.latLonG[0].lat - this.tiler.lastBbox.c.lat//this.latLonG[0].lat 
  ,this.latLonG[0].lon - this.tiler.lastBbox.c.lon//this.latLonG[0].lon
  ); 

备注:

  1. 我需要推断汽车在每个车架上的样条曲线上的哪个点。如果我想在5秒内完成路径,我该怎么做?
  2. 我希望能够控制速度:如何实现它?

1 个答案:

答案 0 :(得分:1)

沿着曲线移动汽车,按照其方向:

  1. 首先需要定义样条曲线的要点
  2. 然后创建此样条曲线
  3. 增加一个代表汽车在曲线上,开始(0)和结束(1)之间的数字
  4. 让汽车更进一步。

    Tools > Options
  5. 要使车轮转动,您可以获得方向矢量和先前方向矢量的叉积。假设您在x轴和z轴的平面上移动汽车,则车轮的方向将与结果矢量的y值成比例:

    //1. create spline's points
    var points = [
        new THREE.Vector3( x1, y1, z1 ),
        new THREE.Vector3( x2, y2, z2 ),
        ...
        new THREE.Vector3( xn, yn, zn )
        ];
    
    //2. create spline
    var spline = new THREE.CatmullRomCurve3( points );
    //since r72. Before : THREE.SplineCurve3
    
    //3. define the car position on the spline, between its start (0) and end (1)
    var carPositionOnSpline = 0;
    
    //Then at each increment (in your render loop or in the 'update' function of a tween)
    var newPosition = spline.getPoint( carPositionOnSpline );
    car.position.copy( newPosition );
    
    //Also update the car's orientation so it looks at the road
    var target = spline.getPoint( carPositionOnSpline + .001 );
    car.lookAt( target );//+.001 or whatever
    
相关问题