Is there an easy method to reposition the rotation pivot of an Object3D in Three.js?

Is there a straightforward method to change the rotation pivot of THREE.Object3D? By default, the pivot point is the position point of the object.

I have a group (implemented as THREE.Object3D) of objects and I want to rotate all objects together around a specific THREE.Vector3 point that can be adjusted during runtime.

It's important to note that the position of the group cannot be altered.

I attempted the following solution (with assistance from Derte Trdelnik), however, it did not yield the desired outcome:

someButton.onClick(function () {

    var object = editor.selected;
    if (!object instanceof THREE.Object3D) return;

    var pivot = new THREE.Vector3(100, 0, 0);
    var vectorToPivot = object.position.sub(pivot);
    var moveToPivot = new THREE.Matrix4().makeTranslation(vectorToPivot.x,
                                                          vectorToPivot.y, vectorToPivot.z);
    var rotation = new THREE.Matrix4().makeRotationZ(Math.PI);
    var inverseMove = new THREE.Matrix4().makeTranslation(-vectorToPivot.x,-vectorToPivot.y, -vectorToPivot.z);

    var matrix = inverseMove.clone().multiply(rotation).multiply(moveToPivot);
    object.applyMatrix(matrix);

    editor.signals.objectChanged.dispatch(object);

});

Thank you in advance for any guidance!

Answer №1

Instead of adjusting the position, simply construct a matrix that achieves your desired outcome :

const pivotVector = object.position.subtract(pivotPoint);
const moveToPivot = new THREE.Matrix4().makeTranslation(pivotVector.x, 
                                                        pivotVector.y, pivotVector.z);
const rotationMatrix = new THREE.Matrix().makeRotationX(Math.PI/2);
const inverseMove = new THREE.Matrix().makeTranslation(-pivotVector.x,
                                                       -pivotVector.y, -pivotVector.z);

const finalMatrix = inverseMove.clone().multiply(rotationMatrix).multiply(moveToPivot);
object.applyMatrix(finalMatrix);

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

Pass the full path of the Dropzone file to the Rails controller

I'm currently working on extracting the complete file path from a dropped folder in Chrome's Dropzone. All file upload data is being transferred correctly, but I also need to capture the full path of the uploaded file. Despite my efforts, I&apos ...

What should we do to resolve the uncommon use of the 'this' es-lint error? Which rule should we disable?

Recently, I encountered an issue with my Nuxt setup that is configured with el-lint and prettier. Every time I try to use 'this' within my template, it throws up an error unexpectedly. 6:15 error Unexpected usage of 'this' vue/this ...

The Bootstrap Modal consistently shows the contents of the first record when used within a SQL While Statement

As a newcomer, I am in the process of converting an operational Bootstrap modal that houses a post form. Initially, the modal would open using a button with a specific Id and would always display the FORM contents from the first record found in the SQL w ...

To include a Material Icon in your React-Toastify notification

This is an example of code located in a specific folder. While trying to incorporate a material Icon, an error has been encountered. 'React' must be in scope when using JSX import { toast } from 'react-toastify'; import ErrorIcon from ...

What is the best way to eliminate a div upon component dismount in React?

Is there a way to remove a div element on component unmount using react? I created a div with the id portal in the useCallback method. Now, I want to remove it when the component unmounts. Can anyone guide me on how to achieve this? Below you can see the ...

Selecting the content within a table to easily edit it

I am working on a project where I have a table filled with data fetched from a database. My goal is to allow users to click on a cell in the table, make changes to its content, and then save those changes back to the database. Below is the code snippet I a ...

Utilize the "incorporate" feature to include any string within an array

I am currently working on improving the search function in my application. This particular search function takes input from a search bar and is designed to handle multiple search terms. For example, it should be able to handle queries like "javascript reac ...

Is there a way to allow only the block code to shift while keeping the other span tags stationary?

Is it possible to change the text without affecting other span tags in the code? I want to make sure only this specific text is updated. How can I achieve that? var para_values = [ { content: "BRAND " }, { content: "MISSION" } ]; functi ...

Button activated on second click

As I am working on a project, I encountered an issue with two buttons and one input field. The input field is supposed to take the value of the clicked button, but currently, the function works only after the second click on the button. I have tried using ...

Looking to enhance code readability using regular expressions

While I am not a programmer, I enjoy exploring and learning new things. Here is what I have: 1) My URL is structured like this: http://site.com/#!/show/me/stuff/1-12/ 2) I have a jQuery pagination script that displays the number of available pages. Each ...

What is the reason behind the cross-origin error thrown by JSON.parse?

When I don't use JSON.parse, my code works perfectly fine. However, as soon as I attempt to parse or stringify my data object, a cross-origin error is thrown. Why is this occurring and what steps can I take to resolve it? The snippet of code in Title ...

Provide users with the option to select a specific destination for saving their file

In the midst of my spring MVC project, I find myself in need of implementing a file path chooser for users. The goal is to allow users to select a specific location where they can save their files, such as C:\testlocation\sublocation... Despite r ...

Achieving smooth transitions with CSS3 when removing a class

I've been attempting to implement animation transitions on a removed class, but unfortunately it's not working as expected. I referenced this link: CSS transition when class removed I have tried setting the transition on the parent element and o ...

RectAreaLight causing low frame rates in three.js

In my futuristic setting, I am incorporating RectAreaLights to mimic strip lights. However, I am encountering a performance issue as my frames per second (fps) drop significantly when using these lights, typically not exceeding 20 or 30. On the other hand, ...

Having trouble selecting an element by name that contains a specific string followed by [..] using jQuery

I have elements with names like kra[0][category], kra[1][category], and so on. However, I am facing difficulties in selecting these elements by name. Here is my jQuery code: $("[name=kra[0][category]]").prop("disabled", true); ...

Error: Unable to assign value to the 'props' property of an undefined object

Currently, I am in the process of setting up server side rendering with react. However, every time I try to access the route where I want server side rendering to work, I encounter the error TypeError: Cannot set property 'props' of undefined. T ...

Error encountered while running dev in Next.js: Unexpected JSON token 'u' at position 0

Recently, I decided to dive into learning Next.js and sought out resources like the Youtube Video Next.js Crash Course by Traversy Media and the Next.js Official Documentation for guidance. Following the tutorials, I added the simple code snippet below to ...

Using Axios to fetch data and populating select dropdown options in a Vue component

I am currently working on integrating the response data values with my multiselect options. Although the console log displays the returned results, I'm facing difficulty in connecting them to my multiselect options. When I enter 'Day' into ...

Is it permissible for me to incorporate a package from the dependencies listed in the package-lock.json file into my

I'm looking to incorporate date-fns into my project. I currently have react-datepicker, which also uses date-fns. Can I simply utilize date-fns from react-datepicker, or do I need to install it separately in my project? ...

The website was accessed via HTTPS, but an insecure XMLHttpRequest endpoint from the same website was requested

My axios request below is going out over https, but I'm encountering a specific error: The page at 'http://websitename/folder1/folder2/api/atlas/home/coach' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http: ...