Mastering the Art of Rotating Objects Using Three.js World Axis

How can I retrieve the rotation values of an Object3D based on world axes like:

roation.x, rotation.y, rotation.z

Currently, when I use

object.rotation.x 

I receive the rotation value relative to the object's local axes.

Appreciate any help!

Answer №1

Apologies for resurrecting an old thread.

I encountered the same issue and was struggling to find a solution until I came across some enlightening ThreeJS documentation on Euler angles. Suddenly, everything made sense.

When working with Three.js, remember that it utilizes intrinsic (Tait-Bryan) ordering, which consists of yaw, pitch, and roll. This means rotations are executed in relation to the local coordinate system. For example, with order 'XYZ', the rotation occurs around world-X first, followed by local-Y (which may differ from the world Y-axis), and then local-Z (which might not align with the world Z-axis).

Some implementations use extrinsic (proper) ordering, where rotations are carried out relative to the world coordinate system. In this case, for order 'XYZ', the rotations happen around world-X, world-Y, and world-Z.

To convert between these two types, simply reverse the order and the rotation. Therefore, an intrinsic (Three.js) Euler rotation with angles a, b, c about XYZ is equivalent to an extrinsic Euler rotation with angles c, b, a about ZYX.

If you need to obtain the world rotations in XYZ order, you can try the following approach :

var worldRot = new THREE.Euler();
obj.getWorldRotation().copy(worldRot);
worldRot.reorder("ZYX");
// access worldRot.x, worldRot.y, worldRot.z

Answer №2

If you want to rotate an object on the world axis, you can achieve this by using the following function:

let rotationWorldMatrix;
function rotateOnWorldAxis(item, axis, angle)
{
    rotationWorldMatrix = new THREE.Matrix4();
    rotationWorldMatrix.makeRotationAxis(axis.normalize(), angle);
    rotationWorldMatrix.multiplySelf(item.matrix);
    item.matrix = rotationWorldMatrix;
    item.rotation.getRotationFromMatrix(item.matrix, item.scale);
}

Simply call rotateOnWorldAxis(objectToRotate, new THREE.Vector3(1,0,0), 90 * Math.PI/180);

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

Stop the execution of the setTimeout function when the webpage loads in JavaScript

In my postgres database, I have a table named students for storing student information. The structure of the table is as follows: id first_name last_name time_remind 1 pers1 pers1_name 2022-07-30 21:30 2 pers2 pers2_name 2022-07-30 20:38 Current ...

Error: Jest encounters an unexpected token 'export' when using Material UI

While working on my React project and trying to import { Button } from @material-ui/core using Jest, I encountered a strange issue. The error message suggested adding @material-ui to the transformIgnorePatterns, but that didn't resolve the problem. T ...

Detecting collisions in JavaScript

Currently, I am in the process of reviving an old game reminiscent of Tron using HTML5 canvas and JavaScript. The unique twist in this version is that the snakes have the ability to move in curves rather than right angles (known as Achtung Die Kurve). The ...

Unable to retrieve scope variable within AJAX call

I am currently in the process of running a loop function that contains an AJAX request within it. However, I am facing difficulties with the success function on the AJAX not being able to access a counter variable outside of the loop function. var upda ...

Using environment variables in next.config.js allows for successful connection to the database, however, when attempting to use a

Utilizing the serverless-mysql library, I have successfully connected my next app to a remote MySQL DB through an SSH tunnel with the ssh2 library. Although everything is functioning properly, I am looking to enhance the security of my code by removing the ...

How can one activate a function using jQuery and then proceed to activate another function once the first function has finished executing?

My current assignment involves implementing a task using jquery. Upon loading the page, the initial jquery function will be executed. After the successful completion of the first function, it should seamlessly transition to the next function on its own. ...

Tips for configuring Visual Studio Code to utilize path mappings for handling automatic imports

In order to streamline my project and avoid messy paths, I am implementing absolute paths that will allow for consistent imports regardless of the file's location in the project tree. For this purpose, I made adjustments to the tsconfig.json: "paths ...

Sending information through a form in html to a server via AngularJS Http Post

Seeking assistance with a posting issue related to an AngularJS form. Despite using ng-submit and ng-click for the function, it's not functioning as expected. Can anyone offer guidance? <form method="post" class="sky-form" name="form"> < ...

The local time displayed by Moment JS is not accurate and matches the time stored in the database

Is there a solution to displaying the createdAt field date based on the local timezone? I've attempted to implement it, but it only seems to work for localhost. When I upload it to the server, it displays the createdAt field data in UTC format. Below ...

Accordion feature exclusively toggles the initial item

I am having an issue with the code in my index.php file. Only the first item in the accordion menu is showing up correctly. When I click on "Status" or "Repeated", I expect to see the sub-menu of the clicked item, but instead, I am getting the result from ...

use ajax to selectively update specific element

Seeking assistance with updating the cart page using ajax when adjusting the quantity of products. The view logic seems to be functioning correctly. The issue arises when targeting the class "ajax_updater". Upon clicking the quantity buttons, ajax succes ...

An alternative to PHP's exec function in Node.js

I am interested in developing a piece of software using C (such as prime number factorization) and hosting it on my web server with Node.js. Following that, I would like to create an HTML document containing a form and a button. Upon clicking the button, ...

Issue: NS_ERROR_FAILURE encountered in Firefox when using getBBox()

Is there a way to use the method getBBox() in SVG to retrieve the width and height of an element? I have included my code below, which works in Chrome but not in Firefox. If anyone has a solution to this issue, please let me know. try { console.log( ...

Delete the selected background color from custom drop-down lists in versions of Internet Explorer 9 and earlier

In my asp.net application, I have a dropdown element. I am trying to remove the default blue background color that appears when the dropdown is selected, as shown in the image below. I have tried all the solutions provided in this link, but none of them s ...

sending object value to directive rather than other data variables

I have been attempting to customize the functionality of this functioning http://jsfiddle.net/markcoleman/JNqqU/. In the current fiddle, the object is directly assigned where I am trying to change it to $scope.obj.items. However, passing the object to the ...

Utilizing auto-generated Nonce for Content Security Policy in a Node.js/Express web application

I've exhausted all possible solutions in my attempt to create a nonce and pass it successfully to the CSP and inline scripts with the nonce variable. Despite reading numerous articles, the guidance on accomplishing this task remains vague. Fortunately ...

How can I sync changes between two variables in node.js?

Is there a method to create a shared variable in JavaScript? Here is an example of what I am attempting to achieve: var id = 5; var player = new Player(id); var array1[0] = player; var array2[0] = player; array1[0].id = 8 console.log(array1[0]); // ...

Directing JSON POST Request Data to View/Controller in a Node.js Application

Currently, I am working on a project hosted on a local server at http://localhost:3000/. This server receives a post request from another server in the following manner: return requestLib.post({ url: 'http://localhost:3000/test', timeout ...

What are the steps to make React JSX direct to "/profile" and display the profile page?

Throughout my application, I have integrated reach router to facilitate navigation between the different pages. However, I came across a navbar component that I really like and decided to add it to my app. Strangely, clicking on the "to: "/profi ...

Error message in Next.js: Wavesurfer.js encountering an issue - unable to access properties of null (specifically, 'isPaused')

I've encountered an intermittent error while working with controls on my audio player using wavesurfer.js. Below is the error I'm facing: https://i.sstatic.net/u4w8t.png It seems like the error might be related to how I imported wavesurfer in ...