What could be causing the rotation speed discrepancy of (1/2) in Three.js?

I'm currently simulating the earth's rotation on its axis in my project. I calculated that one complete rotation of the earth equals

(2*Math.PI)

radians, so to determine the earth's rotation per minute (per frame), I used the formula:

(2*Math.PI)/(24*60)

. My program is rendering at 60FPS with requestAnimationFrame(), indicating that each second of runtime should simulate one hour of actual earth rotation. When I checked Chrome's JavaScript console, it was confirmed to be running at 60FPS. However, the simulation appeared to be twice as fast, completing a full rotation every 12 seconds instead of 24. By adjusting the expression to

(2*Math.PI)/(24*60*2)

, the rotation speed corrected itself, with 24 seconds now equaling one full rotation. While I'm relieved that the program is functioning properly, it's perplexing why multiplying the expression by a factor of (1/2) was necessary for this result. Any insights into this behavior would be appreciated. Thank you.

The render function within my code includes the following expression:

earth.rotation.y += (2*Math.PI)/(24*60*2)

.

Answer №1

requestAnimationFrame utilizes the optimal frame time for each frame, which may vary slightly and impact movement within seconds.

  1. I cannot fully explain the remarkable outcome you achieved, but when a purely time-dependent movement is needed, one solution is to adjust the movement by the time elapsed since the last render:

    var lastDate = new Date();
    
    function render () {
    
        var now = new Date();
        var delay = now - lastDate;
        lastDate = now;
    
        earth.rotation.y += delay * your-rotation-coeff
    
        requestAnimationFrame( render );
        renderer.render( scene, camera );
    
    }
    
  2. However, new Date() is not the most precise JavaScript feature. It's performance.now(), which is not universally supported. Instead of writing out all the previous lines plus fallback code, three.js offers the handy Clock feature that simplifies this process:

    var clock = new THREE.Clock();
    
    function render () {
    
        earth.rotation.y += clock.getDelta() * your-rotation-coeff
    
        requestAnimationFrame( render );
        renderer.render( scene, camera );
    
    }
    

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

What could be the reason for my Angular component not updating its value?

component1.html : <div class="nums-display"> {{nums}} </div> TS: nums: Array<number> = [0, 1, 2, 3]; ngOnInit(): void { this.numService.getNum().subscribe((res) => { this.num = res; }); } component2.html: <div (cli ...

I'm having trouble getting my button to work with addEventListener while using Ejs and Express. What could

Creating a Twitter-like platform where I can post tweets and have them display on the same page has been quite challenging. I've set up each post to have an Edit button initially hidden with 'display:none'. However, when I try to click on th ...

Upon hitting submit, the form remains unresponsive

I have been developing a permissions system for my NodeJS project (Using the SailsJS MVC) and encountered an issue. After resolving my initial error as detailed here, I am now facing a problem where the form is unresponsive. It neither shows an error in th ...

Tips for highlighting HTML syntax within JavaScript strings in Sublime Text

Is there a Sublime package available for syntax highlighting HTML within JavaScript strings specifically? (Please note that the inquiry pertains to highlighting HTML within JS strings only, not general syntax highlighting.) Currently, I am developing Ang ...

Exploring the creation of dynamic DOM elements within Angular

I'm having trouble figuring out how to create a DOM element in Angular and pass it to jsPlumb, which I'm using to draw charts. When creating connections between nodes using jsPlumb, I also need to create an overlay on these connections. The offi ...

Limit access to route in ExpressJS only to internal redirects

I'm managing an ExpressJS application that includes specific routes which I intend to only function when redirected to from my code, rather than input directly into the URL. Essentially, if a user attempts to enter "myapp.com/url" it should not be ac ...

How can I retrieve an array from an object containing both a property and an array in TypeScript?

One of my objects always consists of a property and an array. When I use the console.log(obj) method to print it out, it looks like the following example: ProjectName: MyTest1 [0] { foo: 1, bar: 2} [1] { foo: 3, bar: 4} [2] { foo: 5, bar: 6} Alternat ...

Is there a way to identify the index of user input when using the .map method?

I'm currently utilizing the Array.prototype.map() method to present an array within a table. Each row in this table includes both an input field and a submit button that corresponds to each element from the Array.prototype.map() function. Is there a w ...

Error: Attempting to access property 'setData' of an undefined object results in a TypeError [Slider]

I encountered an error with my slider that says Uncaught TypeError: Cannot read property 'setData' of undefined. The error occurs when I use material ui as a component along with redux-form. This issue happens specifically when the slider is bein ...

Troubleshooting Firebase AppCheck v8 in React: Encountering a 400 error message stating "App ID is Invalid: 'undefined'"

I've been attempting to integrate appCheck into my Firebase project. Despite following the instructions in the Firebase documentation and consulting several StackOverflow posts, I'm encountering difficulties getting it to function correctly. When ...

Performing a Mongoose query using the "find" method on multiple fields at once

I am currently developing an API using Node.js, Express, and MongoDB for a product available in the market. Each product has a title, brand, image, quantity, and other details. Our goal is to allow users to search for products by both title and brand in a ...

Node.js does not allow for the usage of `.on` to monitor events until the client has been

I'm currently working on developing a WhatsApp chatbot using the whatsapp-web-js package. However, I am facing some difficulties with implementing it due to my limited knowledge in node JavaScript and async code. let client; //To establish connection ...

Utilizing requirejs or grunt for concatenation and minification greatly enhances the performance of AngularJS implementations

I'm facing a dilemma with my Angular app. I have several JS files included in the index.html file, and when the app loads, all these files are downloaded before the app is ready. <html> ... <script src="scripts/controllers/loginController.js ...

"Modifying the content:url() with JavaScript: A step-by-step guide

Hello, I am currently working on creating a traffic light using an array of images. My goal is to change the image path specified in the CSS when a button is clicked, to change the appearance of the traffic light. In this case, the 'content.url()&apos ...

Order of setTimeout calls in React's execution sequence

I am currently trying to wrap my head around the functionality of this React component (which is designed to delay the rendering of its children): function Delayed({ children, wait = 500 }) { const [show, setShow] = React.useState(false); React.useEff ...

The MeshBasicMaterial in THREE.js successfully renders, while the MeshLambertMaterial does not produce the desired outcome

In my current project, I've been working on creating a randomized sheet composed of arrays containing x, y, and z coordinates to draw triangles between points. You can see the outcome by clicking on this screenshot. Initially, I utilized MeshBasicMat ...

Validating uploaded files in Javascript and handling server upload operations

I'm having a small issue with a webpage I am creating. Essentially, I am looking to validate whether a user has selected a file and then upload it to the server. I understand this can be done using JavaScript: if(document.getElementById("uploadBox"). ...

Encountering obstacles while trying to update an array in MongoDB with NodeJS

I've done all the necessary research, but I'm still unable to identify my mistake. Any assistance would be greatly appreciated. In my Mongo collection (trips), I have the following data: { "_id": ObjectId("56f5ee3dab124b181256ddf1"), "w ...

Is there an official IRC channel for YUI?

Although the yui3 documentation is quite helpful, it can also be beneficial to ask unconventional questions in order to discover the best practices. Are there any gatherings or meetups for all the talented yui developers out there? ...