Moving an object by its rotation in three.js

I am trying to move a cube to three.js based on its rotation, but not sure how to do it.

Currently, I can rotate the rotation of the cube z with the keys A and D. And with the key W, I would like it to move forward relative to its rotation.

From 2D, I would do something like strings:

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

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

How can I apply something similar in three.js?

+5
source share
3 answers

It is believed that objects can collide with their positive Z axis. To move an object forward, relative to its own coordinate system, you can use

Object3D.translateZ( distance );

three.js r.57

+7
source

, ( ) 4 × 4 . Object3D.matrix three.js , matrixAutoUpdate false, . translate .

0

Your 2D method is exactly how I did it in three.js. For position Y, I use the terrain collision method (which still needs work);

0
source

All Articles