javascript - 三个js对象3D旋转

标签 javascript rotation three.js

喂! 我在三个 JS(r71)/THREEx (THREEx.LaserBeam) 中遇到了一个奇怪的问题。我在旋转 Object 3D 时遇到问题。

我正在计算纬度,姿态指向 phi,theta,如下所示: (或 50/-51 的任何其他变量)

var phi = (90 - 50) * Math.PI / 180;
var theta = (-51) * Math.PI / 180;

在此之后,我使用以下代码在该位置放置一个球体:

var geometry = new THREE.SphereGeometry( 0.005, 15, 15 );
var material = new THREE.MeshBasicMaterial( {color: 0x0000ff} );
var sphere = new THREE.Mesh( geometry, material );

scene.add( sphere );

sphere.position.x = 0.5 * Math.sin(phi) * Math.cos(theta);
sphere.position.y = 0.5 * Math.cos(phi);
sphere.position.z = 0.5 * Math.sin(phi) * Math.sin(theta);

然后我使用以下代码将我的光线旋转到相同的位置:

laserBeam.rotation.y = -theta
laserBeam.rotation.z = phi

laserBeam 在 Object3D 中实际上充当“线”。光线的原点位于 (0,0)。所以我一点也不知道为什么它没有穿过球体(参见 screenshot )。

有什么想法吗?

---编辑--- 或者这里是一个简单的例子

var phi = (90 - 50) * Math.PI / 180;
var theta = (-51) * Math.PI / 180;
var geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(0, 0, 0));
geometry.vertices.push(new THREE.Vector3(1 * Math.sin(phi) * Math.cos(theta), 1* Math.cos(phi), 1 * Math.sin(phi) * Math.sin(theta)));
var material = new THREE.LineBasicMaterial({
                                    color: 0x0000ff
                                });
var line = new THREE.Line(geometry, material);
containerLine   = new THREE.Object3D();
containerLine.add( line );
scene.add(containerLine);

最佳答案

您错误地计算了一个小半径和 y 坐标:

var rm = R * Math.cos(phi); // vs `R * Math.sin(phi)`
sphere.position.x = rm * Math.cos(theta);
sphere.position.y = R * Math.sin(phi); // vs `R * Math.cos(phi)`
sphere.position.z = rm * Math.sin(theta);

http://jsfiddle.net/sxen2kLd/

关于javascript - 三个js对象3D旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32200298/

相关文章:

Java、JPanel 旋转时裁剪图像

opencv - 如何找到两个二进制图像之间的旋转

javascript - Three.js 不带 translateZ 向前移动对象

javascript - 嵌套for循环中的变量定义?

javascript - 如何使用 jQuery 获取、解析和求和表的所有 tds'?

javascript - 反射型 XSS 保护 - 澄清吗?

java - 如何围绕另一个点旋转一个点

javascript - 如何实现Tab键作为控件而不跳转到地址栏

three.js - Threejs 与多种 Material 合并只应用一种 Material

javascript - meteor js未渲染的钩子(Hook)?