给定经度和纬度位置,我如何旋转一个对象,使其远离球体

How do i rotate an object so that it faces away from a sphere given its position in longitude and latitude

我有一个球体和盒子,上面应该是建筑物。我想旋转盒子(围绕 x y 和 z 轴),使它们背对球体,就像普通建筑物在给定盒子的经度和纬度(也是 x、y、z 位置)时一样。

我希望你能帮助我解决我的问题,任何帮助将不胜感激:)

我尝试了很多使用经度和纬度的旋转,但 none 奏效了。 我也想过用球体中心到建筑物位置的矢量来求出旋转,但没能想出解决办法。

  var lat = i; //-90 to 90
  var lon = j; //-180 to 180


  var height = 10;
  var width = 10;
  var length = 10;

  var xyz = getXYZ(lat, lon, sphereradius, height);

  var boxE3 = document.createElement('a-box');

  boxE3.setAttribute('material', {
    color: getRandomColor()
  });

  boxE3.setAttribute('rotation', {
    x: 0, 
    y: lat, //Rotation that needs to be correctly set.
    z: lon
  });

  boxE3.setAttribute('position', {
    x: xyz[0],
    y: xyz[1],
    z: xyz[2]
  });

  boxE3.setAttribute('scale', {
    x: length,
    y: height,
    z: width
  });
  sceneEl.appendChild(boxE3);
}

实际结果:

想要的结果:

旋转一个对象以背对另一个对象。

我会重用 look-at 组件逻辑,其中 "looking at" 是用一个简单的方法制作的:

object3D.lookAt(THREE.Vector3())

有一个盒子和一个球体,只需将盒子 lookAt 变成球体,然后旋转它。

box.object3D.lookAt(spherePosition)
box.rotation.y += Math.Pi

this fiddle中查看。但我会做一些不同的事情。

围绕球体旋转参考系

假设我们有以下设置。

<a-sphere id="earth" position="1 1 -2" radius="1.5"></a-sphere>
<a-entity id="frameOfReference" position="1 1 -2>
   <a-box position="0 0 -1.5></a-box>
</a-entity>

现在,行星和框架处于同一位置。框位置对应于行星半径。无论我们如何旋转参考系,它实际上都背对着地球。

为什么有用?因为现在您可以使用纬度和经度旋转参考系:

    let x = latitude
    let y = longitude + 180 // 180deg shift because z is negative
    frame.setAttribute("rotation", x + " " + y + " " + 0)

this fiddle, where the ISS is tracked using this API中查看。