Jared
Jared

Reputation: 607

reposition objects created at the origin to follow a direction of a vector

link to the jsfiddle of this question

How can I reposition objects created at the origin to follow a direction of a vector?

My fiddle shows a sphere with evenly distributed sprites. I then create a disc (circle) of evenly distributed sprites. My goal is to position the disc outside the sphere, matching the direction generated from origin > sphere object.

I believe my problem stems from the fact that I create the disc sprites at the origin, then try to reposition later by using translateOnAxis.

pertinent code. disc_objects contains a few sprites; direction is a THREE.Vector3 with direction from origin > point on a sphere. I need help aligning the disc_objects to that direction.

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]);
}

Upvotes: 0

Views: 233

Answers (1)

Jared
Jared

Reputation: 607

ok figured it out. I didn't realize you could create Object3D objects as parents, then add children. All children have their origin relative to the parents.

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

now when I add the disc points, they are relative to the parent, correctly "looking at" the origin.

Upvotes: 1

Related Questions