【发布时间】:2013-09-27 09:26:00
【问题描述】:
在 three.js 中,我在 xyz 有一艘太空船,我喜欢它飞向 xyz 的行星的网格物体。
我这辈子都想不通。
需要沿直线以恒定的速度向行星移动。
【问题讨论】:
标签: javascript geometry three.js
在 three.js 中,我在 xyz 有一艘太空船,我喜欢它飞向 xyz 的行星的网格物体。
我这辈子都想不通。
需要沿直线以恒定的速度向行星移动。
【问题讨论】:
标签: javascript geometry three.js
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 但不满意,所以我自己编写了一个函数。
【讨论】:
您可以使用专注于此的https://github.com/sole/tween.js。
一个非常基本的例子http://jsfiddle.net/qASPe(正方形会在5秒后飞向球体),主要是这段代码:
new TWEEN.Tween(ship.position)
.to(planet.position, 700) // destination, duration
.start();
稍后,您可能想要使用 THREE.Curve 或其他 Path 机制,作为“飞行”路径,如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();
【讨论】: