What is the best way to rotate a mesh group around the axis of a randomly selected 3D element within the Three.js environment?

I'm currently delving into the intricacies of three.js in order to experiment with a basic mechanism. My end goal is to visualize board tilt (front view) in relation to axle orientation (plan view) for different designs on an XY plot, although that's more like step five or six in my process. Right now, I'm grappling with step one, trying to grasp the concept of obtaining an axis of rotation, symbolized by a small diameter cylinder.

This image clearly illustrates a setback. https://i.sstatic.net/akqtW.png

  • I aim to use the matrix functions already available in Three.js for data visualization and system analysis.
  • The purpose of the worldMatrix object within a mesh object is not entirely clear. I presume it contains information about location, orientation, and scaling, but I'm unsure how to extract this data for translating or rotating other components in the 3D space.
  • Perhaps it would be better to create duplicates of original component locations and matrix values, and then work from those copies. Reaching the same point after tilting left and right seems to pose a challenge when working directly with the original.

The model features a simple skateboard. My objective is to rotate the front truck hanger (housing an axle and two wheels) along a tilted axis based on user input from a slider element.

Check out the sandbox here...

 var euler;
 const rotateMeshGroup =( meshGroup, rotationAxis, angleOfRotation) =>{
     euler = new THREE.Euler().setFromRotationMatrix(rotationAxis.matrixWorld);
     meshGroup.children.forEach(item => {
         item.rotateOnAxis(euler, THREE.MathUtils.degToRad(angleOfRotation));
     }) 
 }

I'm struggling with creating an axis of rotation. While I grasp the concept of a 3D angle, I'm unsure about determining the distance from the axis. Is it calculated using a matrix cross product (shortest distance to a 3D axis with perpendicular distance)?

edit: I'm still grappling with this issue. After sifting through numerous console.logs, I haven't found any information in the predefined matrices that could help me. I've converted the pivot axis from a small diameter cylinder mesh object into a line defined by two vector endpoints. Currently, I'm contemplating delta-x, delta-y, delta-z calculations (translate, rotate, translate back) within the RotateMeshGroup function.

Any pointers or suggestions?

Answer №1

Indeed, this project started as a mere work in progress but has now evolved significantly. My goal with this endeavor was to delve into the realm of using Three.js for engineering analysis purposes. Along the way, I've picked up several valuable insights that I'd like to share.

  1. A pivotal resource I stumbled upon was a post by "Stranger in the Q". The provided code showcased a rotating Solar System simulation complete with the Sun, Earth (accompanied by a revolving moon and rocks), and Mars (flanked by two additional planets). Studying this example meticulously shed light on the intricacies of rotation dynamics within a 3D scene hierarchy. One key takeaway was the importance of accurately naming objects to streamline the understanding of their relationships. This example proved instrumental in demystifying various concepts for me.

  2. Another invaluable discovery was a response from BadAskTechie outlining a simple yet effective technique for rotating a mesh's geometry around a specified axis and pivot point. The concise code snippet provided a clear solution to a common challenge.

    // Function to rotate a mesh's geometry about a specific axis and pivot
    const rotateAbout = (mesh, axis, axisPosition, angle) => {
        // Implementation details omitted for brevity
    }
    
  3. One practical tip for handling complex nested 3D meshes is to perform all geometry manipulation, including orientation adjustments, at the object's geometry creation stage rather than trying to modify the meshes directly in the scene space. By following this approach, alignment issues can be minimized, resulting in smoother animations and interactions.

  4. A particularly useful technique I adopted involved tracking the spatial positions of elements during rotations and animations. Instead of relying solely on the mesh's vertices, which could be overwhelming in complex models, I created a simple line object to serve as a reference point. Moving this line along with the assembly allowed me to determine positional changes more effectively and enhance future calculations.

  5. The initial question raised earlier has been overshadowed by significant progress made in addressing challenges related to skateboard truck modeling. While some aspects of the provided sandbox code may not be optimal, I intend to share the finalized project as open source, complete with a link to the Github repository and demo. This way, others can witness the evolution of my work and potentially benefit from the shared insights.

I believe these experiences and revelations could prove beneficial to individuals embarking on their journey with Three.js.

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

Undefined index when decoding JSON data

When calling the save function, a $.post is sent to 'upp.php' as follows: function save(){ var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || []; var newItem = {}; var num = document.getElementById("num").value; newIte ...

How can I utilize the LED flash on a Windows Phone using WinJS?

Currently delving into the world of WinJS and endeavoring to create an application that involves operating the LED Flash. I am seeking guidance on how to properly access the LED Flash functionality to allow for toggling it ON or OFF. Your insights on how ...

What methods can I use to retrieve traversed objects using riak-js?

Utilizing Node with riak-js to interact with a Riak database. I have established two buckets named invites and events. The invites bucket contains a link to events. Is there a way to fetch both the invite object and the corresponding event object in one qu ...

Tips for creating a column with add, edit, and delete buttons in PHP

I found this code on a website that handles pagination and search functionality. However, I am looking to enhance it by adding an icon or button in a column that will allow users to link to the edit and delete functions for specific data selected in the ta ...

Is there a way to export static HTML from Next.js to different directories at once?

My Next.js website is static with 1 million pages, but I am facing an issue where all the exported pages are stored in the same folder, requiring a lot of RAM to access files. Is there a way to export pages into multiple folders, maybe grouping 100k pages ...

What is the process for uploading a JSON file from your local drive?

I am attempting to use jQuery to load a local JSON file. The code seems to be functioning properly, but for some reason, the data is not being made available in an array. $.getJSON("/ajax/data/myjasonfile.json", function(json) { console.log(js ...

Transmit the JavaScript array via AJAX to PHP server

Is it possible to transfer a JS array created using the .push function to another PHP file? I have included the code snippets for the two files involved, game.js and gallery.php. In Game.js: imgarray.push({"img_name": img_name,"x": x_value,"y": y_value,"w ...

One issue encountered in the AngularJS library is that the default value does not function properly in the select element when the value and

In this AngularJS example, I have created a sample for the select functionality. It is working fine when I set the default value of the select to "$scope.selectedValue = "SureshRaina";". However, when I set it to "$scope.selectedValue = "Arun";", it does n ...

Starting the download of the canvas featuring a stylish border design

I'm facing an issue where the canvas border is not showing up when I download the canvas with a border. The border appears on the screen, but it doesn't get included in the downloaded canvas. let canvas=qrRef.current.querySelector("canvas&qu ...

How to Send Data to ASP.NET MVC Controller Using AJAX Request with jQuery

I am trying to send parameters to a controller from jQuery, but I am struggling with it. The call works fine without parameters when the URL is just /SurveySection/EditLocalization. Shouldn't it be something like this: /SurveySection/EditLocalization? ...

Is it possible to integrate a jQuery function within PHP code when encountering a "is not defined"

I am looking to implement the jQuery-confirm dialog in place of using a JavaScript alert generated by PHP. However, when I execute the following code, I encounter the following error message: $ is not defined echo "<script>$.alert({title: ' ...

Leverage Pinia store in Vue helper functions

I have been working on my Vue.js application and I am looking to implement some helper functions that will utilize a Pinia store within the app. These functions need to be accessible by multiple components. Should I define these helper functions directly ...

What is the best way to deploy a Vue Webpack application using Sails.js?

I am currently managing a Sails.js server alongside a Vue Webpack application, running as separate entities. Here's how my folder structure is laid out: /application-root/server/ /application-root/client/ Within the /server/ directory lies my Sails ...

The database was successfully updated; however, the API encountered a 500 error when trying to access

Currently, I am working on a project that utilizes Javascript alongside Node.js, Express, SuperAgent, and KnexJS for database management with Sqlite3. The issue I am facing is as follows: Upon submitting data for updates through my API route using the PUT ...

Make the most of your Bootstrap 3 layout by utilizing a full page container that fills both the width and height between a fixed header and

I am currently working on a basic bootstrap page with the Example template from Bootstrap's website. I want the content in the middle to take up the space between the header and footer, while ensuring that both the header and footer remain visible at ...

Can you please explain the purpose of the state object in the setSearchParams hook of react-router-dom v6

Can anyone explain the { state: ? } parameter used in the update function of useSearchParams? const [search, setSearch] = useSearchParams(); setSearch( { ...Object.fromEntries(search), transFilters: JSON.stringify(filters), }, { ...

Using Vue and vue-multiselect to create interactive options based on the selected language

I'm currently developing a Vue website with multilingual support. The chosen language is stored in a Vuex store and accessed through the computed property lang, like so: lang(){ return this.$store.state.lang } I use this lang property in v-if cond ...

Removing an active image from a bootstrap carousel: A step-by-step guide

Within my bootstrap carousel, I have added two buttons - one for uploading an image and the other for deleting the currently displayed image. However, I am unsure how to delete the active element. Can someone provide assistance with the code for this but ...

Ways to adjust timestamps (DayJs) by increments of 1 minute, 5 minutes, 15 minutes, 30 minutes, and more

Currently, I am exploring time functionality within React Native by utilizing DayJs. I have noticed a slight inconsistency when comparing 2 different points in time to calculate the time difference. Typically, everything works smoothly such as with 10:00 ...

What steps should I take to fix the issue of "[ERR_REQUIRE_ESM]: Must use import to load ES Module" while working with D3.js version 7.0.0 and Next.js version 11.0.1?

Encountered a roadblock while integrating D3 with Next.js - facing an error when using D3.js v7.0.0 with Next.js v11.0.1: [ERR_REQUIRE_ESM]: Must use import to load ES Module Tried utilizing next-transpile-modules without success Managed to make D3.js ...