重新定位在原点处创建的对象以遵循向量的方向

时间:2014-09-03 17:22:52

标签: three.js

link to the jsfiddle of this question

如何重新定位在原点创建的对象以遵循向量的方向?

我的小提琴显示了一个均匀分布的精灵球体。然后我创建一个均匀分布的精灵圆盘(圆圈)。我的目标是将光盘定位在球体外部,匹配从原点生成的方向>球体物体。

我相信我的问题源于我在原点创建光盘精灵,然后尝试使用translateOnAxis重新定位。

相关代码。 disc_objects包含一些精灵; directionTHREE.Vector3,其来自原点>指向一个球体。我需要帮助将disc_object对齐到那个方向。

for (var i = 0, l = disc_objects.length; i < l; i++) {
    // Vogel's method of Fermat's spiral (Phyllotaxis)
    // http://blog.marmakoide.org/?p=1

    var theta = i * golden_angle;
    var r = Math.sqrt(i) / Math.sqrt(l);

    // all points are created around origin
    disc_objects[i].position.x = r * Math.cos(theta);
    disc_objects[i].position.y = r * Math.sin(theta);
    disc_objects[i].position.multiplyScalar(distance);

    // translate the arranged disc points to the random_point
    // adds disc twice as far away as ranom_point on sphere
    disc_objects[i].translateOnAxis(direction, distance * 2);  
    scene.add(disc_objects[i]);
}

1 个答案:

答案 0 :(得分:1)

好吧想通了。我没有意识到你可以创建Object3D对象作为父对象,然后添加子对象。所有的孩子都有与父母相关的出身。

parent = new THREE.Object3D();
parent.position.add(direction.clone().multiplyScalar(distance * 2));
parent.lookAt(vector);
scene.add(parent);

现在当我添加光盘点时,它们是相对于父级的,正确“查看”原点。