Using THREE.js: Finding the nearest point between two rays in a THREE.js environment

In my current project with THREE.js, I am working with two THREE.Ray objects. Both rays have an origin point represented by a Vector3 and a direction indicated by another Vector3.

I am currently exploring how to determine the nearest intersection point between these two rays.

Answer №1

To uncover the solution, it is crucial to understand that the line connecting the two points closest to each other is perpendicular to the two rays.

The initial step involves determining the direction vector of the line formed by the two closest points. This vector can be obtained using the cross product since it is normal to both rays. For example, let's consider rayA and rayB as objects of type THREE.Ray:

let Nv = rayA.direction.clone().cross(rayB.direction);

Proceeding further, a plane needs to be identified for each ray, encompassing the ray itself along with the closest point on the opposite ray. A plane consists of two vectors - the directional vector of the plane and Nv. However, an alternative representation of the plane is needed, involving a point and a normal vector. The point corresponds to the origin of the ray. Once again, the normal vector can be derived through the cross product. To facilitate subsequent calculations, these vectors must be normalized to become unit vectors (with a length of 1):

let Na = rayA.direction.clone().cross(Nv).normalize();
let Nb = rayB.direction.clone().cross(Nv).normalize();

The primary concern now shifts to finding the intersection of a ray and a plane. Consider ptA and ptB as THREE.Vector3 objects representing the closest points on the respective rays:

let Da = rayA.direction.clone().normalize();
let Db = rayB.direction.clone().normalize();

let da = rayB.origin.clone().sub(rayA.origin).dot(Nb) / Da.dot(Nb);
let db = rayA.origin.clone().sub(rayB.origin).dot(Na) / Db.dot(Na);

let ptA = rayA.origin.clone().add(Da.multiplyScalar(da));
let ptB = rayB.origin.clone().add(Db.multiplyScalar(db));

Diving deeper into the explanation of the intersection between a ray and a plane:

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

A ray is described by a point Ra and a direction Da. On the other hand, the plane is defined by a point Rb and a normal vector Nb.

The perpendicular distance n from point Ra to the plane is calculated using the dot product:

n  =  | Rb - Ra | * cos(alpha)  =  dot(Rb - Ra, Nb)

This leads to the determination of the distance da from the intersection point ptA to the origin of ray Ra:

da  =  n / cos(beta)  =  n / dot(Da, Nb)

The coordinates of the intersection point ptA are then given by:

ptA  =  Ra + Da * da  =  Ra + Da * dot(Rb - Ra, Nb) / dot(Da, Nb)

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

What is preventing the direct passing of the dispatch function from a useState hook into an onClick handler function?

const Counter = () => { const [count, setCount] = useState(0); // Uncommenting the line below would result in an infinite loop because it directly invokes setCount(count + 1) on render. // This causes the component to continuously re-render and up ...

"Error encountered when attempting to upload directory due to file size

Utilizing the webkit directory to upload a folder on the server has been successful, however, an issue arises when there are more than 20 files in the folder. In this scenario, only the first 20 files get uploaded. The PHP code used for uploading the fold ...

Sending data with the request body using Express and Passport

I've created a login application using Express and Passport, but I'm having trouble directing the user to their specific user profile page. The goal is for users to fill out an HTML form and then be redirected to their own profile page (which dif ...

How to extract parameters from an http get request in Node.js

Trying to handle an HTTP request in this format: GET http://1.2.3.4/status?userID=1234 I am unable to extract the parameter userID from it. Despite using Express, I am facing difficulties. Even when attempting something like the following, it does not yi ...

Issues with Angular functionality

I'm a newcomer to Angular and I am trying to recreate this example from jsfiddle in order to experiment with nodes. However, I am encountering issues with the Angular setup. Firstly, the jsfiddle is causing confusion because the application names do n ...

Double quotes in JSON are not being escaped properly

When I try to evaluate some json on a screen, I keep encountering errors with the message 'unexpected identifier'... The problematic data that seems to be causing this issue is: "itemDescription":"STANDARD \"B\" RED BOX", To address ...

Adjusting the height of a Vue component to 100% triggers a change in height whenever a re-render occurs

In my Vue component, there is a class called "tab-body-wrapper" that serves the purpose of displaying the "slot" for the active tab. However, I encountered an issue while troubleshooting where the height of the ".tab-body-wrapper" component reduces during ...

The error message "TypeError: Unable to access the 'get' property of an undefined vue js object" appeared

Struggling to grasp the concept of vue.js, I am currently navigating my way through understanding how to fetch or call an API. Setting up my index.html and app.js, along with the required packages in the node_modules, has been relatively smooth. However, ...

Unable to reset iframe style height in IE8 using JavaScript

I am encountering an issue with resetting the height of an iframe using JavaScript. Here is the code snippet I am working with: var urlpxExt = document.getElementById('urlPx'); urlpxExt.style.height = "200px"; While this approach works well in m ...

Manipulate text within a text area using jQuery

Good afternoon. How can I use JavaScript to update the text in a textarea input? I currently have some content in my textarea that includes a PublisherImprintName tag, and I want to remove it using jQuery. <PublisherInfo> <PublisherName>A ...

Steps for populating an ng-table with data retrieved from a REST web service that returns a JSON format

I am currently facing an issue while trying to load my table from a JSON response that I receive from REST web services in SpringMVC. The error message I received indicates that my REST method does not support the GET request. The URL mapped in my control ...

What is the process for building an interactive quiz using JavaScript?

In the process of creating a quiz, I envision a user interface that presents questions in a "card" format. These questions will include simple yes/no inquiries and some multiple-choice options. As the user progresses through the quiz, their answers will de ...

Enabling sidebar functionality for every component in React JS using React Router v6

I am currently using react router dom v6 and encountering an issue where the sidebar disappears whenever I click on any component in the app. I need to find a way to keep the sidebar visible across all components. Sidebar Available https://i.sstatic.net/ ...

Mastering the art of raycasting onto a point cloud in ThreeJs R71

I am currently working with Autodesk Forge, leveraging Three.js r71, and I am looking to implement a raycaster to identify clicks on various elements within a point cloud. If anyone could provide a sample code snippet on how to achieve this using Three.js ...

Trying to configure and use two joysticks at the same time using touch events

I have been grappling with this problem for the past two days. My current project involves using an HTML5/JS game engine called ImpactJS, and I came across a helpful plugin designed to create joystick touch zones for mobile devices. The basic concept is th ...

I am experiencing difficulty with the button not responding when clicked, despite trying to implement JavaScript and the Actions syntax

Currently, I am in the process of automating form filling. After filling out the form, there is an update button that changes color instead of clicking when activated. This alteration indicates that the xpath is correctly identified. I have attempted two ...

Is there a way to transform a JSON object into a custom JavaScript file format that I can define myself?

I have a JSON object structured as follows: { APP_NAME: "Test App", APP_TITLE: "Hello World" } My goal is to transform this JSON object into a JavaScript file for download. The desired format of the file should resemble the follo ...

The dynamic route parameter does not currently have compatibility with languages that utilize Unicode characters apart from the English

NextJS 13 (beta) Official Documentation provides information on dynamic route parameters in this link. Clarification: app/shop/[slug]/page.js To retrieve the slug, use params.slug. Example: app/shop/this-is-shop/page.js In this case, params.slug will o ...

Contrasting outcomes when tackling a problem in node.js versus python

After tackling a challenging leetCode problem, I successfully came up with the following solution: Given d dice, each with f faces numbered from 1 to f, determine the number of possible ways (modulo 10^9 + 7) to roll the dice so the sum of the face up nu ...

What's the best way to create flying text effects - using CSS or JavaScript

Looking for a way to enhance the user experience of my shopping cart on Android and iPhone/iPod, I want to implement a feature similar to the iTunes Store where the price flies down to the total when a purchase is made. Since I use jQuery (not UI) on this ...