Line from one sphere to another

Hey guys,

So I have a series of spheres inside of each other. Each sphere has a cluster of smaller spheres on them which are all connected with lines. On a per sphere basis, I have that working.

What I am trying to do now, is have each main sphere connect with all of the main spheres. So I take the last smaller sphere from each main spheres small spheres clusters and cache those positions. Then I step over all of my main spheres and create lines from big sphere 1 cluster to big sphere 2 cluster, big sphere 2 cluster to big sphere 3 cluster, etc etc.

It looks like it is creating the lines, but the problem is they aren’t pointing towards the next sphere, they are flat with the sphere where they are originating from. I’m guessing I have to tell the line to look at the next sphere but was having issues and thought I’d make a post.

for(let i = 0; i < sphereLayers.length - 1; i++){

            const lineMat = new THREE.LineBasicMaterial({color: sphereLayers[i].GetCluster().color, transparent: true, opacity: 1 });

            const linePoints = [];

            linePoints.push(sphereLayers[i].GetLinkerNode().position);

            linePoints.push(sphereLayers[i + 1].GetLinkerNode().position);

            const lineGeo = new THREE.BufferGeometry().setFromPoints(linePoints);

            this.line = new THREE.Line(lineGeo, lineMat);

            sphereLayers[i].GetHexSphere().add(this.line);

        }

Thanks guys!

object.position is a local position (ie. position relative to the parent, relative to its parent and so on.)
object.getWorldPosition calculates the world position (which you likely need, since it’s the actual position of object in the 3D space.)

Take a look here (and also remember to add the drawn lines to the Scene, not to the spheres, otherwise the line also inherits the transformation of the sphere and it’ll still be a bit of a mess.)

1 Like

Awesome! and Doh! lol .getWorldPosition is exactly what I was looking for!

Now I just have to compensate for each large spheres rotation. I am getting the line segments I need at least, they just don’t visibly align since all spheres have varying rotation. Ideally I need to glue the sphere to sphere line connections to each other no matter what.

EDIT: I got it! It was a race condition with where I was creating the line and expecting the sphere / cluster spheres information to be available and it was not! Got it working as expected! Thanks again! :slight_smile: