Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move Forward Relative to Object Rotation in Three.js

Tags:

three.js

I've just begun looking at Three.js and have a question regarding object movement. In XNA/ShparDX you can do something like the code below, where the object can be moved forward relative to it's orientation. So if the object is looking down, this will move it down. If it is looking to the right, it would move to the right. A video example of this can be seen here, and is the last video on the page.

float speed = 0.15f;
Vector3 velocity = objectMatrix.Forward * speed;
objectMatrix *= Matrix.CreateTranslation(velocity);

I have tried the following, based on this answer. It simply did nothing when I pressed the key to trigger it.

var STEP = 10;
object.matrixWorld.identity();
object.matrixWorld.multiplySelf(three.Matrix4.translationMatrix(object.position.x, object.position.y, object.position.z + STEP));
object.updateMatrixWorld();

I have been unsucessful in replicating this using the members and functions available in Three.js. Can someone show me if it is possible? Thanks.

like image 897
Dragonseer Avatar asked Oct 24 '25 19:10

Dragonseer


1 Answers

mesh.translateX( delta );
mesh.translateY( delta );
mesh.translateZ( delta );

To move forward, use translateZ( delta ).

If you are just learning, it is best not to mess with object.matrix, unless you really know what you are doing. Set the position, rotation, and scale instead.

like image 142
WestLangley Avatar answered Oct 26 '25 20:10

WestLangley