简体   繁体   English

Three.js - 计算相对旋转

[英]Three.js - calculating relative rotations

I have a THREE.Scene with two meshes added, meshA and meshB , each of which has been rotated in different ways.我有一个THREE.Scene添加了两个网格, meshAmeshB ,每个网格都以不同的方式旋转。 My goal is to remove meshB from the scene and re-add it as a child of meshA , while preserving its global position and rotation -- in other words, the position and rotation of meshB should appear the same before and after this code.我的目标是从场景中移除meshB并将其重新添加为meshA的子meshA ,同时保留其全局位置和旋转——换句话说,在此代码之前和之后, meshB的位置和旋转应该显示相同。

My current, almost working attempt is as follows:我目前的,几乎可以工作的尝试如下:

var globalOffset = new THREE.Vector3().subVectors( meshB.position, meshA.position );
var localOffset = meshA.worldToLocal( meshB.position );

var rotationOffset = meshA.quaternion.clone().inverse();
var rotation = meshB.quaternion.clone().multiply( rotationOffset );

scene.remove(meshB);
meshA.add(meshB);

meshB.position = localOffset;
meshB.rotation.setFromQuaternion(rotation);

The positioning works fine;定位工作正常; the rotation only works if both meshA and meshB have been rotated around the same axis -- if meshA and meshB have been rotated around different axes, meshB appears to change rotation before and after this code.只有当meshAmeshB都围绕同一轴旋转时,旋转才有效——如果meshAmeshB围绕不同的轴旋转, meshB在这段代码之前和之后似乎改变了旋转。

Any ideas how I can correct the code above (or an idea for a different approach) so that meshB still has the same "global" rotation before and after being removed and re-added to the scene?有什么想法可以更正上面的代码(或不同方法的想法),以便meshB在被删除并重新添加到场景之前和之后仍然具有相同的“全局”旋转?

Thanks!谢谢!

You want to remove meshB from the scene and re-add it as a child of meshA , while preserving its global position and rotation.您想从场景中移除meshB并将其重新添加为meshA的子meshA ,同时保留其全局位置和旋转。

You can do that like so:你可以这样做:

meshA.attach( meshB );

three.js r.109三.js r.109

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 three.js:2 x方向矢量到3 x旋转或矩阵 - three.js : 2 x Directions vectors to 3 x Rotations or Matrix 在THREE.JS中手动将旋转和平移应用于Matrix4 - Manually applying rotations and translations to Matrix4 in THREE.JS Three.js设备方向控件:限制或禁用XY旋转 - Three.js Device Orientation Controls: Limit or disable XY rotations jscript垃圾收集器有多快? Three.js矩阵旋转 - How fast is the jscript garbage collector? Three.js Matrix Rotations 如何将非 three.js 系统中的“YXZ”旋转范围从 -180 到 180 的欧拉角用于 three.js 系统 - How to use the euler angles with rotations in "YXZ" ranging from -180 to 180 in a non three.js system into a three.js system 计算骨骼以查看Three.js中的对象的矩阵 - Calculating matrix for bone to look at object in Three.js 在计算经过时间时Three.js不呈现 - Three.js doesn't render when calculating elapsed time Three.js如何获得一个对象相对于另一个对象的旋转? - Three.js How to get an object's rotation relative to another? 如何使用Three.js定义相对于矢量路径的粒子云? - How to define a cloud of particles relative to a vector path with Three.js? Three.js 相对于相机的统一虚线 - Three.js uniform dashed line relative to camera
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM