Spinning 3D Grid in Global Coordinates - Using THREE.js

I'm struggling with rotating an object in my project. I am currently utilizing THREE.Shape to define a custom shape. After creating the shape, I convert it into a Mesh and set its position to:

buildingMesh.position.set(-70, -300, levelY);

However, due to the position of the mesh and the camera, it looks like the object is standing upright.

Here is how it appears: https://i.sstatic.net/XK8fy.png

I recently implemented an Orbital camera that rotates around the world axis. When the camera moves, the object looks like this:

https://i.sstatic.net/dXNtN.png

This is happening because the y axis was not configured when using the THREE.Shape. Now, I'm trying to find a way to rotate the object so it appears upright, as shown in the initial image. I have experimented with rotations on the x, y, and z axis but it always seems to rotate within the object's axis.

Do you have any suggestions?

Here's a method I attempted after finding it in another forum:

rotateAroundWorldAxis: function(object, axis, radians) {
        this.rotWorldMatrix = new THREE.Matrix4();
        this.rotWorldMatrix.makeRotationAxis(axis.normalize(), radians);
        this.rotWorldMatrix.multiply(object.matrix);        // pre-multiply
        object.matrix = this.rotWorldMatrix;
        object.rotation.setFromRotationMatrix(object.matrix);
    }

Answer №1

Experiment with:

object.rotate.x = Math.PI // to rotate the object 180 degrees around the x axis

Substitute 'object' with the name of the object you have created (remember you can also rotate around the y and z axes)

Answer №2

The main issue at hand was the mesh structure consisting of multiple layers. These layers needed to be consolidated into a single group within an Object3D. Once accomplished, I was able to easily execute the following rotation:

group.rotateOnAxis(axis,Math.pow(Math.PI, 2) / 2);

Hopefully, this explanation will be beneficial to others facing a similar challenge down the road.

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

Sorting files in jquery file upload

Has anyone had experience using the jQuery-File-Upload library from https://github.com/blueimp/jQuery-File-Upload? I'm currently facing an issue and wondering if anyone could assist me in sorting the files in a different manner. By default, this scrip ...

How to Employ Javascript Set in a Mongoose SchemaType?

I am interested in implementing Javascript's Set feature in ES2015 as a SchemaType in Mongoose to take advantage of its uniqueness. However, I have run into an issue where the Mongoose documentation does not explicitly support Set, suggesting the use ...

What is the method to retrieve just plain text (String) from an Ajax URL response?

I'm still learning how to use Ajax. My current project involves populating a dropdown based on the selected value from another dropdown in HTML. The issue I'm facing is that, while I am able to get the desired output, it includes unwanted HTML fo ...

The GET request on the Express route is malfunctioning, causing the Postman request to time out after getting stuck for some

My Express app seems to be experiencing some issues with the GET route. When making a request using Postman, the response gets stuck for a while before fetching. The GET route is properly set up with all necessary request parsers and the app initialized an ...

The array containing JSON objects enclosed within curly braces is causing a syntax error

Given a variable containing data that looks like an "array" with JSON Objects inside (even though it is not actually formatted as an array, starting and ending with curly braces): {"x1","x2"},{"y1","y2"},{"z1","z2"} How can I transform this so that the i ...

Showing a database table in an HTML format using JavaScript

I am currently working on displaying a database table in HTML. I have written a PHP code snippet which retrieves the table data and formats it into an HTML table without any errors: function viewPlane() { if(!$this->DBLogin()) { $ ...

Tips for optimizing large image files on a basic HTML, CSS, and JavaScript website to improve site speed and ensure optimal loading times

Currently, my site is live on Digital Ocean at this link: and you can find the GitHub code here: https://github.com/Omkarc284/SNsite1. While it functions well in development, issues arise when it's in production. My website contains heavy images, in ...

I am unable to comprehend the function definition

I have familiarity with different types of JavaScript function declarations such as expression functions and anonymous functions. However, I am unsure about the syntax used in these two specific functions: "manipulateData: function (input)" and "getDataByI ...

javascript Arabic speech synthesis

Attempting to utilize SpeechSynthesisUtterance for Arabic language Successfully functioning with English content $(document).ready(function() { var u1 = new SpeechSynthesisUtterance('Hello world!'); u1.lang = 'en-US'; u1.pitch ...

How does a web worker behave when the owner is not actively engaged?

[edit: I am using Chrome 49.0.2623.112 m (64-bit) with default settings and HTTPS, but the issue occurred before the switch.] My application utilizes web workers to handle AJAX polling. While this may not provide a significant performance boost, it does e ...

Javascript is not functioning properly when making an ajax call

Having a bit of trouble with JavaScript not working after an Ajax call. Everything functions normally until new data is fetched from the database or an Ajax call is made. The issue seems to be that the JavaScript doesn't load properly. Any help would ...

Obtaining the source code from a different domain website with the help of jQuery

Is there a way to extract part of the source code from a YouTube page without using server-side programming? I've tried cross-domain AJAX techniques like Yahoo YQL and JsonP. While Yahoo YQL allows me to grab part of the source code, I'm facing ...

Personalize headers in v-data-table while preserving default sorting capabilities

I'm looking to enhance my v-data-table by making the table headers "tab-able". To achieve this, I decided to create a slot and include tabindex on the columns. However, I encountered an issue where the sorting functionality stopped working. Does an ...

Tips for showing and modifying value in SelectField component in React Native

At the moment, I have two select fields for Language and Currency. Both of these fields are populated dynamically with values, but now I need to update the selected value upon changing it and pressing a button that triggers an onClick function to update th ...

Discover the myriad of possibilities created by combining arrays

I am working on a code snippet that aims to generate an array containing all possible combinations between two or more arrays. However, I am encountering a specific issue. getCombn(arr: string | any[], pre?: string | undefined) { pre = pre || ' &a ...

Issue encountered while uploading a file using Mongodb, Node.js, and AngularJS

I am encountering an issue when attempting to upload a file to a MongoDB database using AngularJS alongside a NodeJS REST API. Below is the code I have implemented but unfortunately, it is not functioning correctly. Here is an example of my schema: var s ...

Implement jQuery Tabs in Brackets software to enhance user experience

My Adobe Creative Cloud subscription is expiring soon, and I am considering switching to Brackets, an open-source code editor developed by Adobe. However, I am facing some difficulties adding jQuery tabs to my HTML documents. I downloaded the 1.10.4 zip f ...

Update the headers of the axios.create instance that is exported by Axios

I have a single api.js file that exports an instance of axios.create() by default: import axios from 'axios' import Cookies from 'js-cookie' const api = axios.create({ baseURL: process.env.VUE_APP_API_URL, timeout: 10000, headers ...

The process of AJAX polling a JSON-returning URL using jQuery's $.ajax() method does not appear to provide up-to-date responses

I am currently working on a project that involves polling a specific URL for a JSON response using AJAX. The initial AJAX request alerts the server of my need for JSON content, prompting it to start building and caching the response. Subsequent AJAX reques ...

Use the useEffect hook to pass newly updated data to a FlatList component

I have encountered an issue with updating a FlatList component in my React Native application. The scenario involves running a graphql query to render items and then refetching the data when a mutation is executed using Apollo's refetch option. Althou ...