What is the best way to locate the closest points using their positions?

I have a cluster of orbs arranged in the following pattern:

https://i.sstatic.net/9FhsQ.png

Each orb is evenly spaced from one another. The code I am using for this setup 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);

In this case, sphereMesh acts as a Group.

The issue I am facing is that when I provide a specific position as input, I want to select the orbs nearby within a certain radius and change their color. How can I identify the orbs close to the given position?

Answer №1

To determine the proximity of a givenPoint to a sphere, simply measure the distance between them and compare it to a set limit. Here is an example, where givenPoint is a THREE.Vector3 and sphere is 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)
{
   // sphere is outside the limit
} 
else
{
   // sphere is within the limit
}

You can then further analyze the distance to determine how close the sphere is to the givenPoint. To do this, you can normalize the distance to get a scale:

var normalized = 1 - distance / limit

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customizing the Zoom Control Style in Vue Leaflet

I'm currently working on developing a Map App in Dark Mode using Vue, but I've encountered an issue with changing the style of the Zoom Control. Here's what I have tried so far: template> <div class="main-map"> <l ...

Exploring ElectronJs: The journey to sending messages from ipcMain to IpcRender and awaiting a response

I need help with sending a message to ask the renderer to parse an HTML string mainWindow.webContents.send('parse html', { resp}) The renderer processes the data and sends a reply ipc.on('parse html',function(e,p){ let b ...

Comparing Three.js BinaryLoader and BufferGeometry for handling heavy meshes: what's the best approach?

I am seeking the most efficient method for loading large polygonal meshes (with up to 1 million triangles) into Three.js. I am debating between using a THREE.BufferGeometry object or utilizing THREE.BinaryLoader. Some questions that arise are: Does Binar ...

What is the process for redirecting to an external URL while including post parameters?

When a user accesses my endpoint, I need it to automatically redirect them to an external URL with URL-encoded form parameters. Endpoint: https://example.com Form Parameters: Name: testing ID: 123456 I have attempted the following in Express. It succes ...

Experiencing the issue of receiving `undefined` when trying to access res.query with a JSON query string using

Utilizing qs for constructing JSON-based query parameters for my REST API request: On the client side: import qs from "qs"; let query = { dateTime: { $gte: 1664557995000 } }; let q = qs.stringify(query); let url = "http://lo ...

Error encountered with MobileFirst version 8 and Angular JS v1.5.3 integration using Bootstrap

I am encountering an issue with my Cordova application that integrates with MobileFirst Platform version 8, Ionic version 1.3.1, and AngularJS version 1.5.3. Upon bootstrapping AngularJS to connect the app to MobileFirst Platform, I encounter the following ...

Is it possible to conceal the dates from past months in the datepicker plugin?

Currently, I am utilizing a datepicker tool from jQuery UI and adjusting its CSS to suit my requirements. My goal now is to hide any dates that are not currently active, specifically those displayed from other months. I am unsure if this can be achieved ...

Can these two arrays be merged together in JavaScript?

Here is the structure of the first array: [ 0:{program: id_1}, 1: {program: id_2} ] Now, let's take a look at the second array: [ 0: [ 0: {lesson: name_1}, 1: {lesson: name_2} ], 1: [ 0: {lesson: name_3} ] ] I am attempti ...

What is the best way to send the link's ID to an AngularJS controller?

I have a scenario similar to the following: <a href="#" title="Post 1" id="car1"> Audi car </a> <a href="#" title="Post 1" id="car2"> BMW car </a> How can I pass the ID to the controller? I attempted the following: <a href= ...

The functionality of the onclick button input is not functioning properly in the Google

Here is some JavaScript code: function doClick() { alert("clicked"); } Now, take a look at this HTML code: <div > <table border="2" cellspacing="0" cellpadding="0" class="TextFG" style="font-family:Arial; font-size:10pt;"> <tr> <t ...

Can Javascript be used to open a new window, send POST parameters to it, and pass the request as application/json?

I have a browser client application built with JavaScript that manages a large amount of state data. Sometimes, this data needs to be organized into a structured object and sent to a new window for further processing. Currently, I am using a solution wher ...

Experience the classic game of rock-paper-scissors brought to life through JavaScript and HTML,

I've been working on a rock, paper, scissors game in JavaScript and I'm struggling to get the wins and losses to register correctly. The game keeps resulting in a draw even though I've checked my code multiple times. It's frustrating be ...

Loading WordPress posts using Ajax

Struggling with getting an Ajax post loader to work properly. Following the jQuery code from a previous StackOverflow post titled "Load More Posts" with Ajax in WordPress, but still facing issues. Trying to implement an isotope list for ajax loading more p ...

Storing an image in MongoDB using Multer as a file from Angular is not working as anticipated

I'm currently dealing with an issue that I believe is not functioning correctly. I installed a library in Angular called cropper.js from https://github.com/matheusdavidson/angular-cropperjs. The frontend code provided by the developer utilizes this li ...

Tips for writing an async function using TypeScript

I've been working with Typescript and NLP.js. However, I'm encountering an issue where the argument manager is displaying 'Parameter manager implicitly has an any type'. I attempted to use :, but it didn't solve the problem eff ...

Can Angular Universal SSR be activated specifically for Googlebot User Agents?

I am aiming to activate Angular Universal SSR only when the User Agent is identified as Googlebot. However, I am uncertain about how to instruct Angular Universal SSR to deliver server side rendered HTML exclusively if the User Agent is Googlebot. server ...

Retrieving time zone using offset with javascript

I am looking for a way to schedule time-based events on my server operating in UTC time. For the user interface, I need to input 2 parameters: The local time when the scheduled event should trigger The timezone offset Instead of displaying timezone n ...

Combining objects in JavaScript

I am currently working on converting the object received from the server into a format compatible with the backend system. I have a received object that looks like this { 'User.permissions.user.view.dashboard': true, 'Admin.permissio ...

Generate a string that will be utilized to interpret a JSON response

I can't seem to extract a specific element from a json using a dedicated function. Can someone please assist me with this issue? Here is the fiddle that I have created for reference: http://jsfiddle.net/jonigiuro/N5TTM/2/ CODE: var data = { "res ...

What are some solutions to troubleshoot the baseurl bug in Axios for a Node.js application?

An application was developed for a specific device using Vue.js on the front end and Express.js on the back end. The Axios library was utilized to fetch data from an API. However, when attempting to localize the base URL in Axios, connecting to the site t ...