【问题标题】:Rotate object around the world axis围绕世界轴旋转对象
【发布时间】:2015-07-23 09:23:39
【问题描述】:

我发现了几种不同的所谓工作方式来围绕世界轴旋转对象,但所有这些方式都保持围绕它自己的轴旋转对象而不是围绕世界旋转它。那么,围绕世界轴旋转对象的正确方法是什么?

如果我不清楚问题,这里有一张代表我的问题的图

编辑:对不起,我也应该在问题中说明我正在使用 Three.js

编辑2:

//camera is the object I want to rotate 
//I also found a method that uses Euler but the result was far from the expected

/**Method 1**/

//Found this somewhere here in so
var rotateAroundWorldAxis = function(object, axis, radians) {
  var rotWorldMatrix;
  rotWorldMatrix = new THREE.Matrix4();
  rotWorldMatrix.makeRotationAxis(axis.normalize(), radians);
  rotWorldMatrix.multiply(object.matrix);
  object.matrix = rotWorldMatrix;
  object.rotation.setFromRotationMatrix(object.matrix);
}

//this is how I call the method
rotateAroundWorldAxis(camera, new THREE.Vector3(0,1,0), movementX*-0.001);
rotateAroundWorldAxis(camera, new THREE.Vector3(1,0,0), movementY*-0.001);
/**Method 1**/


/**Method 2**/
//I've also tried this method
var q = new THREE.Quaternion();

q.setFromAxisAngle( new THREE.Vector3(0,1,0), movementX*-0.001 ); // axis must be normalized, angle in radians
camera.quaternion.multiplyQuaternions( q, camera.quaternion );
/**Method 2**/


//movementX and movementY are values provided by the mouse movement using the pointerlock. 
//These values works as I've tried them using rotateOnAxis

【问题讨论】:

  • 你有什么尝试吗?看过 CSS 旋转变换和变换原点属性吗?
  • 忘记在我使用 Three.js 的问题中提及
  • javascript 没关系.. 我建议尝试使用 css 中的 transform-origin 属性
  • 嗯,我不熟悉这个属性。我会阅读更多相关信息并尝试。
  • 我看不出这个属性有什么帮助,因为我不明白如何在 3D 世界中使用它。你能给我一些启示吗?

标签: javascript rotation three.js


【解决方案1】:

我最终使用了 FirstPersonControls 类。不过,我不得不对课程进行一些更改

【讨论】:

    【解决方案2】:

    使用四元数。

    var rotateAroundWorldAxis = function(object, axis, radians) {
      var rotation = new THREE.Quaternion();
      rotation.setFromAxisAngle ( axis, radians );
      object.quaternion.multiply(rotation);
    }
    
    rotateAroundWorldAxis(myObject, THREE.Vector3(0,1,0), movementX*-0.001);
    

    我相信你在使用四元数方法之前遇到的问题是以错误的顺序乘以四元数,所以你围绕全局 Y 轴旋转,然后执行局部旋转,最终在局部 Y 轴上旋转.

    【讨论】:

    • 在四元数方法中,我只做了 2 次旋转(X 轴和 Y 轴)。我将尝试弄乱 jsfiddle,看看是否可以重现我的问题。顺便说一句,我试过你的代码,但结果是一样的
    • 我无法在 jsfiddle 中重现该问题,因为出于某种原因,无论我做出何种更改,画布都没有更新,所以我制作了一个视频:youtube.com/watch?v=MTfBoR3LfqY&feature=youtu.be。在这个视频中,我将立方体向下旋转,然后向左旋转。对质量不好感到抱歉
    猜你喜欢
    • 1970-01-01
    • 2019-10-18
    • 2016-09-02
    • 2016-02-29
    • 2015-04-11
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多