"Exploring three.js: How to locate the closest point on a Mesh to the position of

In this scenario, picture a three.js scene configured with an OrthographicCamera and OrbitControls:

https://i.sstatic.net/rhJnr.png

When the user moves the yellow disc (representing the Sun), it should follow along its corresponding yellow circle. To better visualize this, here's another perspective of the scene showing the complete yellow circle:

https://i.sstatic.net/sRQ3g.png

My challenge lies in figuring out how to identify the point on the circle closest to the current cursor position using an event handler. Just for reference, the yellow circle is a THREE.Mesh object.

I've been trying to use THREE.Raycaster and its intersectObjects() function to detect mouseover events, but I'm unsure about how to determine the nearest point on a single object with this Raycaster. Perhaps there's some straightforward calculations involved after transforming the mouse coordinates into world coordinates. Can anyone offer guidance on this? Is Three.js's Raycaster suitable for this task? If not, what approach should I take to find the nearest point on this mesh?

If you want to view the entire source code, visit: https://github.com/ccnmtl/astro-interactives/blob/master/sun-motion-simulator/src/HorizonView.jsx. Look for this.sunDeclination, which represents the Mesh object of the yellow circle.

To see a demo in action, click here:

If you want to understand how the sun should move, check out this link: (Flash required)

Answer №1

A straightforward approach:

  • select a point located on the disk
  • create a projection within the circle's plane
  • determine the appropriate factor to scale the vector by, based on the circle's radius

var chosenPoint = res.point.clone();
chosenPoint.z = 0; // Ensure it lies on the circle's plane
var scaleFactor = circleRadius / chosenPoint.length();
chosenPoint.multiplyScalar(circleRadius / chosenPoint.length())

[ https://jsfiddle.net/c4m3o7ht/ ]

Answer №2

The raycaster provides information on all objects intercepted by the ray, including their hit points in worldspace (which can be converted to or from model space using object3d.worldToLocal and localToWorld).

It also supplies the distances of the hits, allowing for sorting based on various criteria.

My typical approach involves casting on mouseDown to document the object and point, then on mouseMove retrieving the hit point of the same objects to execute editing operations based on the difference between the two points.

Does this align with your inquiry?

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

Unable to bind to property as it is not recognized as a valid attribute of the selector component

I have a situation where I need to pass a variable from one component to another using @input. Here is my parent component : @Component({ selector: 'aze', templateUrl: './aze.component.html', styleUrls: [('./aze.compo ...

Verify your identity using Google reCaptcha during Passport authentication

I am currently working on integrating google reCaptcha into my signup form's back-end, which already includes passport authentication. The code I have so far looks like this: app.get('/signup', function(req, res) { // render the ...

Element sticking and bouncing

I'm currently facing an issue with Sticky-kit where one of my elements jumps when it reaches the bottom of its parent div. Below is the code snippet: <div class="wrapper"> <div id="bg"> <div id="text"> Lorem ipsum dolor sit a ...

Steps to update the package version in package.json file

If I remove a package from my project using the following command: npm uninstall react The entry for this package in the package.json file does not disappear. Then, when I install a different version of this package like so: npm install <a href="/cdn ...

What is the best way to implement date range filtering in vue js?

Currently, I am delving into the realm of vue.js and experimenting with a filtering feature that involves date ranges. The process goes like this: initially filter by type, then proceed to filter by a specified date range, consisting of start and end dat ...

How can I align the isometric camera in THREE.js to the center of a cube?

Hey there, I'm working on a tricky exercise involving setting up a camera and cube with THREE.js. The goal is to maintain the cube's initial ratio while ensuring the camera is focused on the center of the cube. Although I believe I've succes ...

extracting a JSON array from an API

I'm trying to extract the title from my API using JavaScript and then display it in HTML. api:(https://k0wa1un85b.execute-api.us-east-1.amazonaws.com/production/books) The JSON response is as follows: {"statusCode":200,"body":[{"id":"4f160b1f8693cd ...

Exploring the power of JSON and JavaScript: A guide to efficiently reading lists through JSON manipulation

I am facing an issue where I am unable to read an object using the method below function loadData(){ $.getJSON('data.json', function(data) { $.each(data, function(Index, entry) { mem = new user(entry.name, entry.age, entr ...

Adjusting the line width and incorporating arrow points into AxesHelper in Three.js

I have successfully implemented axes using AxesHelper https://i.sstatic.net/gYCKW.png Currently, I am looking to enhance the width of the axes lines and incorporate arrow points. Since I am new to three.js, I could really use some guidance on how to ach ...

Struggling to get the ReactJS.NET MVC tutorial to function properly?

After deciding to start a new project in Visual Studio with MVC 5 and a single page app using ReactJS, I consulted the guide on the ReactJS website. Upon running the project for the first time, I encountered a syntax error due to JSX. It seemed that the b ...

Exploring options in THREE.js beyond MeshFaceMaterial

As per discussions on the three.js github, there are indications that MeshFaceMaterial will eventually become deprecated. My current implementation uses this for terrain rendering, although I acknowledge it's not the most efficient method. In fact, i ...

Identify and troubleshoot scripts that are included in the response returned by Chrome

I am facing an issue where I have a webpage that loads HTML sections through an AJAX call. The response includes both HTML and JavaScript files. Currently, I am trying to figure out how to set a debug point on the JavaScript file. In Internet Explorer, I ...

Is there a way to display these panels in a scrollable table layout?

My aim is to replicate this specific styling: https://i.stack.imgur.com/jz5lm.png This type of table shown above is being dynamically imported via Redux: class LocationList extends React.Component { componentDidMount() { this.props.fetchLocations( ...

Load the scripts only if they have not already been loaded, and proceed to utilize them

Currently, I have a situation where I must load 4 javascript libraries if they have not been loaded already, and then proceed to utilize their functions. However, I am encountering an issue with Uncaught ReferenceError: sbjs is not defined. Even though th ...

JavaScript: Creating a Function that Returns an Object with an Undefined `this.variable`

While using javascript, I encountered an issue with instance variables. Declaring a variable as "this.variable" works fine until my function returns an object. However, if the function returns a String or Number, there are no issues. But when it returns ...

Angular template driven forms fail to bind to model data

In an attempt to connect the model in angular template-driven forms, I have created a model class and utilized it to fill the input field. HTML: <div class="form-group col-md-2 col-12" [class.text-danger]="nameCode.invalid && nameCode.touched ...

Using JavaScript to eliminate brackets

When a map is clicked, I have a function that retrieves GPS coordinates. However, the result is currently displayed in brackets (). It is necessary to eliminate the brackets. function onClickCallback(event){ var str = event.latLng var Gpps = str / ...

Configuring a timeout for your Next.JS API can help optimize the performance

Due to restrictions on another API, my own API takes 10 minutes to return a result. When running locally, I am able to wait and successfully receive the data after 10 minutes. However, when deployed on Vercel, I encounter a timeout error (status code 504). ...

The body-parser module in express 4.0 is currently not accessible for the route handler

I am attempting to send a PUT request via Google Postman in the following format: PUT /updateUser/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="debcbcbc9eadadadf0bdb1b3">[email protected]</a> HTTP/1.1 Host: loca ...

Transitioning from jQuery to Prototype

After switching from a jQuery background to Prototype, I am curious if there is a chart available that shows the equivalent prototype methods for specific jQuery methods? To be more specific, I am searching for the equivalent of $('#my-id').prep ...