Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving a object based on its rotation in three.js

I'm trying to move a cube in three.js based on its rotation but not sure on how to go about it.

As of now I can rotate the cube's z-rotation with the A & D keys. And with the W key I would like it to move forward relative to its rotation.

From 2D I would so something along the lines of:

float angle = GradToRad(obj.rotation);
obj.x = obj.x + cos(angle) * velocity;
obj.y = obj.y + sin(angle) * velocity;

Here's an image of the current implementation. 3D Cube three.js

How can I apply something similar in three.js?

like image 289
Placeable Avatar asked Apr 10 '13 13:04

Placeable


1 Answers

Objects can be considered to be facing their positive-Z axis. So to move an object forward, relative to it's own coordinate system, you can use

Object3D.translateZ( distance );

three.js r.57

like image 184
WestLangley Avatar answered Nov 01 '22 02:11

WestLangley