我有一个球体和盒子,它们应该是建筑物。我想旋转长方体(围绕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);
}实际结果:

想要的结果:

发布于 2019-05-22 18:04:49
旋转一个对象使其背对另一个对象。
我会重用look-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 API追踪国际空间站的this fiddle上查看它。
https://stackoverflow.com/questions/56238690
复制相似问题