Dragonseer
Dragonseer

Reputation: 2874

Move Forward Relative to Object Rotation in 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.

Upvotes: 4

Views: 4147

Answers (1)

WestLangley
WestLangley

Reputation: 104763

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.

Upvotes: 4

Related Questions