Where can I get the position of a planet (specifically, Earth)

edited July 2020 in General
I was looking around in the code, and can't seem to find a way to access the current position of the Earth. I am making a satellite visualisation, where each satellite has a coordinate at a given time. I was planning of just finding the position of the Earth and generating the satellite relative to it. However, I can't find the position of the Earth.

In short, how can I access the current position of a planet? Is this even possible?

Comments

  • edited July 2020
    Sure. The info you want is provided by the Orbit class (ivoyager/tree_refs/orbit.gd) which is a member of Body class (Earth is a Body). Body instances and present time can be gotten from containers in Global.

    var time: float = Global.times[0]
    var earth: Body = Global.bodies_by_name.PLANET_EARTH
    var earth_orbit: Orbit = earth.orbit
    var position: Vector3 = earth_orbit.get_position(time)

    Btw, the Orbit class is set up to handle orbit precessions, which is how satellites can maintain sun-synchronous orbits. The Body class is meant to represent any physical object affected by gravity, which would include artificial satellites.

    [Edit: position above would be Earth's position relative to its parent, the Sun. I.e., this is its local translation, not its global translation. The Sun is not at global origin (0,0,0) for reasons discussed in this issue.]

    [Edit2: Come to think of it, why do you even need Earth's position? Your satellites should be children of Earth. Their position should be relative to Earth.]

    [Edit3: simplified code a bit to take advantage of Global.]
  • edited July 2020
    [Edit2: Come to think of it, why do you even need Earth's position? Your satellites should be children of Earth. Their position should be relative to Earth.]
    Thanks for the help. 

    On your point in edit 2, for each satellite I have the Earth Fixed (ECEF) an Inertial (ECF) positions of the satellites at certain times. Therefore, my plan was to get the position of the Earth to then apply these positions to the satellites relative to the Earth.

    Because my satellite data uses ECEF and ECI coordinates, I thought the way outlined above would be better than, say, creating a csv file with the orbital elements of the satellites, and set PLANET_EARTH as the parent (like you do with the moons).

    Also, my data is in a JSON file format, whereas the other data used in I, Voyager is in csv files. I need to keep my data as a JSON file as I will pull my satellite data from a server - another reason to use the method I outlined above.

    Thanks once again for all your help!

    (also, on an unrelated note, does the "time" variable represent the seconds or days since the J2000 epoch, as in Timekeeper the comment next to "time" says "seconds from J2000 epoch (= 2000-01-01 12:00:00)", but I read elsewhere on the forum that "time" represents days since the J2000 epoch)
  • edited July 2020
    jack_hill said:
    (also, on an unrelated note, does the "time" variable represent the seconds or days since the J2000 epoch, as in Timekeeper the comment next to "time" says "seconds from J2000 epoch (= 2000-01-01 12:00:00)", but I read elsewhere on the forum that "time" represents days since the J2000 epoch)

     It was days in earlier versions, but now seconds. Look in static/unit_defs.gd for anything related to units.
  • Charlie W said:
    var time: float = Global.times[0]
    var earth: Body = Global.bodies_by_name.PLANET_EARTH
    var earth_orbit: Orbit = earth.orbit
    var position: Vector3 = earth_orbit.get_position(time)
    Hi, sorry to bother you again, but I was just using the above code. The problem is, however, that when trying to get:
    Global.bodies_by_name.PLANET_EARTH
    I can't. I get the error:
    Parser Error: The method "get_earth_global_position" isn't declared in the current class.
    I suspect this is because I am using the above code in the process() function, and because process() is run every frame (including in the menu screen, I am assuming), the "bodies" haven't been added yet (the solar system hasn't been built), thus PLANET_EARTH is not yet in bodies_by_name.

    To fix this (if the problems is what I think it is), I've been looking for something that will signal when the solar system has been built, a bool or something (or, alternatively, a method that is run every frame only once the solar system is built).

    My question is: there some way to tell when the solar system is built, so that Global.bodies_by_name.PLANET_EARTH does not result in an error?

    Thanks
  • Look in in singletons/global.gd. There are three signals you could use:

    signal system_tree_built_or_loaded(is_new_game)
    signal system_tree_ready(is_new_game)
    signal about_to_start_simulator(is_new_game)

    All three fire after the solar system is built (or loaded from a save file) and after bodies_by_name has been populated. There are three because sometimes one needs something to happen before something else. Do text searches to see examples of usage in the code.
  • Charlie W said:
    Sure. The info you want is provided by the Orbit class (ivoyager/tree_refs/orbit.gd) which is a member of Body class (Earth is a Body). Body instances and present time can be gotten from containers in Global.

    var time: float = Global.times[0]
    var earth: Body = Global.bodies_by_name.PLANET_EARTH
    var earth_orbit: Orbit = earth.orbit
    var position: Vector3 = earth_orbit.get_position(time)

    Btw, the Orbit class is set up to handle orbit precessions, which is how satellites can maintain sun-synchronous orbits. The Body class is meant to represent any physical object affected by gravity, which would include artificial satellites.

    [Edit: position above would be Earth's position relative to its parent, the Sun. I.e., this is its local translation, not its global translation. The Sun is not at global origin (0,0,0) for reasons discussed in this issue.]

    [Edit2: Come to think of it, why do you even need Earth's position? Your satellites should be children of Earth. Their position should be relative to Earth.]

    [Edit3: simplified code a bit to take advantage of Global.]
    Hello, 
    I've been trying to get the earths global position. To do this, I used the above code for both the earth and sun, and added the vectors. However, this code:
    var sun: Body = Global.bodies_by_name.STAR_SUN
    var sun_orbit: Orbit = sun.orbit
    var sun_position: Vector3 = sun_orbit.get_position(time)
    Returns the error "Invalid call. Nonexistent function 'get_position' in base 'Nil'.

    Looking in the debugger, it says that sun_orbit is [null], so I'm assuming that's why it can't call get_position(). That then means that getting the sun's body is incorrect. Is STAR_SUN the right name?

    Thanks

    (note: the code works perfectly when getting the earths position)


Sign In or Register to comment.