Align the rotation of Object3D at the tab level

Seeking assistance,

I am attempting to synchronize the rotation of an object loaded in the active browser tab with another object loaded in a second tab. The idea is that when the Object in the active browser tab rotates, it will send a message to the Object in the second tab which will listen and rotate accordingly based on the received values. Below is my code snippet (I referenced this example):

 let objectLoaded;
 let previousMousePosition = {x : 0,  y : 0 };
    function mouseDown(e) {
        isDragging = true;
    }

    function mouseUp() {
        isDragging = false;
        if (objectLoaded) {
            // Sending rotation information to the second tab using 'hermes' package.
            hermes.send('rotation', JSON.stringify({
                rotation: objectLoaded.rotation,
                quaternion: objectLoaded.getWorldQuaternion(),
                matrix: objectLoaded.matrixWorld
            })); 
        }
    }

    function mouseMove(e) {
        const deltaMove = { x : e.offsetX - previousMousePosition.x,y : e.offsetY - previousMousePosition.y };         
        if (isDragging && objectLoaded) {
            const deltaRotationQuaternion = new THREE.Quaternion().setFromEuler(new THREE.Euler(toRadians(deltaMove.y * 1), toRadians(deltaMove.x * 1), 0, 'XYZ'));
            objectLoaded.quaternion.multiplyQuaternions(deltaRotationQuaternion, objectLoaded.quaternion);
        }
        previousMousePosition = {x : e.offsetX,  y : e.offsetY };
    }

    function toRadians(angle) {
         return angle * (Math.PI / 180);
    }

Listener code,

  hermes.on('rotation', (rotationObj) => {
  // Structure of rotationObj: {rotation: { _x: 0, _y: 0: _z: 0}, quaternion: { _x: 0, _y: 0: _z: 0, _w: 0}}
      const target = JSON.parse(rotationObj);
      SecondObject.rotateX(target.rotation._x);
      SecondObject.rotateY(target.rotation._y);
      SecondObject.rotateZ(target.rotation._z);
 })

The main problem I am facing is that the second object does not sync properly with the first one. Can anyone point out what might be going wrong here?

Appreciate any guidance,

Answer №1

On one tab, you are requesting the current (absolute) rotation. On the other tab, you are rotating by those values in addition to the current rotation. This method will no longer work after the first time you set a non-zero rotation.

The correct approach is to assign the rotation by using

SecondObject.rotation.copy(target.rotation);

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

Encountering difficulties accessing the array in the controller through ajax

Having trouble receiving an array of data from AJAX to the controller. $.ajax({ type: "POST", url: "/Home/List", traditional: true, contentType: 'application/json', data: { "Query&quo ...

Using THREE.TransformControls to resize tiny items

I have a basic editor for 3D models using three.js. Here is the camera setup: new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 0.01, 10); And this is the geometry of my mesh: new THREE.BoxGeometry(0.3, 0.3, 0.3); To scale the me ...

Having trouble with Raphael's animation callback function?

It seems like I may not be using the callback feature correctly because when I run the code below, the "testCircle" doesn't animate before it disappears. var paper = Raphael(0, 0, 1280,600); var testCircle = paper.circle(300, 300, 50); test ...

successive ajax requests

I am facing a challenge where I need to execute two separate ajax calls sequentially. The second call relies on the result of the first call for its data. Despite my efforts, I haven't been able to achieve the desired outcome. Here's what I have ...

Combine two arrays of objects and merge properties using the Ramda library

I have two arrays as shown below: ['TAG.u', 'TAG.c'] and the other one is: [{name:'some',key:'TAG.u'}, {name:'some new', key: 'TAG.b'}, {name:'some another' , key:'TAG.c'} ...

Encountering a npm error E404 when trying to install unicons package for React development

I recently started working on a weather app project using create-react-app and encountered an issue while trying to install unicons for the project. Despite attempting a few solutions, I was unable to resolve the problem. Here is the command I used for th ...

Receiving an item in place of a true/false indicator

I wrote an asynchronous function that validates the permission status on a device. Here is the function: checkPermission = async () => { const allowed = await requestNotifications(['alert', 'sound']).then((res) => { if ...

react validation for dropdown, react-datepicker, and hour input at intermittent intervals

I have integrated the following packages into my project :- react-datepicker library for selecting from time and to time date validation with date-fns, moment.js, additional validations using jQuery and lodash libraries/packages If you want to view my pr ...

Guide to Implementing i18n-iso-countries Library in React

I am currently developing a React application and attempting to utilize the i18n-iso-countries package to retrieve a countries object in English where keys represent iso codes and values represent country names. This process is straightforward in Node.js, ...

Is there a way to accomplish this without using settimeout?

Whenever the Like button is pressed, I want to trigger the loading process I expect to see LOAD_POST_SUCCESS immediately after LIKE_POST_SUCCESS Pressing the Like button should initiate the load process After LIKE_POST_SUCESS, I want to see LOAD_POST_SU ...

Encountered an npm ERR while executing the React project

I'm encountering an issue with running my React project. When I use the command "npx start", I receive the following error message. Can someone please assist me with this? npm ERR! could not determine executable to run npm ERR! A detailed log of thi ...

Setting the default value to null or 0 in a Select dropdown in Angular 7

There is a select dropdown code that may be hidden in some instances. When the select dropdown is hidden, I want to ensure null or 0 is sent instead of an empty value when saving the form. How can I achieve this? <div class="col-md-4" [hidden]="!cpSe ...

Instructions for transforming rows into columns in JSON format

Looking to convert an array of JSON objects into a format where rows become columns and the values at the side become column values, similar to the crosstab function in PostgreSQL. The JSON data looks something like this: {"marketcode":"01","size":"8,5", ...

Choose or deselect images from a selection

I am currently working on a feature for an album creation tool where users can select photos from a pool of images and assign them to a specific folder. However, I'm facing difficulty in selecting individual photos and applying customized attributes t ...

Encountering an ENOENT error while attempting to incorporate a style guide into next.js (react)

Recently, I delved into learning next.js and decided to enhance my project with documentation using To kickstart my project, I utilized npx create-next-app After installation and configuration setup, I added the following code snippet: [styleguide.config ...

Utilizing Node gRPC for seamless transmission of server metadata to clients without any issues

When working from the client side, adding metadata for the server is a simple process: const meta = new grpc.Metadata(); meta.add('xyz', 'okay'); stub.service.Rpc(request, meta, (err, response) => { }); To access the above on the ...

Combining two arrays containing objects in JavaScript

Imagine having 2 arrays of objects that you want to merge in a parallel manner. For instance var array = [{foo: 2} , {boo: 3}] var array2 = [{foo2: 2}, {boo2: 4}] The outcome would be var array3 = [{foo:2,foo2:2},{boo:3,boo2:4}] In what way can this be ...

Interactions between JavaScript and PHP scripts within a web application

THE SCENARIO In the midst of creating a web application that dynamically loads content by fetching data from a MongoDB database where items and their respective authors are stored in separate collections within the same database. The author's ID is s ...

Creating the jquery/javascript code needed to produce an event or alert similar to the "Confirm Navigation" prompt used on sites like Stack Overflow

Similar Question: How can I have a confirmation message when navigating away from a page after making changes? I've noticed an interesting feature on Stackoverflow: when you start writing a new question and attempt to leave the page, it prompts y ...

Click event on Angular leaflet marker

Currently, I am using leaflet in conjunction with Angular and have a query regarding making a button clickable within a message popup. Although I understand that I need to compile the HTML, I am struggling to implement it successfully as there are no examp ...