How can Three.js help determine the direction between two 3D vectors?

There are two points that I need to consider:

v1 = (0, 0, 0);
v2 = (10, 4, -3);

I am looking to determine the direction between these two points in order to perform a raycast from point v1 to v2.

Can someone guide me on how to achieve this?

Answer №1

To create a direction vector from v1 to v2, follow this pattern:

var dir = new THREE.Vector3(); // create it once and reuse

...

dir.subVectors( v2, v1 ).normalize();

In three.js, direction vectors are expected to be unit-length. In other words, they need to be normalized. If the direction vector used for raycasting is not of length 1, the results will not be accurate.

Version: three.js r.82

Answer №2

Let's delve into a bit of geometry: direction can be calculated as the difference between vector 2 and vector 1

Also, taking a peek at some ThreeJS documentation might help: https://threejs.org/docs/#api/math/Vector3

Voilà! Problem solved!

 var direction = new THREE.Vector3();
 direction.subVectors( v2, v1 ) ;

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

Issue with Aligning Elements Horizontally

I am looking to align this content at the center horizontally: <a href='/large.jpg' class='cloud-zoom' id='zoom1' rel=""> <img src="/small.jpg" alt='' title="Optional title display" /> ...

Steps for making a toggle button that switches panels

Within my application, there are 2 toggle buttons and 3 panels. Button1 toggles between panel 1 and 2, while button2 switches between panel 1 and 3. I managed to make them work by setting the display: none; property to div panels, but after one use of each ...

Error encountered: "Unable to process Three.js FontLoader due to SyntaxError

I attempted to generate 3D text with FontLoader in Three.js, but encountered an error. My Three.js version is r99. const loader = new THREE.FontLoader(); //https://github.com/mrdoob/three.js/tree/dev/examples/fonts loader.load("./fonts/helvetiker_ ...

Whenever I try to access my Node.js API from Heroku's live server, I encounter a CORS policy error

Whenever I try to access my Node.js API from the Angular app (running on a local setup) and host the API on Heroku's live server, I encounter the following error: CORS policy: No 'Access-Control-Allow-Origin'. Interestingly, when I host the ...

Hide dropdown menu when clicked in Angular Bootstrap UI

On my navigation bar, I have a dropdown menu that is only displayed on mobile devices. The issue I am facing is that when I click on a link in the dropdown menu, it takes me to another page without refreshing the entire page. However, the dropdown menu rem ...

What is the process for converting a multidimensional array from PHP into JavaScript?

Recently, I've been experimenting with dygraph. To populate my graph, I fetch data via an ajax call and then need to convert the returned JSON array into a JavaScript array. My dygraph options are set up like this: series = { sample1: { ...

Creating dynamic button names and detecting the pressed button in PHP

Right now, I am experimenting with PHP to create a unique project. This experiment is just a small part of a larger file that I am working on. Essentially, the aim is for the program to generate a series of submit buttons within a form, each assigned a dis ...

Identifying and Blocking Users from Accessing External Domains Outside of the Angular Application

I am working on an angular application and I need to implement a feature where I can detect when a user navigates outside of the app domain from a specific component. For instance, let's say the user is on the upload component processing important in ...

New to Angular: Getting Started with NgModel Binding

Novice query: I am facing an issue with a simple input type=text element linked to ng-model="xyz.zyx", where xyz refers to an object. In my controller, I initialize this object and set the value for the property zyx as shown below: xyz { zyx: $scope.zz ...

"ExceptionThrownByMoveTargetOutOfBounds in Selenium WebDriver for IE9 and Firefox

Having trouble with the FireFox/IE9 driver in Selenium? When using the Actions class and its moveToElement method, I keep encountering a MoveTargetOutOfBoundsException error. Despite trying different solutions like Coordinates, Point, and javascriptexecuto ...

Identify the externally-sourced element of interest

I'm attempting to apply a ScrollReveal effect to an element loaded from a separate html file called "header.html". Unfortunately, the ScrollReveal animation is not working on this particular element, while other elements within my index.html are funct ...

The vertex colors in the three.js viewer for GLTF files appear to be inaccurate

Hello there, I've just come across a strange issue while importing my GLTF file: When I load it into my three.js viewer, the colors appear different compared to the original file. Take a look at McCudy's viewer: GLTF file as viewed on https://g ...

Having trouble updating the state value using useState in React with Material-UI

I have developed a unique custom dialog component using Material-UI const CustomDialog = (props) => { const [dialogOpenState, setOpen] = React.useState(props.dilaogOpenProp); return ( <> <CssBaseline /> <Dialog ...

Adding a file upload feature within an existing form

Ideally, I am looking to achieve the following structure: <form1 ...> ... <form2 ...> //This form uses AJAX to upload a file and then displays the content in a text field below. </form2> <input type="t ...

Increasing the height of the progress bar in videojs

Incorporating videojs into my react application has been a great addition. However, I've noticed that the default skin attributes have resulted in a progress bar that is too thin for my liking. Is there a method to increase the thickness of the progre ...

The response from AJAX delivers HTML data

I recently implemented an AJAX call in my CodeIgniter project. Let me show you the code: Here is the view section: $('#forgotPassword').click(function() { var base_url = '<?php echo base_url()?>'; $('#forgo ...

Issue with JQuery form submission causing controller method not to be executed

I am facing an issue with two click methods in my JavaScript code. Method 1 is successfully hitting the controller method, but when I try to trigger Method 2 by clicking on a button, it does not seem to call the controller. I have tried various solutions ...

Incorrect date being sent by Mat Date picker

I am encountering an issue with date selection using this input field <mat-form-field class="w-full"> <mat-label>{{ "DATE" | translate }}</mat-label> < ...

Ways to switch out the background image using jQuery

I am currently using jQuery to dynamically change the background image of a web page. Right now, I have implemented two separate buttons that toggle between Image A and Image B. By default, Image A is displayed on the page. My goal is to enhance this func ...

Child element casting shadow over parent element

I am currently using box shadow for both the parent (.map) and child (.toggle-button): .map { position: fixed; top: 20px; right: 0; width: 280px; height: 280px; z-index: 9999; box-shadow: 0px 1px 6px 0px rgba(0,0,0,0.3); } .map ...