【问题标题】:three.js - how do I rotate a cylinder around a specific point?three.js - 如何围绕特定点旋转圆柱体?
【发布时间】:2012-09-26 14:17:44
【问题描述】:

我已经提到了以下关于对象旋转的问题。

但无法准确理解如何围绕特定点旋转圆柱体?

【问题讨论】:

    标签: three.js


    【解决方案1】:

    下面的函数可以用来实现这一点。请注意,几何图形被平移,但网格的位置没有改变。

    //rotates a mesh's geometry about a specified axis and pivot
    //the axis is a normalized Vector3
    const rotateAbout = (mesh, axis, axisPosition, angle) => {
        mesh.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(mesh.position.x-axisPosition.x, mesh.position.y-axisPosition.y, mesh.position.z-axisPosition.z));  //translate geometry to axis location
        mesh.geometry.applyMatrix(new THREE.Matrix4().makeRotationAxis(axis, angle));    //rotate geometry about axis
        mesh.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(axisPosition.x-mesh.position.x, axisPosition.y-mesh.position.y, axisPosition.z-mesh.position.z));  //translate geometry back to original location
    }
    

    【讨论】:

      【解决方案2】:

      我假设你的意思是你希望一个对象围绕一个特定的点旋转在它的几何结构内

      例如,cylinderGeometry 围绕它的中心旋转。假设你想让它围绕它的end旋转。

      您需要做的是在圆柱几何体创建后立即平移它,以便几何体中的所需点现在位于原点。

      geometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, cylinderHeight/2, 0 ) );
      

      编辑:您现在可以这样做:

      geometry.translate( 0, cylinderHeight/2, 0 ); // three.js r.72
      

      现在,当您旋转圆柱体时,它现在将围绕其末端旋转,而不是围绕其中间旋转。

      它旋转的一端也将位于您为圆柱体网格设置的位置。

      显然,您可以对任何几何体执行此操作,而不仅仅是圆柱体。

      【讨论】:

      • 谢谢,它有效。但是在管几何形状的情况下,我如何将轴助手放在管的边缘,或者换句话说,轴助手放在每个段点上,以便用户可以旋转或旋转管?请在link-@WestLangley 上查看我的另一个问题
      • 你能帮忙以这种方式旋转管几何吗?我想旋转管几何的特定部分。当我旋转时,整个管子都会旋转。
      • 是的,但该网站不接受我的任何新问题。-@WestLangley
      • 我之前已经发布过一个关于这个的问题。 rotate part of geometry
      • 如果你想每帧都做呢?在这种情况下转换几何体是没有意义的。 threejs.org/docs/#api/core/Geometry?
      【解决方案3】:

      为上面的WestLangley答案给出一个代码示例:

      // CYLINDER
      var cyl_material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
      var cyl_width = 1;
      var cyl_height = 5;
      // THREE.CylinderGeometry(bottomRadius, topRadius, height, segmentsRadius, segmentsHeight, openEnded )
      var cylGeometry = new THREE.CylinderGeometry(cyl_width, cyl_width, cyl_height, 20, 1, false);
      // translate the cylinder geometry so that the desired point within the geometry is now at the origin
      cylGeometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, cyl_height/2, 0 ) );
      var cylinder = new THREE.Mesh(cylGeometry, cyl_material);
      
      scene.add( cylinder );    
      

      现在旋转围绕圆柱体原点进行:

      cylinder.rotation.x = 0.5*Math.PI;
      

      希望对您有所帮助。

      【讨论】:

      • 谢谢你 - 你帮了我很多!
      猜你喜欢
      • 2019-08-05
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      相关资源
      最近更新 更多