Three js 3D rotation

限于喜欢 提交于 2021-02-07 22:38:49

问题


I have 2 mesh objects. Anchor and Rod. The Anchor rotates around z-axis, as illustrated by the image. The rod is supposed to move only backward and forwards. Here is the image: http://imageshack.us/photo/my-images/841/7a83.png/.

But i am stuck with trying to figure out the matrix calculation. For example if the anchor rotates 45degrees, so its facing the x-axis, how can i make the rod still move backwards and forwards?


回答1:


For the rotation around z axis :

var rotation = new THREE.Matrix4().makeRotationZ(Math.PI/2);

For the translation, where z is your delta :

var translation = new THREE.Matrix4().makeTranslation(0, 0, z);

You can combine the two transformations in beginning with the translation :

var transformation = new THREE.Matrix4().makeTranslation(0, 0, z).makeRotationZ(Math.PI/2);

var transformation = rotation.multiply(translation);

Then apply transformation to your geometry :

geometry.applyMatrix(transformation);



回答2:


Just as scooby37 noticed, combination example provided by Troopers is not valid. You should NOT:

new THREE.Matrix4().makeTranslation(0, 0, z).makeRotationZ(Math.PI/2);

Instead you can try something like:

let rotation = new THREE.Matrix4().makeRotationAxis(new THREE.Vector3(0, 1, 0), Math.PI / 6.0);
let transformation = new THREE.Matrix4().makeTranslation(0, this.height, 0);
this.mesh.applyMatrix(rotation.multiply(transformation));

which introduce matrix multiplication - usual way transformations should be combined.



来源:https://stackoverflow.com/questions/19729486/three-js-3d-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!