Transform Euler Angles into Vectors by utilizing Euler's "setFromVector3" method

Given three Euler angles x, y, z in radians, I am looking to convert them into a Vector location X, Y, Z with the center as the origin.

If it is possible to utilize https://threejs.org/docs/#api/en/math/Euler.toVector3 to obtain the Vector, I would appreciate knowing how. Alternatively, any mathematical (sin/cos) solution is also welcome.

In this code snippet, axesHelper represents the angles while the cube should be positioned based on the Euler angles. Use Dat gui for real-time editing of the rotations.

// Adding Axis to represent Euler
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);

// Adding cube to represent Vector
const geometry = new THREE.BoxGeometry(0.1, 0.1, 0.1);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);

scene.add(cube);

render();

const gui = new GUI();
const angles = {
    degX: 0,
    degY: 0,
    degZ: 0,
}

gui.add(angles, 'degX', 0, 360, 1).onChange(function() {
    axesHelper.rotation.x = THREE.MathUtils.degToRad(angles.degX);
    render();
    updateEULtoAngle();
});
gui.add(angles, 'degY', 0, 360, 1).onChange(function() {
    axesHelper.rotation.y = THREE.MathUtils.degToRad(angles.degY);
    render();
    updateEULtoAngle();
});
gui.add(angles, 'degZ', 0, 360, 1).onChange(function() {
    axesHelper.rotation.z = THREE.MathUtils.degToRad(angles.degZ);
    render();
    updateEULtoAngle();
});

console.log(THREE.MathUtils.radToDeg(axesHelper.rotation.x));
console.log(THREE.MathUtils.radToDeg(axesHelper.rotation.y));
console.log(THREE.MathUtils.radToDeg(axesHelper.rotation.z));

function updateEULtoAngle() {
    let eul = new THREE.Euler(
        THREE.MathUtils.degToRad(angles.degX),
        THREE.MathUtils.degToRad(angles.degY),
        THREE.MathUtils.degToRad(angles.degZ)
    );
    let vec = new THREE.Vector3();
    eul.toVector3(vec);
    console.log(eul,vec);
    cube.position.copy(vec);
}

Visual representation: Cube following the Y axis

Related question: encountering issues with axis alignment - How to convert Euler angles to directional vector?

Answer №1

Euler.toVector3() may not provide the functionality you are seeking. It simply transfers the x, y, and z angles into the corresponding vector components.

You might want to explore THREE.Spherical, which offers a way to work with spherical coordinates. By using two angles (phi and theta) and a radius, you can define a point in 3D space. Subsequently, these values can be utilized to create a Vector3 using either Vector3.setFromSpherical() or Vector3.setFromSphericalCoords().

Answer №2

It's all a bit confusing to me, but

I found success with the following line of code: let vector = new THREE.Vector3(0, 0, 1).applyEuler(eulerAngles)

This solution worked for my situation.

If you're interested, you can also take a look at: https://github.com/mrdoob/three.js/issues/1606

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

Utilizing React with Firebase involves executing several Firebase requests simultaneously and ensuring the completion of all promises

I'm facing a challenge with the current code where it hits Firebase's 10 item limit in an array. My goal is to iterate through all teamIds and execute a Firebase query for each individual teamId. However, I'm unsure how to ensure that the ex ...

Various behaviors in multiple instances of DatePicker

Currently, I have a form with two datePicker elements (using jQuery UI). When I hover over a date in the first datePicker, an AJAX call is made successfully. However, the issue is that the AJAX call is triggered for both datePickers when I only want it to ...

The child element is responsible for retrieving data that needs to be transmitted to the parent component

Within my react component, I am successfully retrieving data from a graphQL server using react-apollo. However, this component functions as a subordinate element and I am seeking a way to pass the graphQL data up to a parent component. Can this be achieved ...

The challenge of executing JavaScript in the correct order

I am facing an issue where 5 always prints before 4 in my code snippet below. I expected the callback to postUsers within a return statement from matchAgainstAD to wait for the completion of the for loop and ad lookup before returning. What is the simple ...

Tips on identifying when the user is silent and automatically play the following audio file using Html5 and Javascript

My goal is to create a project similar to AI using an array of audio files. I am utilizing the annyang API for voice commands to initiate the program. Once the first audio file finishes playing, the program pauses and waits for me to speak. If there is no ...

Is there a method to customize the scrolling speed of VueRouter?

How can I implement smooth scrolling for router links using VueRouter? <router-link :to="{ hash: 'home' }">Home</router-link> <router-link :to="{ hash: 'about' }">About</router-link> Here ...

After attempting to implement immutable js with Redux, my state mysteriously vanished into the ether

Starting my development journey with reactjs and redux, I thought it would be beneficial to incorporate immutable.js into my workflow while using redux. However, things took a turn for the worse. Everything seemed to crash, making me wonder if I needed mo ...

Swap out the div block with a new one

I am currently facing an issue with my JavaScript code. The code is supposed to remove block1 and replace it with block2 when an onclick function is triggered. function buyerclick() { div = document.getElementById('block2'); div.style.displa ...

What is the best method for incorporating the current time and total time into a personalized audio player?

I am currently working on incorporating a current time / total time element into my project. I envision it looking something like this: 01:11/03:00. At the moment, I have already implemented the progress bar and play/pause button. Here are the variables I ...

Can a website be saved entirely, including the initial HTML page, and accessed without an internet connection?

Our unique website utilizes AJAX technology to communicate with the server without making additional page requests after the initial setup. This allows users to seamlessly switch between online and offline modes during a session, automatically synchronizin ...

After my jQuery functions are executed, my CSS rules are loaded

Struggling with the communication between CSS and jQuery, I find myself torn. In CSS, my rules are often very specific, like this: div#container > div#contentLeft { //Code here } However, when I add jQuery to spice up my site, the CSS rules seem to ...

Strategies for Dealing with 'No Search Results' in Your Search Functionality

I am currently facing an issue with displaying "No Results Found" when a user utilizes my search feature. The current problem is that "No Results Found" appears immediately on the screen and then disappears while a search query is being processed, only to ...

Transfer the values of two divs into a unified output div

I am working on a task that involves two text boxes, one with an id and the other with a class. The goal is to copy the values from both text boxes into a single div when a button is clicked. So far, I have made an attempt at this but only the value from t ...

unable to retrieve the response from a POST request sent to a node server

Currently in the process of learning how to utilize node and express, I have embarked on creating a sample website that incorporates both along with the Google translate API. The goal is to explore the numerous features offered by these technologies. Unfor ...

The raycaster intersection with sprites in Three.js seems to be skewed to the left

I have a unique setup where sprites with text are arranged in a spherical pattern on the screen. I would like to enable users to click on individual words and highlight them. However, there is an issue with raycasting and raycaster.intersectObjects(). It ...

Ways to extract a specific value from a geojson object using the key name

After making a call to the back-end, I retrieved the below geojson data in the 'data' object. However, when trying to access the values of the 'type' and 'features' keys within the geojson, I encountered difficulties. data["g ...

Executing Python and JavaScript with Selenium

I am encountering an issue while trying to run this code. The error message indicates that I may be missing a closing ) or using an illegal character. It seems like there might be confusion with inserting arguments where they are not needed. Could someon ...

Navigating through different components in React is made possible with React Router

I have views in my application that depend on each other. For example, in one view a user can choose an item from a list (generated on the server), and in the next view they can perform operations on that selected item. The item is passed to the second v ...

Why isn't this code performing well when trying to alter the styling of DOM elements?

JavaScript is causing major issues and is performing poorly. Why is this code not working properly?? (this is a back to top button) function checkScrollTop(){ if (document.body.scrollTop > 300) { document.getElementById("backToTop").style.dis ...

Error message "Render function must return a valid value" occurs when utilizing buttons in mapped details

I am currently working on a project where I fetch data from the backend and display it on the frontend by mapping it out. Everything is functioning well, but when I try to add a button with some functionality (along with binding it and creating a handler) ...