【问题标题】:Get distance of two objects from camera three js从相机三js获取两个物体的距离
【发布时间】:2013-01-14 03:20:08
【问题描述】:

我在空间中有两个正方形,就像立方体的前后墙一样有顶点

x=-2 y=-1138 z=-2;
x=-2 y=-1134 z=-2;
x=2 y=-1138 z=-2;
x=2 y=-1134 z=-2

x=-2 y=1134 z=2;
x=-2 y=1138 z=2;
x=2 y=1134 z=2;
x=2 y=1138 z=2

当我像这样从相机计算 distanceTo 时

var point1 = this.camera.matrixWorld.getPosition().clone();
var point2 = this.mesh.cubePlane3.children[0].matrixWorld.getPosition().clone();
var distance = point1.distanceTo( point2 );  

我对 20,09 的距离总是相同的。这些方块在空间中旋转,所以只改变了旋转,我需要以某种方式找出哪个墙更靠近相机才能做一些事情,在立方体 3 中靠近相机的墙壁不会显示,接下来的 3 面墙会显示。

显然我不明白这背后的数学原理,例如为什么彼此相邻的墙壁具有 y 的正坐标和下一个负的 + 为什么距离是相同的值,当 z 轴上的一个比第二个更近时。你能请有人帮我怎样才能靠近墙壁吗?谢谢

【问题讨论】:

    标签: javascript position three.js


    【解决方案1】:

    每个几何图形都有一个 computeBoundingBox 函数。所以你可以这样做: var bbox = geometry.computeBoundingBox(); 对于您感兴趣的每个几何图形,然后使用 bbox.center() 获得几何图形的中心。更快的计算是在几何体上使用 computeBoundingSphere。 然后您只需将中心的关系与您的相机位置进行比较。

    【讨论】:

      【解决方案2】:

      我在这段代码中使用了 computeBoundingBox

          this.mesh[cubePlane[0]].children[0].geometry.computeBoundingBox();
      
          var position = new THREE.Vector3();
          position.sub( this.mesh[cubePlane[0]].children[0].geometry.boundingBox.max, this.mesh[cubePlane[0]].children[0].geometry.boundingBox.min );
          position.multiplyScalar( 0.5 );
          position.addSelf(this.mesh[cubePlane[0]].children[0].geometry.boundingBox.min );
      
          this.mesh[cubePlane[0]].children[0].matrixWorld.multiplyVector3( position );
      
          var point1 = this.camera.matrixWorld.getPosition().clone();
          var point2 = position;
          var distance = point1.distanceTo( point2 );
      

      它有效,所以谢谢你的建议:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-12
        • 2017-09-16
        • 1970-01-01
        • 1970-01-01
        • 2011-09-08
        • 1970-01-01
        • 2019-07-22
        • 1970-01-01
        相关资源
        最近更新 更多