Discover the items that a line intersects within the realm of three.js

I am currently immersed in a three.js project that involves selecting two boxes from a board and drawing a straight line to connect them. Once this is done, the goal is to highlight all the boxes that the line intersects. My main challenge at the moment is figuring out how to identify which boxes the line crosses paths with.

Any assistance on this matter would be greatly appreciated - whether it be a code snippet, relevant links, recommended reading material, or mathematical formulas.

Answer №1

It appears that you are in need of utilizing a Raycaster for what you have described.

To achieve this, establish your ray by defining its position and direction vector, followed by employing the intersectObjects method on the objects to obtain a collection of the boxes intersected by the ray.

Answer №2

For solving this particular problem, I found that utilizing Bresenham's line algorithm was essential in order to accurately track all the various points along the line.

Discovered at https://www.geeksforgeeks.org/bresenhams-line-generation-algorithm/

A big thank you goes out to Ivan and Diarmid for their assistance, even though my code was quite messy.

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

Give drawn elements a touch of fuzziness and experiment with the color of the fragment shader

I am currently experimenting with creating an animated gradient effect by blending three separate blobs together in a melting-like fashion, with each blob moving independently. The desired result can be seen in the image provided below. So far, I have bee ...

In situations where there may be a duplicate, what alternative can I utilize in place of the id attribute?

I understand that almost any element in the DOM can have an "id" attribute, and I've used it to track each client in a table of clients. Although ids should not be repeated, my rows are assigned unique identifiers based on each person's "clientId ...

Is it possible for a synchronous (blocking) ajax request to delay the rendering of a website's UI?

This query pertains to jQuery, though not exclusively focused on it. To sum it up: Can a synchronous ajax call prevent a regular button from being clicked on? My experimentation shows no issues, but compatibility with other browsers could be a concern. ...

Developing a custom function that analyzes two distinct arrays and sends any corresponding pairs to a component

I'm in the process of developing a component that utilizes two arrays. These arrays are then mapped, and the matching pairs are sent down as props to a child component. The goal is to create a list component that retrieves the arrays from the backend ...

npm does not accommodate the current version of Node.js (vX.X.X)

Today, I am attempting to install the most recent version of Node.js (13.12.0) along with npm. However, I have encountered an issue because the latest npm version (6.14.4) does not support this Node.js version, resulting in the following error message: np ...

Angular 2's Multi-select dropdown feature allows users to select multiple options

Recently, I encountered an issue with customizing CSS for angular2-multiselect-dropdown. I found the solution in this link: https://www.npmjs.com/package/angular2-multiselect-dropdown. I have included my code below. Any assistance in resolving this matter ...

Sending a form without the need to reload the page

While it's known that Ajax is the preferred method to submit a form without refreshing the page, going through each field and constructing the Post string can be time-consuming. Is there an alternative approach that utilizes the browser's built-i ...

What causes the self-invocation of "foo()" when it is assigned to the addEventListener function?

function foo() { console.log("clicked"); } element.addEventListener("click", foo()); How come foo() is invoked automatically when the script loads, but foo is not? Also, what if I want to pass an argument to the function like this: function foo(elem ...

The Angular FormGroup may not reflect the updated value right away after using patchValue or setValue methods

Below is a form I have created: createForm() { this.procedimentoForm = this.formBuilder.group({ nome: ['', Validators.required], descricao: ['', Validators.required], conteudo: ['', Validators.required] ...

The dirtyFields feature in React Hook Form is not accurately capturing all the fields that are dirty or incomplete,

I am facing an issue with one field in my form, endTimeMins, as it is not registering in the formState. I have a total of four fields, and all of them are properly recognized as dirty except for the endTimeMins field. It is worth mentioning that I am utili ...

AngularJS is capable of executing conditional logic using the if/else statement

I am attempting to set the inputLanguage value to 'en' and encountering an error that says english is not defined. Here is the code snippet from my alchemy.js file: module.exports = function(Alchemy) { Alchemy.language = function(inText, inp ...

Discovering the presence of a component in Angular 1.5

Link to embedded content $injector.has('myMessageDirective') is returning true, while $injector.has('myMessageComponent') is not. Has anyone else encountered this issue or found a solution? I am concerned that my components may not be ...

React useEffect not working when using the default state

I'm currently facing an issue where setting the page to its default value from the refresh function does not trigger the useEffect hook on the first attempt. However, if I run the refresh function for the second time, it works fine. Interestingly, thi ...

Spotlights in Three.js failing to cast shadows

Visit to see the current issue at hand. On that page, you will find links to videos showcasing an animation and a real clock that serves as my inspiration for what I am trying to achieve using three.js. The concept involves a clock with 2 LEDs rotating ...

Is the size of the array significant in the context of JavaScript here?

Whenever a button is clicked on the page, I am dynamically creating an array in javascript using item id's fetched from the database. Each entry in the array will hold a custom object. The id's retrieved from the database can range from numbers ...

Eliminate a specific choice from a drop-down menu in an Angular application

I am implementing a feature where clicking on a button adds more select drop downs. I want to ensure that the selected options in these new dropdowns do not duplicate any already chosen options. Below is the code snippet used for the select drop down: < ...

JavaScript pop-up purchase tooltips from MenuCool

I am relatively new to the world of web design and programming. I am putting in a lot of effort to learn as much as possible, but I am encountering difficulties with the tooltip JavaScript that I incorporated into my website Click here to visit my website ...

Pagination using Laravel 4, Angular JS, and the latest Twitter Bootstrap 3 features

Update: In my application built with Laravel 4 and Angular JS, I am in need of pagination functionality. While there is an option to use the angularui-bootstrap pagination, I have decided to explore and integrate the features of Laravel pagination with An ...

How to access a global variable within an Angular application

Using node express and angular, I have set up a route in my express app.js where I pass a variable to the rendered page: app.get('/test', function(req, res){ res.render('test', { user: 12345 }); }); Within my 'test' view, ...

Am I on the right track in my understanding of how document and viewport relate to mouse position in JavaScript?

After reviewing responses from a previous inquiry, it is evident that both pertain to the x and y coordinates of mouse positions. In relation to the document and In relation to the viewport. I have delved into an article on QuirksMode, yet I feel ther ...