我有一个球体和盒子,上面应该是建筑物。我想旋转盒子(围绕 x y 和 z 轴),以便它们背对球体,就像普通建筑物在给定盒子的经度和纬度(以及 x、y、z 位置)时一样。
我希望你能帮助我解决我的问题,任何帮助将不胜感激:)
我尝试了很多使用经度和纬度的旋转,但都没有用。 我也想过使用从球体中心到建筑物位置的矢量来找出旋转但无法提出解决方案。
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 中查看,其中使用 this API 跟踪 ISS .
关于javascript - 给定经度和纬度位置,我如何旋转一个对象,使其远离球体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56238690/