Three.js Enemy向球员移动

时间:2013-09-26 00:06:31

标签: javascript geometry three.js

在three.js中,我在xyz上有一艘太空飞船,并且它喜欢它在xyz飞向一个行星的网格物体。

我无法为我的生活弄清楚这一点。

需要沿着直线行进,速度恒定朝向地球。

2 个答案:

答案 0 :(得分:1)

 updateFcts.push(function(delta, now){

    if (shipArr[0]===undefined){
}else{  

       //create two vector objects
var xd = new THREE.Vector3(marsMesh.position.x,marsMesh.position.y,marsMesh.position.z);
var yd = new THREE.Vector3(shipArr[0].position.x,shipArr[0].position.y,shipArr[0].position.z);
//find the distance / hypotnuse to the xyz location
var dicks = shipArr[0].position.distanceTo(marsMesh.position);

 var subvec = new THREE.Vector3();
     subvec = subvec.subVectors(xd,yd);
     //sub subtrac the 3 vectors.
   var hypotenuse = dicks;
     console.log(hypotenuse);
   //1.5 stops it at 1.5 distance from the target planet


       if(hypotenuse > 1.5){
        //console.log(hypotenuse);

    shipArr[0].position.y += .0001*200*(subvec.y/hypotenuse);
    shipArr[0].position.x += .0001*200*(subvec.x/hypotenuse);
    shipArr[0].position.z += .0001*200*(subvec.z/hypotenuse);
    }else{
    //within fire range
    alert ("FIIIIIRE");

    }


}

})

我尝试了tween.js并且不高兴所以我自己编写了一个函数。

答案 1 :(得分:0)

您可以使用专注于此的https://github.com/sole/tween.js

一个非常基本的例子http://jsfiddle.net/qASPe(方格将在5s后飞向球体)主要是这段代码:

new TWEEN.Tween(ship.position)
    .to(planet.position, 700) // destination, duration 
    .start();

稍后,您可能希望使用THREE.Curve或其他路径机制作为“飞行”路径,例如此处http://jsfiddle.net/aevdJ/12

// create a path
var path = new THREE.SplineCurve3([
    ship.position,
    // some other points maybe? representing your landing/takeoff trajectory
    planet.position
]);

new TWEEN.Tween({ distance:0 })
    .to({ distance:1 }, 3000) // destination, duration 
    .onUpdate(function(){
        var pathPosition = path.getPointAt(this.distance);
        ship.position.set(pathPosition.x, pathPosition.y, pathPosition.z);
    })
    .start();

在所有情况下,请不要忘记在更新功能中添加此行

TWEEN.update();