calculating the rotation angle from the origin to a vector within a three-dimensional space using three.js

I am working with a vector in three-dimensional space.

Is there a method to determine the angle of rotation from the origin to this vector on each axis?

For example, if the vector is located at x=6, y=-10, z=20,

what angle does the vector make with respect to the origin (x=0, y=0, z=0) on the x, y, and z axes?

I have been trying to solve this in Three.js for a while now. Any assistance would be greatly appreciated.

Answer №1

One way to determine the angle between two vectors is by using this formula: v1•v2 = |v1||v2| cos(angle). To calculate the angle, you can use the acos function on the dot product of the two vectors.

In ThreeJS, the dot product can be calculated as follows: dotproductV1V2 = V1.dot(V2)

You can then apply acos to the result in order to determine the angle: Math.acos(dotproductV1V2)

Edit :

Here's an example that illustrates how to find the angle for each component of a vector:

var V1x = new THREE.Vector3(1, 0, 0)
var V1y = new THREE.Vector3(0, 1, 0)
var V1z = new THREE.Vector3(0, 0, 1)

var V2 = new THREE.Vector3(6, -10, 20)

var V2xz = new THREE.Vector3(V2.x, 0, V2.z)
var V2xy = new THREE.Vector3(V2.x, V2.y, 0)

//angle in radian between origin X axis and X axis of V2
var angle_V1V2x = Math.acos(V1x.dot(V2xz.normalize()))

//angle in radian between origin Y axis and Y axis of V2
var angle_V1V2y = Math.acos(V1y.dot(V2xy.normalize()))

//angle in radian between origin Z axis and Z axis of V2
var angle_V1V2z = Math.acos(V1z.dot(V2xz.normalize()))

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

Deciphering the hidden power of anonymous functions within Express.js

Recently, I started learning about express and am grappling with understanding callbacks in RESTful actions. In the following PUT request code snippet, I am puzzled by the specific line that is highlighted below. Why is response.pageInfo.book being assigne ...

Difficulty encountered when bringing in React elements

I encountered an issue after reconfiguring the folder structure and changing import paths for my components. Now, I'm facing a Module not found error even though the file paths seem correct. import Header from "./Components/MiscComponents/Header& ...

Are MVC Controllers fundamental components in a Node.js application, or just another piece of the puzzle?

Currently, I am in the process of developing a web crawler using Node. This project aims to crawl through my different bank accounts and generate a quick summary of my financial situation. Although I am fully aware of the security concerns involved in this ...

Leveraging the httponly cookie for transmitting JWT token between a Node REST API and a Vue.js SPA

I am currently in the process of developing a web application that utilizes a Vue.js frontend along with a Node REST API built using express. I am facing a challenge in implementing authentication, particularly in striving for a stateless approach by utili ...

Arrange JSON information in an HTML table with distinct header rows for every data category

I have a JSON object with a key:value pair named callRoot. Some examples of values for this pair are @S, @C, and @W. There are multiple objects that share the same value and I want to create an HTML table head row at the beginning of each group and repeat ...

execute the execCommand function following an ajax request

I am currently developing a WYSIWYG editor and encountering an issue with image handling using execCommand. Here is a simplified example of my page structure: <div id="buttons_panel"><input id="img_submit" type="button"/></div> <div ...

Obtain JSON information from a backend Node.js server and transfer it to the frontend HTML

I am currently facing an issue while trying to access JSON data and display it on the frontend screen. I have exported it in Node.js and attempted to import it into a script tag in HTML, but unfortunately, I am not able to retrieve the data. Is there a sol ...

Adding a dynamic form to each row in a table: A step-by-step guide

https://i.sstatic.net/HctnU.jpg Hey there! I'm facing a challenge with dynamically generated rows in a form. I want to send only the inputs from the selected row when a checkbox is clicked. Can you help me figure this out? I tried using let rowForm ...

Updating Vue-Devtools props in Chrome while executing asynchronous promise functions: A step-by-step guide

When working with Vue 3 and mutating a prop that is an object (I understand it's not recommended to mutate props directly, but in this case it works because it's passed by reference as an object), I noticed that the changes reflect in the Vue Dev ...

Why does the parent URL become the origin for an AJAX request coming from an iframe?

I am facing an issue with a website where I need to load an iframe from a different subdomain. The main website is hosted on portal.domain.com, and the iframe is on iframe.domain.com. To make requests to iframe.domain.com from portal.domain.com, I decided ...

Do individual JavaScript script tags operate independently of one another in terms of error handling?

My main goal is to establish a connection to my server using websockets and send messages to the browser for remote page reloads. I want to ensure that this code runs independently of any other errors on the page, allowing me to remotely refresh the page i ...

make changes to the current selection in the drop-down menu

Imagine I have a select list with 3 options: <select> <option>1</option> <option>2</option> <option>3</option> </select> My goal is to create a textfield and button that will allow me to upd ...

Can someone recommend a streamlined JavaScript algorithm for identifying the unique arrays within a collection of arrays?

Consider the array below: let array = [[1, 2], [1, 2], [3, 4], [5, 6], [2, 1]] I am looking for a way to determine the number of unique arrays in this set. Using the array above as an example, the expected result is 3. How can I achieve this? The code sn ...

The kendo-chart-tooltip script is causing an error by generating an Uncaught TypeError: background.isDark is not a recognized function

Click here for image descriptionHaving issues with the kendo-chart-tooltip functionality in my Angular 5 platform. The console shows a script error related to 'background.isDark' not being a function. zone.js:192 Uncaught TypeError: back ...

"Error: The angularjs code is unable to access the $http variable within

$http({ url: "php/load.php", method: "GET", params: {'userId':userId} }).success(function(data, status, headers, config) { $scope.mydata = data; mydata = data; }).error(function(data, status, headers, config) { }); It's puzzling why ...

Is it advisable to include the `engine` attribute in the packages.json file for a web browser library project?

When creating a JS library project meant for browser use, do I need to include the engines field in the packages.json file? ...

Ways to integrate PHP MySQL with NodeJS and SocketIO

Currently, I am working on developing a chat application. I have successfully implemented features like creating accounts, logging in, selecting, viewing, and more using PHP MySQL. Now, I am venturing into the Instant Messaging aspect by utilizing NodeJS a ...

Making adjustments to a Jquery data attribute can have a direct impact on CSS

My CSS is using a data attribute, but when I try to change it with jQuery, the change is not reflected on the screen or in the DOM. $('#new-1').data('count', 5); .fa-stack[data-count]:after { position: absolute; right: -20%; to ...

A guide on effectively testing the value of Object.constructor using Jasmine and minimizing redundancy in the it statements for AngularJS

In my AngularJS application, I have defined an object model like this: .factory('Watermark', function () { // Constructor, with a class name // Assumption: the backend provides us with a topic or not! function Watermark(content, tit ...

The download attribute does not seem to function properly when used within the <a></a> tag in ReactJS

I've been attempting to download a PDF using the "download" attribute within an <a> tag: <a className="dl_link" href={'./myfiles/abc.pdf'} target="_blank" download/> However, instead of downloading, it only opens in the next ...