javascript - Three.js Move object forward without translateZ - Stack Overflow

Like in lots of other similar questions, I want to move an object forward in three.js based on its rota

Like in lots of other similar questions, I want to move an object forward in three.js based on its rotation. The difference is, I can't use object.translateZ. This is because I'm using a physics simulator and it will mess up if I update the object like that. Here is an example fiddle of my problem. Feel free to play around with my fiddle and add your updated version to your answer.

I would like the cube to move forward by updating mesh.position.x and mesh.position.z based on mesh.rotation.y.

To keep in mind, I've sometimes realized that three.js rotations are a little weird some times when accessing them using mesh.rotation.y.

Thanks!

Like in lots of other similar questions, I want to move an object forward in three.js based on its rotation. The difference is, I can't use object.translateZ. This is because I'm using a physics simulator and it will mess up if I update the object like that. Here is an example fiddle of my problem. Feel free to play around with my fiddle and add your updated version to your answer.

I would like the cube to move forward by updating mesh.position.x and mesh.position.z based on mesh.rotation.y.

To keep in mind, I've sometimes realized that three.js rotations are a little weird some times when accessing them using mesh.rotation.y.

Thanks!

Share Improve this question edited May 23, 2017 at 10:29 CommunityBot 11 silver badge asked Jan 16, 2017 at 5:58 Griffin M.Griffin M. 1961 silver badge17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

I would suggest using the mesh.getWorldDirection() to get a direction where you want to move your mesh. Then you can multiply it with a scalar to adjust movement speed.

direction = mesh.getWorldDirection();

mesh.position.add(direction.multiplyScalar(moveSpeed));

Fiddle: https://jsfiddle/k1swrxjr/1/

Good luck!

To move your mesh simply increment the value of the position in which you want your mesh to go in you animate() function instead of init function, example as follows:

function animate() {
        mesh.position.x += 0.1;
        requestAnimationFrame( animate );
        renderer.render( scene, camera );
    }

This example code will increase x position of your mesh by 0.1 every time animate() is run.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745649408a4638167.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信