有没有办法在Three.js中移动一个对象?
也许我应该将rotation.x,y,z转换为向量,并处理它。但我是初学者,我不知道该怎么做。
答案 0 :(得分:46)
Object3D有一些方便的方法。
object.translateZ( 10 );
答案 1 :(得分:7)
请使用ThreeJS的创建者@mrdoob的上述答案:
object.translateZ( delta );
=== OLD ANSWER ===
适用于旧版ThreeJS的教程: http://www.aerotwist.com/tutorials/getting-started-with-three-js/
// set position of YOUR_OBJECT
YOUR_OBJECT.position.x = 10;
YOUR_OBJECT.position.y = 50;
YOUR_OBJECT.position.z = 130;
更多选项:
var STEP = 10;
var newCubeMatrix = cube.matrix;
newCubeMatrix.identity();
//newCubeMatrix.multiplySelf(THREE.Matrix4.rotationYMatrix(cube.rotation.y));
newCubeMatrix.multiplySelf(THREE.Matrix4.translationMatrix(cube.position.x, cube.position.y, cube.position.z + STEP));
cube.updateMatrix();
答案 2 :(得分:2)
相机是空间中的一个点。 “前进”是空间的另一点。 因此您只需使用第二个点的坐标,并使相机位置更靠近“前进”位置即可。
但是,您可能还需要向左和向右转,这可能涉及极坐标。
为方便起见调整这些值:
var scene;
var camera;
var playerDirection = 0;//angles 0 - 2pi
var dVector;
var angularSpeed = 0.01;
var playerSpeed = 0.075;
var playerBackwardsSpeed = playerSpeed * 0.4;
此函数将初始化场景:
function init(){
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
camera.position.z = 5;
dVector = new THREE.Vector3( 0, 0, 0 ) ;
camera.lookAt( dVector );
animate();
}
当播放器按向上键时,播放器的运动停止。
function key_up(event){
playerIsMovingForward = 0;
playerIsMovingBackwards = 0;
playerIsRotatingLeft = 0;
playerIsRotatingRight = 0;
playerGoesUp = 0;
playerGoesDown = 0;
}
玩家移动时,我们会更新位置:
function updatePlayer(){
if(playerIsRotatingLeft){ // rotate left
playerDirection -= angularSpeed;
}
if(playerIsRotatingRight){ // rotate right
playerDirection += angularSpeed;
}
if(playerIsRotatingRight || playerIsRotatingLeft){
setPlayerDirection();
}
if(playerIsMovingForward){ // go forward
moveForward(playerSpeed);
}
if(playerIsMovingBackwards){ // go backwards
moveForward(-playerBackwardsSpeed);
}
}
我们假设您的意思是“使用WASD密钥”
function key_down(event){
var W = 87;
var S = 83;
var A = 65;
var D = 68;
var minus = 189;
var plus = 187;
var k = event.keyCode;
console.log(k);
if(k == A){ // rotate left
playerIsRotatingLeft = 1;
}
if(k == D){ // rotate right
playerIsRotatingRight = 1;
}
if(k == W){ // go forward
playerIsMovingForward = 1;
}
if(k == S){ // go back
playerIsMovingBackwards = 1;
}
}
播放器的移动速度仅与浏览器一样快。 所以也许调整一下代码?
function animate() {
requestAnimationFrame( animate );
updatePlayer();
renderer.render( scene, camera );
}
这是将摄像机移到dVector对象位置的代码 并重新定位方向矢量(dVector),以使它始终从摄像机向前。
function moveForward(speed){
var delta_x = speed * Math.cos(playerDirection);
var delta_z = speed * Math.sin(playerDirection);
var new_x = camera.position.x + delta_x;
var new_z = camera.position.z + delta_z;
camera.position.x = new_x;
camera.position.z = new_z;
var new_dx = dVector.x + delta_x;
var new_dz = dVector.z + delta_z;
dVector.x = new_dx;
dVector.z = new_dz;
camera.lookAt( dVector );
}
向前移动通常涉及向左和向右旋转,这是一些执行此操作的代码,它也使用极坐标来相对于摄影机(即“圆”的中心)以给定的量移动点。度(弧度)
function setPlayerDirection(){
//direction changed.
var delta_x = playerSpeed * Math.cos(playerDirection);
var delta_z = playerSpeed * Math.sin(playerDirection);
var new_dx = camera.position.x + delta_x;
var new_dz = camera.position.z + delta_z;
dVector.x = new_dx;
dVector.z = new_dz;
console.log(dVector);
camera.lookAt( dVector );
}
animate();
我希望有帮助。
答案 3 :(得分:-2)
另一种选择是使用Vector3的set方法/函数。
:focus::-webkit-input-placeholder {
opacity: 0;
transform: translate(70%);
-webkit-transition: all 0.35s ease-in-out;
transition: all 0.35s ease-in-out;
}