How to move an object forward in Three.js?

Object3D has some handy methods for that.

object.translateZ( 10 );

The camera is a point in space. "Forward" is another point in space. so you can simply use the coordinates of a second point, and make the camera location closer to the "forward" location.

however, you may also need to turn left and right, which might involve polar coordinates.

adjust these values for your convenience:

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;

this function will initialize the scene:

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();

}

movement of the player is stopped when the player presses the up key.

function key_up(event){
    playerIsMovingForward = 0;
    playerIsMovingBackwards = 0;
    playerIsRotatingLeft = 0;
    playerIsRotatingRight = 0;
    playerGoesUp = 0;
    playerGoesDown = 0;
}

when the player is moving, we update the position:

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);

    }

}

We assume by forward you meant "using the WASD keys"

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;
    }


}

player will only move as fast as his browser. so maybe adjust this code?

function animate() {
    requestAnimationFrame( animate );

    updatePlayer();



    renderer.render( scene, camera );
}

this is the code that moves the camera into the position of the dVector object and also repositions the direction vector (dVector), so that it is always forward from the camera.

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 );    

}

moving forward usually involves turning left and right, here is some code that does that, it also uses polar coordinates, to move the point in relation to the camera (which is the center of the "circle" by a given amount of degrees (in radians)

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();

I hope that helps.


Please use above answer of @mrdoob, creator of ThreeJS:

object.translateZ( delta );

===OLD ANSWER===

A tutorial that worked for older ThreeJS version: 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;

More options:

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();

details posted here https://gamedev.stackexchange.com/questions/7490/translate-object-in-world-space-usings-its-local-rotation