Tips for orchestrating the moon's orbit around the Earth and its journey with the Earth around the sun

The moon orbits the earth as the earth orbits around the sun. While trying to maintain the matrix4 methods, I attempted to achieve this through matrix multiplication but encountered some difficulties in changing the order of multiplication.

// Solar System Simulation
window.onload = function()
    {
        var scene = new THREE.Scene();

        // Setting up camera
        var camera = new THREE.PerspectiveCamera(60,window.innerWidth/window.innerHeight,0.1,1000);
        camera.position.x = 1;
        camera.position.y = 1;
        camera.position.z = 10;

        var renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth,innerHeight);

        document.body.appendChild(renderer.domElement);

        // Create sun with specified geometry
        var sun_geometry = new THREE.SphereGeometry(1,32,32);
        var sun_material = new THREE.MeshLambertMaterial( {color: 0xff0000} );
        var sun = new THREE.Mesh(sun_geometry,sun_material);
        sun.matrixAutoUpdate = false;
        scene.add(sun);

        // Create earth with specific geometry
        var earth_geometry = new THREE.SphereGeometry(0.5,32,32);
        var earth_material = new THREE.MeshLambertMaterial( {color: 0x0000ff} );
        var earth = new THREE.Mesh(earth_geometry,earth_material);
        earth.matrixAutoUpdate = false;
        scene.add(earth);

        // Create moon with specified geometry
        var moon_geometry = new THREE.SphereGeometry(0.25,32,32);
        var moon_material = new THREE.MeshLambertMaterial( {color: 0x00ffff} );
        var moon = new THREE.Mesh(moon_geometry,moon_material);
        moon.matrixAutoUpdate = false;
        scene.add(moon);

        // Light setup
        var system_light = new THREE.DirectionalLight( 0xffffff, 0.4);
        system_light.position.set(0.5,0,1).normalize();

        var sun_light = new THREE.PointLight( 0xffffff, 1, 100);
        sun_light.position.set(1,1,0).normalize();

        // Adding lights to scene
        scene.add(system_light);
        sun.add(sun_light);

        // Rendering function
        var render = function()
        {
            // Calculating delta time
            var now = new Date();
            var dt = now-(render.time || now);
            render.time = now; 
            renderer.render(scene,camera);

            var sun_rotation_matrix = new THREE.Matrix4().makeRotationY(0.001*render.time);
            
            var earth_rotation_matrix = new THREE.Matrix4().makeRotationY(0.002*render.time);
            var earth_translation_matrix = new THREE.Matrix4().makeTranslation(4,0,0);
            earth.matrix = earth_rotation_matrix.multiply(earth_translation_matrix);

            // Potential issues here
            var moon_rotation_matrix = new THREE.Matrix4().makeRotationY(0.004*render.time);
            var moon_translation_matrix = new THREE.Matrix4().makeTranslation(1,0,0);
            moon.matrix = moon_translation_matrix.multiply(earth_rotation_matrix).multiply(moon_rotation_matrix);

            requestAnimationFrame(render);
        }

        render(); // Start rendering
    }

Answer №1

Instead of bringing the Moon into the scene, let's add it to the Earth and adjust the sequence in which the matrix is applied:

earth.join(moon);

// ...

moon.matrix = moon_translation_matrix.multiply(moon_rotation_matrix);

[ https://jsfiddle.net/Xdkw49fh/ ]

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

Using dynamic template URLs in resolving with Angular's UI-Router can provide flexibility

Currently, I have implemented a parent directive for an entire view to interchange templates based on the outcome of a promise. .directive('myDirective', function(myService) { var rootDir = '/path/to/templates'; return { ...

Tips for creating tree diagrams for JSON data with D3.js?

I am eager to represent intricate nested JSON objects as tree diagrams using D3.js. Most examples utilize JSON files that explicitly outline their hierarchy, such as through the children attribute: { "name":"Alex", "children& ...

Retrieve the value of a TextBox and display it as the title of a Tool

Hello there, I am currently learning front-end technologies and have a question. I would like to retrieve the value of a TextBox and display it in a Tool-tip. The code for the TextBox has a maximum length of 30 characters, but the area of the TextBox is no ...

What is the purpose of using square brackets in the angular.module() function in AngularJS?

const myapp=angular.module('myApp',[]); As someone venturing into the realm of angularjs, I have a question. What is the significance of using [] in angular.module()? If anyone could shed some light on this, it would be greatly appreciated. ...

React/Redux encountered a roadblock when attempting to iterate through an array stored in the state

Currently, I am delving into the world of React/Redux and have encountered a stumbling block while transitioning one of my single-page web applications to this framework. What I aim to achieve is to populate an array in the initial state of the web app wit ...

Duplicate the image from a specific div to another div, ensuring that only one clone is created

I'm trying to replicate an image in multiple spots within a frame, but I can only manage to get one instead of many. Can someone please tell me what I'm doing wrong? I am creating a map of terrain where I need to clone images from one div to anot ...

How do I resolve the issue of PHP sendmail form sending empty emails and fix the JavaScript?

I spent the whole day dealing with sendmail.php. Initially, I had an issue with not receiving emails, which I managed to fix. However, after that, I started receiving blank emails. It wasn't a problem with my PHP or HTML code, but rather with the Java ...

Experiencing a problem with a loop in iMacros/JS?

I have an iMacros/JS script for Facebook that logs into a FB account from a CSV file, then it has a second loop j which sends 20 friend requests from each account. The issue arises when switching accounts and encountering a popup message requiring phone n ...

Is the VueJs and ChartJs combination causing the chart to be width-responsive, but not height-responsive?

I've exhausted all options, but I can't seem to make the height of my chart respond effectively. The width is behaving as expected, adjusting responsively, but the height stubbornly remains fixed at 400px. Within my primary Vue application, I i ...

Display a smaller image preview in the product photo gallery

Hey there! I'm currently working on a website and looking to showcase my product in a similar way as shown here: I would like my main image to be displayed with all the other variants of the product image underneath, along with the same visual effect ...

Add empty objects to an array using a push function following the Vue JS constructor function

During my learning journey with Vue JS, I encountered an issue while attempting to populate an array with objects using a constructor function and the push method. Surprisingly, in Vue JS, the push function inserts a blank object into the array instead of ...

The try and catch block in JavaScript is failing to correctly capture the HTTP status

I have a function that successfully posts JSON to an API endpoint. Here is the code I am using: function sendValuesPageLoad(){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { try { if (xhr.readyState === ...

Leveraging nested objects within React state

A React child cannot be an object. If you intended to render a collection of children, utilize an array Encountering the error mentioned above has left me puzzled regarding its restrictions. Below is the code where I am facing this issue. It includes an o ...

React is indicating that returning functions is not appropriate as a React child. This issue may arise if you accidentally return a component instead of <Component /> within the render method

Check out the main game.js and app.js files: //Here is the code in game.js// import React, { Component } from "react"; class Game extends Component { constructor(props) { super(props); this.state = { num: 0 }; this.numPicker = this.numPi ...

Using Ajax to Prevent Duplicate Form Submissions

In the legacy application I am working with, they have implemented Ajax form "submits" using [a] tags. If we were using [input] buttons, we could easily disable them by setting the disabled attribute. However, when it comes to hyperlinks, disable is not pa ...

What is the best method for coordinating the Vue mounted function with external dependencies?

Within my Vue mounted function, I am in need of both an internal value referred to as foo and an external value known as bar. Here is a snippet from myVue.js file -- //myVue.js export var myVue = new Vue({ el: '#my-vue', data: { ...

Issue with Firebase V9 addDoc: No indication of success or failure, and data is not being written to the

I am encountering an issue where the authentication related functions are working fine, but I am unable to make any progress with Firestore. Despite no errors or successes being returned by the try-catch block, nothing seems to happen in the Firestore data ...

Issue with merging JSON in Angular using RxJS

Seeking assistance with combining two JSON objects in an Angular application. JSON Object 1: { "level1": { "level2": { "level31": "Text 1", "level32": "Text 2", "leve ...

Having trouble with SQLite and local storage in Android PhoneGap app specifically on Samsung Galaxy devices

My PhoneGap app is running smoothly on iOS and most Android devices, but I'm encountering an issue specifically with Samsung Galaxy S3 and S4. Upon the initial page load, the app creates a local SQL database to store question and answer values. Howev ...

The information submitted through the form is not displaying on the console after being gathered using the post method in Express

When attempting to add products using a form on the admin page, the data (including an image file) is not being displayed in the console as expected. Instead, the following message appears: "{} POST /admin/add-product - - ms - -" Below is th ...