disciple
disciple

Reputation: 262

How to find nearest points based on position?

I have a group of spheres which are arranged like this:

enter image description here

Each sphere is equidistant to each other. For this my code is:

geometry = new THREE.SphereGeometry(1.2);
material = new THREE.MeshPhongMaterial({
    color: 0xe0e0e0
})

for (var zPos = -120, i = 0; zPos <= 120; zPos += 7, i++) {

    arr[i] = [];

    for (var xPos = -120, j = 0; xPos <= 120; xPos += 7, j++) {

        arr[i][j] = "(" + i + "," + j + ")";

        sphere = new THREE.Mesh(geometry, material);
        sphere.position.set(xPos, 0, zPos);
        sphere.name = i + "-" + j;
        count++;
        sphere.receiveShadow = true;
        sphereMesh.add(sphere);
    }
    rows++;
}

scene.add(sphereMesh);

Here sphereMesh is a Group.

Now my problem is when I give a particular position as input the spheres which are near to the position with in some radius should get selected and they should change their color.

How to find the spheres near to that position given?

Upvotes: 0

Views: 63

Answers (1)

pietro909
pietro909

Reputation: 1861

just measure the distance between givenPoint and sphere and test it versus an arbitrary limit. Just an examples, assuming givenPoint being a THREE.Vector3 and sphere being a THREE.Mesh

var limit = 2;
var distance = Math.sqrt(Math.pow(sphere.position.x - givenPoint.x, 2) + Math.pow(sphere.position.y - givenPoint.y));
if (distance > limit)
{
   // is out
} 
else
{
   // is in
}

You can then add several breakpoint in order to say how much a sphere is near the givenPoint, or better normalize it and get a scale:

var normalized = 1 - distance / limit

Upvotes: 1

Related Questions