The solution to my rotation acceleration issue with Matrix4 in three.js

I am trying to implement rotation of an object in my project when the "x" key is pressed using a Matrix4. The current code I have rotates the object, however, I am facing an issue where the rotation speed increases over time the longer I hold down the "x" key. How can I resolve this issue?

var r1 = 1, r5 = 1, r9 = 1, r2 = 0, r3 = 0, r4 = 0, r6 = 0, r7 = 0, r8 = 0;
var Rx = 0, Ry = 0, Rz = 0;
var rotation = new THREE.Matrix4();

function keyDown(event){
    if(event.key == 'x'){
        mesh.matrixAutoUpdate = false;
        Rx += Math.PI/180 //1 degree per key press
        r5 = Math.cos(Rx);
        r6 = -Math.sin(Rx);
        r8 = Math.sin(Rx);
        r9 = Math.cos(Rx);
        rotation.set( r1, r2, r3, 0,
                      r4, r5, r6, 0,
                      r7, r8, r9, 0,
                      0, 0, 0, 1 );
        mesh.applyMatrix4(rotation);
    }
    if(event.key == 'y'){

    }
    if(event.key == 'z'){

    } 

}

let loader = new THREE.STLLoader();
var material = new THREE.MeshLambertMaterial({color: 0x818181});

loader.load('STL/test.stl', function (geometry) {
    mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);
});

window.addEventListener("keypress", keyDown);

Answer №1

When using .applyMatrix4, keep in mind that it does not simply set a matrix, but instead concatenates the new transformation with the current one of the mesh. If you increment Rx, the rotation will add up and speed up. To avoid this issue, avoid incrementing Rx and append the same transformation (1°) for each key press:

Rx += Math.PI/180

Rx = Math.PI/180 //1 degree per key press
r5 = Math.cos(Rx);
r6 = -Math.sin(Rx);
r8 = Math.sin(Rx);
r9 = Math.cos(Rx);
rotation.set( r1, r2, r3, 0,
              r4, r5, r6, 0,
              r7, r8, r9, 0,
              0, 0, 0, 1 );
mesh.applyMatrix4(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

The issue of elements flickering while being hidden and shown with jQuery has been observed in both Chrome and

Having trouble with a simple animation using jQuery? While it may work smoothly in Firefox, you might be experiencing flickering in Chrome and Edge. Check out this jsfiddle for reference: HTML <div id="boxes-wrapper"> <div class="box"></ ...

Cannot load JSON file from Visual Studio 2013

I am currently working on building a drag-able network diagram using d3.js, and I've come across an unusual issue. Whenever I try to run the page from Visual Studios 2013, I encounter the following error: "Unhandled exception at line 25, column 13 in ...

"Uh oh! Encountered an error while trying to extend an unrecognized button type: copyHtml5. Here's a guide on utilizing datatables.net-buttons-bs4

I recently installed Datatables using npm and added the necessary packages: npm install --save datatables.net-bs4 npm install --save datatables.net-buttons-bs4 Now, I also want to include the buttons.html5 js file in my project. Previously, I used CDNs ...

Tips for Passing Parameters to Vuex mapActions correctly:Executing mapActions in Vuex requires a specific

I am facing an issue with my ProjectPage.vue where I display project issues in a v-data-table. The projects are fetched from a server API call in the sidebar and displayed there. Once I select a project, I want to use its id to retrieve its corresponding i ...

What is causing my radio button event to activate when a separate radio button is selected?

I have implemented the code below to display "pers" and hide "bus," and vice versa. JQuery: $('input[name="perorbus"]').click(function() { if ($(this).attr('id') == 'bus') { $('#showbus&apo ...

Using Webpack to transfer a function to the front-end

I am currently delving into the world of webpack and attempting to set up a basic integration. Within my file script.js, I have included some essential functions: scripts.js export foo = () => { console.log('foo') } export bar = () => ...

JavaScript debugging causing system freeze

Currently, I am working on a project that involves using MVC and dropdown lists. My issue arises when the dropdown list changes, as there is some javascript code that needs to execute. To troubleshoot the problem of the system locking up every time I tried ...

Guide on utilizing Chrome Push Notifications on an HTTP website

I'm looking to incorporate Chrome push notifications (GCM) on my HTTP site. I've read that it's typically only for HTTPS sites, but is there a workaround or sample code available for integrating push notifications on an HTTP site? I'm ...

Importing JS files or SDKs in Angular that are not modules

Currently, I am incorporating the Avaya SDK (which consists of 3 JS files) into my Angular project. However, when attempting to import it, I encounter an error stating that it is not recognized as a module. Any suggestions on how to resolve this issue? Th ...

Attempting to grasp the fundamentals of angular Routing, however, upon attempting to reload the page, nothing appears to be displayed

While working in the Bracket editor, I created a file structure with various files located under the 'scripts' tags within the Index.html file. The root folder is named 'projectAngular', inside which there are subfolders like 'appC ...

Is it possible to convert a blob to an image file using the FileReader in HTML

client side code <head> <script> var reader = new FileReader(); var objVal; var image = new Image(); reader.onload = function(e) { document.getElementById('propertyImg').setAttribute('src', e.target.result); }; fun ...

The background image is functioning correctly, but there seems to be an issue with the image overlay

I attempted to design a profile page for my website users to have an image as a banner and their profile photo. I have achieved this: https://i.sstatic.net/kFlrQ.jpg However, when I apply the top-margin to the image, it both moves the image and expands t ...

Unusual issue encountered when launching an express application in node.js

Starting my app is easy with nodemon - just type nodemon However, I encountered an error when trying node app.js https://i.sstatic.net/rRXhZ.png I have checked my package.json and it is properly configured with: "scripts": { "start": "node ./bin/ww ...

What is the best method for linking an HTML file to CSS, javascript, and image files?

I need assistance with running an HTML file along with its source files that I downloaded from the server. The HTML file is located in NSCachesDirectory and here is the code snippet I am using. Can someone please help me figure this out? NSArray *paths ...

Issue with passing JSON data to a PHP file using CURL

I am currently working on a project that involves sending exam data with the exam-name and the selected number of questions from a list. Here is the project architecture I am following: To send JSON via AJAX to my make_exam.php file The questions.php fil ...

Converting string literals to an array of enums

I have a scenario where I am getting the following data in an API response: { "roles": [ "ADMIN", "USER" ] } The response always includes an array of roles (USER, PRESENTER, ORGANIZER, and ADMIN). I am looking to transform this into a valid TypeScript a ...

Dealing with jQuery hover/toggle state conflicts in Internet Explorer?

Check out my code snippet: <!doctype html> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <style type="text/css"> label {display:block; w ...

Is it possible to install non-root dependencies in node_modules using NPM?

The repository @superflycss/component-navbox contains the following list of dependencies: "devDependencies": { "@superflycss/component-body": "^1.0.1", "@superflycss/component-display": "^1.0.2", "@superflycss/component-header" ...

Creating a series of image files from CSS and Javascript animations using Selenium in Python

Looking to convert custom CSS3/Javascript animations into PNG files on the server side and then combine them into a single video file? I found an interesting solution using PhantomJS here. As I am not very familiar with Selenium, adapting it for use with S ...

Use ajax to add rows to the second-to-last table

I am facing a situation where I have a table with 25 default rows. When scrolling to the bottom of the table, I want to dynamically insert another set of 25 rows. Everything is functioning correctly, but in a specific scenario, I need to preserve the last ...