cosinekitty / astronomy

Astronomy Engine: multi-language calculation of Sun, Moon, and planet positions. Predicts lunar phases, eclipses, transits, oppositions, conjunctions, equinoxes, solstices, rise/set times, and other events. Provides vector and angular coordinate transforms among equatorial, ecliptic, horizontal, and galactic orientations.
MIT License
496 stars 63 forks source link

Planets positions in ThreeJs enviroment #229

Closed AfarsheziF closed 2 years ago

AfarsheziF commented 2 years ago

Hello all. A beginner question.

I recently discovered astronomy.js while working on a client project, and the project looks great!

I like to calculate the current solar system planets positions and set it on a ThreeJS sketch. Which functions should I use to convert the data to match an XYZ system? Is there a function to get the current distance of a planet from the sun?

I like to achieve a result similar to the 3D Solar System simulation of TheSkyLive (https://theskylive.com/3dsolarsystem). As the simulation code is not available, I ask for your help here.

Many thanks!

cosinekitty commented 2 years ago

Hello @AfarsheziF, and welcome to the Astronomy Engine project! Yes, I can help you with this.

First you will want to look at the function HelioVector. This will return a 3D position vector for any solar system body at any moment in time.

In order to show the solar system from different angles, you can rotate the vector by different angles using the rotation matrix functions. For example, you can create an identity matrix (no rotation) using IdentityMatrix. Then use the Pivot function to rotate the matrix by a given number of degrees, probably around the x-axis. Finally call RotateVector giving it the pivoted rotation matrix and each 3D vector, to produce a rotated 3D vector.

When you graph the 3D position on a 2D surface, you can use the rotated vector's x-coordinate and y-coordinate, and ignore the z-coordinate.

cosinekitty commented 2 years ago

I realize I didn't answer part of your question. To calculate the distance of a planet from the Sun, once you have the 3D vector from HelioVector, you can call the vector's Length method. This returns the number of astronomical units between the center of the Sun and the center of the planet.

cosinekitty commented 2 years ago

Hi @AfarsheziF, are you having good luck with the vectors and rotation matrices? Please let me know if I can provide any more help.

AfarsheziF commented 2 years ago

Dear @cosinekitty ! Thank you so much for replying to this case. I'm surly waiting to test your solution and come with results. Unfortunately the project has stopped and will continue in a month or so. I'll post the results then.

Thanks again.

jffaust commented 2 years ago

I am trying to do something similar but in my case I'm only interested in mapping the Sun, the Earth and the Moon in ThreeJS.

Reading your advice @cosinekitty, I started by using the HelioVector function but then I was a bit lost as to what kind of rotation I should apply so that the Earth's orbit around the Sun appears on the horizontal plane in my 3D scene. Looking further, I saw the ecliptic coordinates, which from what I understand are based on the Earth's orbit. I thought this would remove the need for me to apply the rotation myself. I tried something like this:

    const gv = astro.GeoVector(astro.Body.Sun, date, false);
    const ecliptic = astro.Ecliptic(gv).vec;
    console.log(ecliptic); // x: -0.7703776488791916, y: -0.6257079284497832, z: 0.00003668609414608781

My expectation was that the values for z would be 0. They are close but not quite. Is this maybe related to the "mean plane" mentioned in the Coordinate transforms section for EQD?

cosinekitty commented 2 years ago

@jffaust Yes, that is exactly right. Because every massive object in the solar system is pulling on every other object, orbits are not exactly the simple shapes one would at first think. So the Earth's movement is not confined to a single plane. However, the Earth does not wander away from the ecliptic plane very far; it stays quite close to that plane at all times.

However, the same cannot be said for the Moon. Its z-coordinate will be quite variable, no matter what coordinate system you use, because its movement is all over the place. The Moon's orbit is one of the most complicated things to calculate, because it is a three-body interaction between the Sun, Earth, and Moon.

See the Wikipedia article orbit of the Moon for more details.

I hope this helps. Feel free to drop more questions here. That sounds like a cool project you are working on. I would like to see it when you get it working.