What is the best way to rotate a ThreeJS Object3D object around its endpoint?

Can an Object3D object be rotated around its end point? I located the center of my object using

const boundingBox = new THREE.Box3().setFromObject(this.object)
. Would rotating at a specific point involve moving the object to that point, rotating it, and then moving it back?

Answer №1

Can an Object3D object be rotated around its end?

Absolutely. By default, an Object3D rotates around its center, but it is possible to change the rotation point by enclosing the object in a parent (Object3D) and setting the position for the parent which will then become the new rotation point:

function redefineRotationPoint(obj, scene, position) {
  // create a parent object and add it to the scene
  const parent = new THREE.Object3D();

  parent.add(obj);
  if (obj.parent) {
    obj.parent.add(parent);
  } else {
    // add to the THREE scene if obj has no parent
    scene.add(parent);
  }

  // set the position for rotation, for example: { x: 0.1, y: 0.1, z: 0 }
  parent.position.set(position.x, position.y, position.z);

  // adjust the object's position so only the rotation point changes
  const x = obj.position.x - position.x;
  const y = obj.position.y - position.y;
  const z = obj.position.z - position.z;
  obj.position.set(x, y, z);
}

// invoke the function
redefineRotationPoint(this.object, scene, { x: 0.1, y: 0.1, z: 0 });

// rotate the object
this.object.parent.rotation.set(0.1, 0.2, 0.3);

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

The error encountered reads as "Uncaught TypeError: Unable to destructure the property 'basename' from 'React2.useContext(...)' as it is null."

Currently experiencing the following issue: Uncaught TypeError: Cannot destructure property 'basename' of 'React2.useContext(...)' as it is null. Specifically in the Link component from this code snippet: import React, { useEffect, u ...

Having trouble running tests on the Express.js server

I'm struggling to run basic tests on my expressjs server and close it immediately. I have exported the server as a promise, but can't seem to figure out how to achieve this. Below is the code for my server file : index.js const config = require( ...

After invoking call(), the object becomes 'this' on a worldwide scope

I have a list containing the names of different methods. var methodList = ['method1', 'method2', 'method3', 'method4']; I dynamically select a method based on certain criteria. These methods are part of a larger cl ...

Having trouble with the automatic closing feature of Bootstrap drop down in React?

While using the drop button feature of Bootstrap, I have encountered a situation where it works fine when clicked on, but disabling it by clicking outside using autoClose='outside' does not seem to work as expected. When I click on my hamburge ...

verifying the presence of a specific key within a dictionary in Python

Check out the javascript code snippet: const isValid = function (area, row, col, num) { if (area[num] || row[num] || col[num]) { return false; } else { return true; } }; I attempted to convert this into python but encountered some difficulti ...

Display a single item from the Bootstrap carousel on mobile devices

Seeking help with my Bootstrap Carousel - displaying 2 items on Desktop but wanting to show one item at a time on Mobile. Any solutions? Visit this link for more info HTML Code: <div class="container"> <div id="myCarousel" class="carousel sli ...

Learning about the intricacies of backend Node.js through Angular using GET requests

I am having trouble retrieving the query parameters from a frontend GET request on the backend side. I have attempted to use url and query, but still need assistance fetching the query on the nodejs side. Can someone recommend a method that would allow me ...

Listening to events on the iterative variable of NgFor directive in Angular 2

Angular2 has been my latest exploration in solving a unique data binding challenge. In my UI, I've presented a javascript array of objects like a database recordset in an HTML table. Each row contains menus and inputs allowing users to modify the rec ...

Press the identical button to return to the initial position with the help of jQuery

1) I am currently working on a vertical menu layout and have successfully placed an image in the top right corner that allows users to adjust the width of the menu. However, I am facing an issue where I have to click on the same image to restore the menu t ...

Unable to append to the array

I am currently experimenting with using React to create a sorting visualizer. I am encountering an issue where the array does not update in line 31 for some unknown reason. Strangely, when I check the console log, the updated array is being logged. impo ...

Mastering jQuery ajax in Google Chrome Extensions

I have developed a script to fetch data from an external server using JSONP request in jQuery. Please take a look at the code snippet below: $("#submit").click(function() { var state = $("#state").val(); var city = $("#city").val(); $.ajax({ ...

Navigating through the API routes in Express

Within my node API, I have set up two middlewares: app.use('/api', apiRouter); app.use('/*', express.static('public')); The first middleware serves the API endpoints (e.g. /api/users - which returns all users without ent ...

How do you trim a string and display the final 3 characters?

When dealing with a list of objects, I want to ensure that the chain of tasks does not become too long and break the table or appear aesthetically unpleasing. Therefore, my goal is to trim the tasks and display only the last 3. In the image below, multiple ...

Transferring information from ng-Dialog HTML to a controller

Having some issues with ng-Dialog. When I include the ngDialog controller option, it works. I can retrieve the value of $scope.descriptionText from <p>Description:</p> <textarea ng-model="descriptionText"></textarea> Now, whe ...

The states of both components are only being updated once the second event call is triggered within React

I am currently working with 2 functions in my project. One function is triggered when I type something into the search input, while the other one is called upon selecting a category. The initial values for the search and selectedCategories variables are an ...

Discovering a value that aligns with one of the values in an array using Javascript

Just a heads up: this quiz is super simple and only has one input field for each question. This block of Javascript code is used to check if the answer entered in the input field is correct or not. In this case, it checks if the answer entered is 'en ...

Is there a way to remove a link to an image that pulls data from another website?

On my HTML page, I have implemented the following code to display weather data: <!-- Begin Weather Data Code --> <div style="display:none;"> <a href="http://iushop.ir"> <h1>Weather</h1> </a> </div> < ...

The position of a Three.js child mesh is consistently showing as (0,0,0)

After loading objects using OBJLoader, I discovered that the loaded object contains a parent and multiple children. I then used Raycaster to identify the clicked child object. However, upon trying to update the position of the child object, I found that t ...

Determine total number of covid-19 cases with JavaScript

I am looking to incorporate a real-time COVID-19 total cases tracker for Russia on my website. In order to achieve this, I am sending a request to using the following script: function httpGet(theUrl) { var xmlHttp = new XMLHttpRequest(); xmlHttp.o ...

React component utilizes Material UI to detect scrolling within a table

Currently, my table's body size is set to fixed dimensions in accordance with Material UI guidelines. I am looking to implement a functionality that allows me to dynamically load more rows as the user scrolls through the table. Can you recommend the ...