+ 5

How to rotate the moon around the earth on JAVA

I was creating a simple Solar System. The earth rotates around the sun. Now I am struggling while trying to rotate the moon around earth, in the same time while earth rotates around sun. Thank you for your help!

12th Feb 2021, 6:24 PM
Altina
Altina - avatar
8 Answers
+ 7
Just a simple vector manipulations would solve the problem. For a 2D plane, If earth is rotating around the sun, the rotational transformation would be x' = x + cos(a) * r; y' = y + sin(a) * r; Now, the problematic part is that when the earth rotates, it actually being translated too and you've gotten the new translation vector as [x', y'] but then you don't know what would be the center of rotation for earth-moon on each frame. The trick is to position the moon by some certain known angle relative to the earth and increment/decrement this angle. This means, we'll have to modify the above formula for moon's positional vector, so mx' = (x' - x) * cos(a) + (x' - x) - sin(a); my' = (x' - x) * -sin(a) + (x' - x) + cos(a); Where (a = angle) and (x' - x) is the translational vector relative to the new earth position in VECTOR SPACE. This would work for the rotation but remember the translation have been modified, so you have to translate the moon back to the actual pos relative to the earth position.
12th Feb 2021, 10:00 PM
Mirielle
Mirielle - avatar
+ 4
It can be called a circles superimposition implemented like here: https://code.sololearn.com/WYEQcTXBZbeK/?ref=app
12th Feb 2021, 9:43 PM
JaScript
JaScript - avatar
+ 2
Would be easier if you show your code, but maybe this helps: I guess you're defining rotation in polar coordinates, i.e. increase angle by a fixed amount in each step, and then transferring to cartesian. So for the moon you can do the same, with an increase roughly 13 times that of the earth. That will represent the rotation moon around earth. Then transfer to cartesian and add those coordinates to the coordinates of the earth
12th Feb 2021, 7:46 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
Do the rotation relative to the earth's frame of reference.
12th Feb 2021, 8:49 PM
Sonic
Sonic - avatar
+ 2
Use the same method for a 3D plane, just add the z-axis. I cannot write a code for this because I'm taking a break since December last year.
12th Feb 2021, 10:01 PM
Mirielle
Mirielle - avatar
+ 2
I have a doubt that you don't understand my answer but you just decided to mark it as the best so I decided to write a pseudocode in javascript. Remember, mathematics everywhere is the same, so this same formula is how it would appear in a Java code. I also realise that this method in the code is straightforward and simple unlike my comment above. Also, this is just a rotation demonstration, no physics whatsoever is going on in the code https://code.sololearn.com/WW5YvZ1OBt48/?ref=app
12th Feb 2021, 10:49 PM
Mirielle
Mirielle - avatar
+ 1
Mirielle well girl thank you so much, a great solution😍 also sorry for taking your time❤️
12th Feb 2021, 11:27 PM
Altina
Altina - avatar
- 4
ajaysingh
13th Feb 2021, 6:51 AM
Rajkumar Singh