Spin celestial bodies on various trajectories at varying velocities

I am currently working on creating a solar system animation using Three.js. So far, I have successfully added 3 planets, each rotating on its own axis. In the center, I have placed the sun.

  • Each planet is represented by a separate group (mesh);
  • All the planets are contained within a main group (Object3D).

As of now, I am able to:

  • Rotate each planet on its own axis (using the planet object/group);
  • Rotate all the planets along their orbits (using the main object/group).

The issue arises when I rotate the main group by an angle, say 1, causing every planet to move by the same angle. For instance:

If I rotate one planet by 180 degrees, all the planets rotate by 180 degrees along their respective orbits. How can I make each planet rotate at a different speed?

I believe that I may not need the main group and instead should write a specific algorithm for each planet, but I am unsure how such algorithms operate. Can anyone provide guidance?

Main parts of the code:

... main loop:

function loop() {
    jQuery.each(planets, function( key, value ) {
        value.rotation.y += 0.005;
        value.rotation.x += 0.005;
    });

    group.rotation.z -= 0.005;

    requestAnimationFrame( loop );
}

... adding a planet:

var data = {
    0: {
        texture:  "images/telos.png",
        radius:   10,
        segments: 20
    },
    1: {
        texture:  "images/ako.png",
        radius:   8,
        segments: 20
    },
    2: {
        texture:  "images/alba.png",
        radius:   21,
        segments: 20
    }
};
var planets = {}
jQuery.each(data, function( key, value ) {
    var planet = creator.planet({
        texture:  value.texture,
        radius:   value.radius,
        segments: value.segments
    });
    planet.position.set( key * 60, 0, 0 );
    planets[ key ] = planet;

    group.add( planets[ key ] );
});

Any suggestions or tips?

Answer №1

Within this code snippet:

value.rotation.y += 0.005;

Lies the crucial element. This is where all planets are rotated by a uniform angle per iteration. If you desire varying speeds, each planet must possess its own unique speed value. Initially, create the planet like so:

var planet = creator.planet({
    texture:  value.texture,
    radius:   value.radius,
    segments: value.segments,
    speed:    value.speed
});

Subsequently, access that particular speed value:

value.rotation.y += value.speed;

Be sure to specify a speed value when generating the planets.

If realistic speed values are preferred, they can be derived from the radius using Kepler's laws of planetary motion. The relationship is inverse square, allowing for an approach akin to this:

var planet = creator.planet({
    texture:  value.texture,
    radius:   value.radius,
    segments: value.segments,
    speed:    1/(value.radius*value.radius) * 100 //adjust 100 as needed
});

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

Switch Tabs with Dropdown Selection

I came across a website with a similar feature, but I am interested in using a dropdown menu instead of a button or link. Check out this webpage for reference The problem seems to be related to this line of code: onchange="$('#'+$this).tri ...

What is the way to activate Dynamic ng-model from a controller?

I am implementing a loop in my HTML code where each iteration dynamically creates a model. Here is an example of how the loop looks: <tr ng-repeat="item in voucherItems"> <td><input type="text" ng-model="this['id-' + $index ...

Deciphering the AngularJS Component Hierarchy and Managing Identifiers for Freshly Generated Objects (using HTTP)

In my project using Angular 1, I have developed a todo list application which consists of two components. The first is a smart (container) component responsible for server-side interactions, while the second is a dumb/pure/stateless presentation component ...

Obtaining a value from an input field in Vue.js

Just starting out with Vue and could use a hand extracting a value from an input field: Here's what I have in my form: <input type="hidden" id="groupId" value="1"> If I were using jQuery, I would do the following: ...

eliminate the common elements between two arrays in typescript/javascript

I have two lists of objects, each containing two fields: let users1 = [{ name: 'barney', uuid: 'uuid3'}, { name: 'barney', uuid: 'uuid1'}, { name: 'barney', uuid: 'uuid2 ...

Tips for preventing the caret (^) symbol from being added to a package when installing with yarn

Is there a way to prevent Yarn from adding the caret prefix to version numbers when installing packages like npm has for changing version prefix (https://docs.npmjs.com/misc/config#save-prefix)? I would like to apply this configuration only for the current ...

When the user modifies the input, React fails to update the component state

Currently, I am utilizing React in conjunction with express to server-side render the views for my project. However, I have encountered a minor setback. Within one of my components, users are directed after their initial login. Here, they are prompted to ...

The use of the || operator within arguments

I am facing a challenge: //this console log displays correct value console.log('localstorage', localStorage.getItem('subMenu')); setSubMenu( JSON.parse(localStorage.getItem('subMenu') || JSON.stringify(s ...

Is utilizing getStaticProps the best approach for handling offline data in Next.js?

My current project in Next.js involves offline static data for the webpage content. I am using an array to store the data for later mapping. Do you recommend using getStaticProps in this scenario? For example, here is a snippet of my code: import { data } ...

Converting a JavaScript object to JSON within Node-RED

I am attempting to convert a JavaScript object created in my Node-RED flow to JSON format, but I am struggling to figure out how to do it. The object consists of an hour and minute displayed on the screen, such as "13:02". I need to see this in JSON format ...

Steps to toggle the visibility of a table by clicking on a plus or minus button

//Custom Table Display Script using JQuery $(document).ready(function(){ $('.table1').on('click',function(){ $('.table1shw').show(); }); }); //A ...

Maintain the state of the previous page in Vue to ensure continuity

Currently in the process of creating a small PWA to simulate an Android app, I have discovered Vuejs. However, I encountered an issue that has proven difficult to resolve. While scrolling through lists on the homepage for movies, TV shows, or news, clicki ...

Tips for showcasing the contents of a file on a website

Greetings! I am a newcomer to the world of web development and I am seeking a method to showcase file content on a webpage. Presently, I have succeeded in loading text file content and displaying it in a large text box. However, I am now interested in di ...

Find out the moment a script is fully loaded when it is called from a different script

I am facing an issue with a script, which I will refer to as Script A. Upon loading, Script A injects another script into the document head, called Script B. The majority of crucial code resides within Script B. Typically, when adding Script A to the host ...

One-liner for converting an array into an object

Is there a more concise method to convert all array indices and values into an object than the following: arr = ["one","two","three"]; var rv = {}; for (var i = 0; i < arr.length; i++) rv[i] = arr[i]; I understand that you can iterate through the ...

Select only the top-level items in a dropdown menu using jQuery

Wondering how to create a fade effect that only triggers when hovering over the parent LI's without affecting the items in the dropdown menu? Any suggestions or solutions? Tried numerous examples with no success, struggling to make it work and gettin ...

How can I show or hide a survey pop-up depending on the URL in the address bar?

There are 2 different applications in play here. My application is built using Angular for the front end and can be accessed at http://test.com/search. It includes a JavaScript function that triggers a survey popup whenever a user visits our website. Anot ...

Creating a multi-tiered dropdown menu in the navigation bar with the help of Bootstrap 4 and Angular 7

My goal is to implement a multilevel dropdown using bootstrap 4 and angular 7. While I successfully created a simple dropdown in the navbar following the official bootstrap documentation, I struggled to make the multilevel dropdown work. After referring ...

Tips for creating a condensed header

As a newcomer to HTML, I am facing challenges in creating a simple header similar to the one displayed on this website or the example in the image below. Below is the snippet of HTML that I have attempted: <header class="header"> <div ...

How can I modify my code to ensure that trs and th elements are not selected if their display property is set to none?

I am currently working on creating a filter for my pivot table, but I am unsure of how to dynamically calculate the sum of each row/column based on whether they are displayed or not. If you need any other part of my code, feel free to ask. To hide employee ...