Passing on rotational values from a parent Object3D to its child - the magic of three.js

Currently, I am working with a scenario where a Mesh is nested within an Object3D. The goal is to rotate the Object3D on the x and y axes while dragging it, and then reset the rotation of the Object3D to 0, 0, 0 upon release, transferring its rotation to the nested Mesh.

Although my approach seems to work initially, it breaks after the first movement. I suspect that the issue might be related to the Euler order.

The code snippet I am using is as follows:

function mouseDown(e) {
    hold.x = e.pageX;
    hold.y = e.pageY;
    window.addEventListener('mousemove', mouseMove);
}

function mouseMove(e) {
    var diffX = e.pageX - hold.x;
    var diffY = e.pageY - hold.y;

    object.rotation.x = (diffY * 0.25) * RADIAN;
    object.rotation.y = (diffX * 0.25) * RADIAN;
}

function mouseUp() {
    window.removeEventListener('mousemove', mouseMove);

    cube.rotation.x += object.rotation.x;
    cube.rotation.y += object.rotation.y;

    object.rotation.x = 0;
    object.rotation.y = 0;
}

I have created a jsfiddle demonstrating this issue: http://jsfiddle.net/BJaju/4/

Answer №1

In order to transfer the parent's global transformation to the child object, you must execute the following command:

child.applyMatrix( parent.matrixWorld );

For your specific scenario, it should look like this:

function mouseUp() {

    window.removeEventListener('mousemove', mouseMove);

    cube.applyMatrix( object.matrixWorld );

    object.rotation.x = 0;
    object.rotation.y = 0;

}

You can view the working example here: http://jsfiddle.net/BJaju/5/

This code snippet was tested with three.js version r.59

Answer №2

Utilizing Quaternions was the key to solving my problem!

I defined two distinct transforms as follows:

baseQuaternion = (new THREE.Quaternion).setFromEuler(new THREE.Euler(-Math.PI / 2, 0, -Math.PI / 2 , 'XYZ'))

Combining them through multiplication allowed for a seamless merging of effects.

myMesh.quaternion = baseQuaternion.clone().multiply((new THREE.Quaternion).setFromEuler(...

This instructional video proved to be extremely helpful:

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

Tips on preventing hardcoded, chained asynchronous functions in JavaScript/jQuery

In my program, almost all functions involve asynchronous calls that rely on the results of previous functions. To manage this, I've hard-coded each subsequent function call within the success callback of the preceding one: function getStuff() { $ ...

Unable to modify the title property of a link

Currently, I am in the process of transforming my website to support multiple languages. To achieve this, I have implemented a function using jQuery that dynamically changes text when a language button is clicked. While this method has been successful for ...

Partially extended texture in Three.js

Currently, I am using the Collada loader in Three.js r65 to load my 3D object. Upon loading, I apply a texture to all parts of the model using the following code snippet. var loader = new THREE.ColladaLoader(); loader.options.convertUpAxis = true; loader ...

Navigating through nested promises can be a daunting task within the world of JavaScript and Angular

Function 2 relies on the return of Function 1, while Function 3 uses both returns. How can I clean up this process? Currently, Function 3 is only giving me undefined values. Below are my 3 functions: Function1 $scope.nombreCompetencesATraiter = function ...

The usage of nextTick in Vue.js and its role in updating components

Although I am a beginner with vue.js and have a basic understanding of it, I came across a sample code utilizing nextTick() today. Trying to comprehend its purpose led me to explore the documentation, which ended up complicating things further and leavin ...

Can arrays be incorporated into HTML scripts using AngularJS?

Below is the HTML code snippet I am working on: <script> Raven.config('___PUBLIC_DSN___', { release: '1.3.0', whitelistUrls: {{urls}}, }).install() </script> This is the controller code snippet: $ ...

Tips for inserting information from a JSON file into a mailto hyperlink

As a newcomer to JavaScript, I am eager to tackle the challenge presented below: Situation: In possession of a JSON file containing personal details such as name, email address, bio, etc., my task is to design a basic web page showcasing this data for ea ...

Is there a way for me to verify if a number is represented in exponential form?

Is there a way to determine if a number is in exponential form? I encountered a situation in my code where normal integers are being converted to exponential notation when adding or multiplying them. For instance, performing the operation 10000000*1000000 ...

The initial render in a Kanban board seems to be causing issues with the functionality of react-beautiful-dnd

I recently integrated a Kanban board into my Next.js and TypeScript project. While everything seemed to be working fine, I encountered a minor glitch during the initial rendering. Interestingly, when I refreshed the page, the drag and drop functionality st ...

The issue with Extjs store.proxy.extraParams being undefined appears to only occur in Internet Explorer

I currently have an ExtJs store set up with specific configurations. var fieldsStore = new Ext.create('Ext.data.Store', { model : 'FieldsModel', proxy : { type : 'ajax', url : 'queryBuilder_getQueryDetails', ...

React form not detecting the Enter key press in onSubmit event

Currently, I am in the process of working on a form that includes a tag input feature. The issue I am facing is that when a user enters a tag and presses enter, it not only adds the tag to a specific array but also submits the form. While I can use the e.p ...

What is the method for adding 24 hours to a 12-hour timestamp while preserving the AM and PM designation?

I have created the following code to display real-time, but I am struggling with adding a timestamp that switches from 24-hour format to 12-hour format with AM and PM. setInterval(function() { var date = new Date(); var hours = date.getHours(); va ...

Is it possible to transfer a value when navigating to the next component using this.props.history.push("/next Component")?

Is there a way I can pass the Task_id to the ShowRecommendation.js component? recommend = Task_id => { this.props.history.push("/ShowRecommendation"); }; Any suggestions on how to achieve this? ...

What is it about that that ticks off so many different boxes?

Hey there! I've been working on creating checkboxes within next js that iterate through each filter and its options. Fortunately, I managed to get it up and running smoothly. However, I'm encountering an issue where checking one option in a speci ...

Incorporate 'Additional features' into the Navbar when adjusting window size

When the window is resized, I want to display a collapsed 'More options' button that will collapse all hidden <li> elements. Here is an example: <li id="menu_more_container" class="dropdown" style="display: none; ...

Making HTTP requests to the Gmail API

Today, I dedicated most of my time to learning how to utilize the Gmail API with node.js. Following Google's QuickStart guide, I successfully got the API up and running fairly quickly. It seemed like there were two methods for making API requests. One ...

Retrieve JSON data from an external link and showcase it within a div, unfortunately encountering an issue with the functionality

Encountering the error message "XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access" Check out the plunker link for more information: http ...

Comparison between the A-Frame camera and the three.js camera in handling a scene packed with numerous elements

For a stress test, check out this CodePen that explores 3 different scenarios: https://codepen.io/ubermario/pen/QqWQPa ... this.el.setObject3D(objName, cubeObj); // Register to A-Frame; can be accessed with this.el.getObject3D(objName) ... Navigat ...

Occasionally, the script tags in Next.js fail to load

https://i.stack.imgur.com/tSrIu.png An error message appears when the script tag fails to load. I have a total of 6 script tags in my code. These scripts are present in my code, but they do not consistently load. However, I can see them when ins ...

What is the best way to prevent a directory from being included in the Webpack bundle?

Issue: Despite configuring my Webpack settings in webpack.config.js to exclude files from the ./src/Portfolio directory, all files are being bundled by Webpack. Code Snippet: Webpack.config.js const path = require('path'); module.exports = { ...