【问题标题】:THREE js rotationon one axes toward camera, not affected by parent三个 js 旋转一个轴朝向相机,不受父级影响
【发布时间】:2019-03-08 02:34:56
【问题描述】:

我有情况:

    var camera = new THREE.PerspectiveCamera(...);
        scene.add(camera);

    var parent = new THREE.Object3D();
        parent.position.set(1,2,3);
        scene.add(parent);

    var child = new THREE.Object3D();
        child.position.set(4,5,6);
        parent.add(child);


   var v = new THREE.Vector3().copy(child.position); 
       child.localToWorld(v);

   child.rotation.z = Math.atan2( ( v.x - camera.position.x   ), ( v.y - camera.position.y ) );

它可以工作,孩子正对着 Z 轴上的摄像头。

当我这样做时:

parent.rotation.set(1,2,3); // or whatever

子级受旋转影响,如何避免该旋转并保持子级在父级旋转之前保持相同的旋转(将局部旋转更新为通过父级转换在视觉上保持不变),但要保持全局位置因父级而改变旋转?

【问题讨论】:

标签: three.js


【解决方案1】:

设置子对象的旋转后请尝试以下操作:

var matrix = new Matrix4();
var quaternion = new THREE.Quaternion();

parent.updateMatrixWorld(); // ensure matrixWorld is up to date
matrix.extractRotation( parent.matrixWorld );
quaternion.setFromRotationMatrix( matrix );
child.quaternion.premultiply( quaternion.inverse() );

【讨论】:

  • 嗨,谢谢,我正在调用每帧中的旋转更新。似乎这不是计算最终角度,而是增加了现有角度。结果导致子对象围绕 Z 轴旋转(仅当父旋转不是 0,0,0 时)
  • 您能否以live example 证明您当前的代码?我帖子中的代码本质上是来自Object3D.lookAt() 的sn-p,它支持轮换父母。
猜你喜欢
  • 2019-01-10
  • 2015-03-18
  • 2013-01-22
  • 1970-01-01
  • 2016-03-19
  • 2015-06-17
  • 2012-01-06
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多