planet.gd 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. extends Spatial
  2. # Declare member variables here. Examples:
  3. # var a = 2
  4. # var b = "text"
  5. #These are terrible but will hopefully improve as problems become evident
  6. #Units are 1000km, hours, and degrees
  7. export var semimajor_axis = 100000
  8. export var semiminor_axis = 0
  9. export var angle_of_apoapsis = 42
  10. export var xtilt = 0
  11. export var ytilt = 0
  12. export var daylen = 6
  13. export var color = Color.blue
  14. var orbital_period
  15. var planet
  16. var eccentric_anomaly
  17. var mean_anomaly
  18. var eccentricity
  19. var focus
  20. # Called when the node enters the scene tree for the first time.
  21. func _ready():
  22. planet = get_child(0)
  23. self.rotate_y(deg2rad(angle_of_apoapsis))
  24. self.rotate_x(deg2rad(xtilt))
  25. self.rotate_z(deg2rad(ytilt))
  26. eccentricity = sqrt(1 - pow(semiminor_axis,2)/pow(semimajor_axis,2))
  27. eccentric_anomaly = 0
  28. mean_anomaly = 0
  29. orbital_period = 2 * PI * sqrt(pow(semimajor_axis,3)/1.327e2)
  30. focus = sqrt(pow(semimajor_axis,2) - pow(semiminor_axis,2))
  31. var path = get_node("ImmediateGeometry")
  32. path.material_override.albedo_color = color
  33. path.begin(Mesh.PRIMITIVE_LINE_LOOP)
  34. for i in range(1000):
  35. var x = semimajor_axis * (cos(i/500.0 * PI) - eccentricity)
  36. var y = semiminor_axis * sin(i/500.0 * PI)
  37. path.add_vertex(Vector3(x,0,y))
  38. path.end()
  39. pass # Replace with function body.
  40. # Called every frame. 'delta' is the elapsed time since the previous frame.
  41. #func _process(delta):
  42. # pass
  43. # warning-ignore:unused_argument
  44. func _physics_process(delta):
  45. mean_anomaly += delta * 5000 / orbital_period * 60
  46. eccentric_anomaly = mean_anomaly
  47. # warning-ignore:unused_variable
  48. for x in range(1000): # yay brute force!
  49. eccentric_anomaly = mean_anomaly + eccentricity * sin(eccentric_anomaly)
  50. var x = semimajor_axis * (cos(eccentric_anomaly) - eccentricity)
  51. var y = semiminor_axis * sin(eccentric_anomaly)
  52. planet.translation = Vector3(x,0,y)
  53. pass