Working with Sphere

:thinking: With the method I shared above it should always find the closest relative point, that being said, that’s not to account for an exact center or precisely half way between points, demo… (EDIT: increased the sphere geo to 30 x 30 in the demo, still works as expected…)

1 Like

There is no closest point, in both cases I described you will get infinitely many points. However if the reference point is outside the sphere then yeah you will get one point.

Edit,

Maybe I’m wrong I need to sketch this out

Edit2

Yeah I was wrong lol, there will be one closest point inside too. Only when you’re in the center it’s infinite

@dubois so after you’re edit do you know how I could get the closest point?

@dubois @Lawrence3DPK @manthrax I was wondering if I could get the intersection point using raycaster?

Yes this is also an option, the direction of the ray would be…

dir = new THREE.Vector3(0,0,0).copy(speaker.position).sub(sphere.position).normalize()

raycaster.set(sphere.position, dir) 

speaker.position.copy(intersects.point)
1 Like

@Lawrence3DPK
I’ve tried the following

  constructor ( speakerTriplets:Speaker[][], sphere:THREE.Mesh ) {

    this.triplets = speakerTriplets.map( triplet => {

      return ( triplet.map( speaker => {

        const raycaster = new THREE.Raycaster();

        const dir = new THREE.Vector3( 0, 0, 0 ).copy( speaker.object.position ).sub( sphere.position ).normalize();

        raycaster.set( sphere.position, dir );

        speaker.object.position.copy( raycaster.intersectObject( sphere )[ 0 ].point )

        return [ speaker.object.position.x, speaker.object.position.y, speaker.object.position.z ]


      } ))

    })

  }

but I get

TypeError: Cannot read properties of undefined (reading ‘point’)

any idea?

Try this…

constructor ( speakerTriplets:Speaker[][], sphere:THREE.Mesh ) {

    const raycaster = new THREE.Raycaster();
    const dir = new THREE.Vector3( 0, 0, 0 )
    let intersects:
    this.triplets = speakerTriplets.map( triplet => {

      return ( triplet.map( speaker => {

        dir.copy( speaker.object.position ).sub( sphere.position ).normalize();

        raycaster.set( sphere.position, dir );
        intersects = raycaster.intersectObject(sphere)
        speaker.object.position.copy( intersects[0].point )

        return [ speaker.object.position.x, speaker.object.position.y, speaker.object.position.z ]


      } ))

    })

  }

I had to correct your code to

constructor ( speakerTriplets:Speaker, sphere:THREE.Mesh ) {

this.triplets = speakerTriplets.map( triplet => {

  return ( triplet.map( speaker => {

    const raycaster = new THREE.Raycaster();

    const dir = new THREE.Vector3( 0, 0, 0 ).copy( speaker.object.position ).sub( sphere.position ).normalize();

    raycaster.set( sphere.position, dir );
    let intersects = raycaster.intersectObject(sphere)
    speaker.object.position.copy( intersects[ 0 ].point )

    return [ speaker.object.position.x, speaker.object.position.y, speaker.object.position.z ]


  } ))

})

}

but I get the same error. Any ideas? Thanks in advance

Good catch updated the code above… Ah right, because you’re shooting a ray from inside to out I think you may have to set your sphere material to THREE.Backside or THREE.DoubleSidefor the ray to detect the surface.

2 Likes

@Lawrence3DPK @dubois @manthrax Thanks a lot for all of your time it finally worked

2 Likes