围绕世界轴旋转对象(Rotate object around world axis)

编程入门 行业动态 更新时间:2024-10-26 20:33:33
围绕世界轴旋转对象(Rotate object around world axis)

我正在尝试围绕世界轴旋转一个对象。 我发现了这个问题: 如何在轴世界three.js上旋转一个对象?

但是它并没有通过使用这个函数来解决问题:

var rotWorldMatrix; // Rotate an object around an arbitrary axis in world space function rotateAroundWorldAxis(object, axis, radians) { rotWorldMatrix = new THREE.Matrix4(); rotWorldMatrix.makeRotationAxis(axis.normalize(), radians); rotWorldMatrix.multiplySelf(object.matrix); // pre-multiply object.matrix = rotWorldMatrix; object.rotation.getRotationFromMatrix(object.matrix, object.scale); }

multiplySelf和getRotationFromMatrix没有定义(我收到控制台错误)。 如何解决这个问题?

更新

我试图使用Quaternion ,但它似乎不正确。 我试图根据用户点击旋转一个对象,这是我写的功能:

function mouseUp(event) { var x= event.clientX; var y= event.clientY; var dx= (x - xBegin); var dy= (y - yBegin); var quaternion= new THREE.Quaternion(); quaternion.setFromAxisAngle(new THREE.Quaternion(dy,dx,0).normalize(),Math.sqrt(dx*dx+dy*dy)/250.0); object.quaternion.multiplyQuaternions(quaternion,object.quaternion); }

只要物体处于垂直或水平位置,它就会正确旋转,但是如果它与x轴成45度角,它会非常缓慢地旋转并沿着与咔哒声相反的方向旋转。

I am trying to rotate an object around the world axis. I've found this question: How to rotate a object on axis world three.js?

But it didn't solve the problem by using this function:

var rotWorldMatrix; // Rotate an object around an arbitrary axis in world space function rotateAroundWorldAxis(object, axis, radians) { rotWorldMatrix = new THREE.Matrix4(); rotWorldMatrix.makeRotationAxis(axis.normalize(), radians); rotWorldMatrix.multiplySelf(object.matrix); // pre-multiply object.matrix = rotWorldMatrix; object.rotation.getRotationFromMatrix(object.matrix, object.scale); }

multiplySelf and getRotationFromMatrix are not defined (I'm getting a console error). How to fix the problem?

Update

I tried to use Quaternion, but it doesn't seem to behave correctly. I'm trying to rotate an object according to the user click, this is the function that I've written:

function mouseUp(event) { var x= event.clientX; var y= event.clientY; var dx= (x - xBegin); var dy= (y - yBegin); var quaternion= new THREE.Quaternion(); quaternion.setFromAxisAngle(new THREE.Quaternion(dy,dx,0).normalize(),Math.sqrt(dx*dx+dy*dy)/250.0); object.quaternion.multiplyQuaternions(quaternion,object.quaternion); }

It rotates correctly as long as the object is in vertical or horizontal position, but if it's for example at 45 degrees from the x axis, it rotates very slowly and in the opposite direction of the click.

最满意答案

只要对象没有旋转的父对象,就可以围绕世界坐标轴旋转对象,如下所示:

object.rotateOnWorldAxis( axis, angle );

axis必须标准化(有单位长度); angle以弧度表示。

三.js r.92

As long as the object does not have a rotated parent, you can rotate the object around a world axis like so:

object.rotateOnWorldAxis( axis, angle );

axis must be normalized (have unit length); angle is in radians.

three.js r.92

更多推荐

本文发布于:2023-07-15 21:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119298.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   世界   Rotate   axis   world

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!