1

I am trying to add a number of spheres in the following example. Initially it had only three cubes, but I need to add some 10 spheres that would be equidistant from each other and would be rotating in different speeds.

My Try

var parent, renderer, scene, camera, controls;

init();
animate();

function init()
{
    // renderer
    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    // scene
    scene = new THREE.Scene();

    // camera
    camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100);
    camera.position.set(20, 20, 20);

    // controls
    controls = new THREE.OrbitControls(camera);
    controls.minDistance = 10;
    controls.maxDistance = 50;

    // axes
    scene.add(new THREE.AxisHelper(20));

    // geometry
    var geometry = new THREE.SphereGeometry(0.3, 50, 50, 0, Math.PI * 2, 0, Math.PI * 2)
    // material
    var material = new THREE.MeshBasicMaterial({
        color: 0xffffff,
        wireframe: true
    });

    // parent
    parent = new THREE.Object3D();
    scene.add(parent);

    // pivots
    var pivot1 = new THREE.Object3D();
    var pivot2 = new THREE.Object3D();
    var pivot3 = new THREE.Object3D();
    var pivot4 = new THREE.Object3D();
    pivot1.rotation.z = 0;
    pivot2.rotation.z = 2 * Math.PI / 3;
    pivot3.rotation.z = 4 * Math.PI / 3;
    pivot4.rotation.z = 6 * Math.PI / 3;
    parent.add(pivot1);
    parent.add(pivot2);
    parent.add(pivot3);
    parent.add(pivot4);

    // mesh
    var mesh1 = new THREE.Mesh(geometry, material);
    var mesh2 = new THREE.Mesh(geometry, material);
    var mesh3 = new THREE.Mesh(geometry, material);
    var mesh4 = new THREE.Mesh(geometry, material);
    mesh1.position.y = 5;
    mesh2.position.y = 5;
    mesh3.position.y = 5;
    mesh4.position.y = 5;
    pivot1.add(mesh1);
    pivot2.add(mesh2);
    pivot3.add(mesh3);
    pivot4.add(mesh4);

}

function animate()
{
    requestAnimationFrame(animate);

    parent.rotation.z += 0.01;

    controls.update();

    renderer.render(scene, camera);
}

Why am I not able to add more than 3 spheres into the scene? I tried to add the fourth sphere but it did not work. How can speed be accounted for here? That is: can I specify different speeds for some spheres?

1 Answer 1

1

Missing 4th Sphere

You specify:

pivot1.rotation.z = 0;
pivot2.rotation.z = 2 * Math.PI / 3;
pivot3.rotation.z = 4 * Math.PI / 3;
pivot4.rotation.z = 6 * Math.PI / 3;

6 * Math.PI / 3 = 2 * Math.PI

Note, three.js uses radians, therefore 2 * PI is 0 (a full revolution is the same place as no rotation.

So pivot1 and pivot4 have the same effective rotation and your 2 sphere end up in the same place in space.

Speed

You currently handle speed by mutating the z rotation on every frame.

parent.rotation.z += 0.01;

This obviously works just fine for a demo. You can speed it up by moving more per frame (or getting more frames, ie better machine or other upgrades)

parent.rotation.z += 0.04;

Now it rotates at 4 times the speed!

More Spheres

Once you get past working with counts larger than your number of fingers on a hand, I recommend getting generic with arrays. Instead of listing out pivot1, pivot2, pivot3, . . . pivot0451, generate this with a loop. (Functionally you could use ranges if you prefer).

First, we declare how many spheres to make. Then divide up the circle (2 * Math.PI radians to go around). Then for ever sphere, make a pivot. Then, for every pivot, add a mesh. And you're done.

var numberOfSpheres = 10;
var radiansPerSphere = 2 * Math.PI / numberOfSpheres;
// pivots
var pivots = [];
for (var i = 0; i < numberOfSpheres; i++) {
  var pivot = new THREE.Object3D();
  pivot.rotation.z = i * radiansPerSphere;
  parent.add(pivot);
  pivots.push(pivot);
}

var meshes = pivots.map((pivot) => {
  var mesh = new THREE.Mesh(geometry, material);
  mesh.position.y = 5;
  pivot.add(mesh)
  return mesh;
});

I implemented this at this codepen.io

Happy coding.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible that different spheres have their own orbits just like different planets have their own orbits.what change I would require here so that all the 10 sphere revolve around their own oribts as right now they are following each other?
Some crazy pattern like this? They do have their own orbits in the first example, they're just all very similar. Recall you have 3 layers deep, the parent, which controls all of them, each have a pivot, which you use to control rotation from the parent, but they are all in the same place in space, then the spheres, which are places in the +y direction away from each pivot. I'm unclear on exactly what you mean by 'their own oribits', but if you can think of it, you can render it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.