-
Notifications
You must be signed in to change notification settings - Fork 0
/
solar system position computation v.1.py
177 lines (120 loc) · 4.54 KB
/
solar system position computation v.1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from ast import While
from operator import truediv
from re import M
from turtle import distance
import pygame
import math
pygame.init()
Auther = "Vyoam"
WIDTH, HEIGHT = 900 , 900
WIN = pygame.display.set_mode((WIDTH , HEIGHT))
pygame.display.set_caption("<<<SOLAR SYSTEM SIMULATION>>>")
WHITE = (255,255,255,255)
YELOW = (255,255,0)
BLUE = (100, 149, 237)
RED = (188, 39, 50)
DARK_GREY = (80 , 78, 81)
FONT = pygame.font.SysFont("comicsans", 16)
ps = 1.094 # 1 approx
# perfect value for AU distance scale multiplication is 1.094
# as taking same disatnce as of the AU could not allow vissible elliptical axis
px = 1/ps
# made px(pixel scale) scale of absrtact visual reperesentation inversely proprtional to AU' s scale
class Celestial:
AU =149.6e6 * 1000 * ps
GRAV = 6.67428e-11
SCALE = 190 / AU # 1 AU = 100 pix
TIMESTEP = 3600 * 24 # 1 day EARTH
def __init__(self , x, y, radius, color, mass):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.mass = mass # kilograms
self.x_vel = 0
self.y_vel = 0
# FOR DRAWING ORBIT
# .
# .
# V
self.orbit = []
self.is_sun = False
self.distance_from_sun = 0
def draw(self, win):
x = self.x * self.SCALE + WIDTH / 2
y = self.y * self.SCALE + HEIGHT / 2
if len(self.orbit) > 2:
updated_points = []
for point in self.orbit:
x , y = point
x = x * self.SCALE + WIDTH / 2
y = y * self.SCALE + HEIGHT / 2
updated_points.append(( x , y ))
# DRAWNG LINES
pygame.draw.lines( win , self.color , False , updated_points , 1 )
# DRAWING ORBITS
pygame.draw.circle(win, self.color , (x,y) , self.radius)
# DISTANCE DISPLAYING
if not self.is_sun:
distance_text = FONT.render(f"{ self.distance_from_sun / 1000, 1 }Km", 1 , WHITE )
win.blit( distance_text , ( x - distance_text.get_width() / 2 , y - distance_text.get_height() / 2 ))
def attraction(self, other):
other_x, other_y = other.x , other.y
distance_x = other_x - self.x
distance_y = other_y - self.y
# USING PYTHAGOREAN THEOREM ----- for calculating distance betweem bodies-------
distance = math.sqrt(distance_x ** 2 + distance_y ** 2)
if other.is_sun :
self.distance_from_sun = distance
# force calculatiom
force = self.GRAV * self.mass * other.mass / distance ** 2
theta = math.atan2(distance_y, distance_x) # gives arc tangent of y/x // both's signs are considered
force_x = math.cos(theta) * force
force_y = math.sin(theta) * force
return force_x, force_y
def update_position(self , Celestials):
total_fx = total_fy = 0
for celestial in Celestials:
if self == celestial:
continue
# UPDATING POSITIONS
fx , fy = self.attraction(celestial)
# FORCE COMPONENTS UPDATION
total_fx += fx
total_fy += fy
# VELOCITIES
self.x_vel += total_fx / self.mass * self.TIMESTEP
self.y_vel += total_fy / self.mass * self.TIMESTEP
self.x += self.x_vel * self.TIMESTEP
self.y += self.y_vel * self.TIMESTEP
self.orbit.append((self.x, self.y))
def main ():
run = True
clock = pygame.time.Clock()
sun = Celestial(0, 0 , 30 * px ,YELOW, 1.98892 * 10 ** 30)
sun.is_sun = True
#--------------- velocity in m/s -----------------
# ...........(-,+) decide direction...............
# 30, 16, 12, 8, 14
# 9, 1 ,0.5, 0.2, 0.1
earth = Celestial(-1 * Celestial.AU , 0 , 16 * px ,BLUE, 5.9742* 10 **24)
earth.y_vel = 29.783 * 1000
mars = Celestial(-1.524 * Celestial.AU , 0 , 12 * px , RED , 6.39 * 10 ** 23)
mars.y_vel = 24.077 * 1000
mercury = Celestial( 0.387 * Celestial.AU, 0 , 8 * px, DARK_GREY, 3.30 * 10 ** 23 )
mercury.y_vel = -47.4 * 1000
venus = Celestial( 0.723 * Celestial.AU , 0 , 14 * px , WHITE , 4.8685 * 10 **24)
venus.y_vel = -35.02 * 1000
Celestials = [sun , earth , mars , mercury, venus]
while run :
clock.tick(60)
WIN.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for celestial in Celestials:
celestial.update_position(Celestials)
celestial.draw(WIN)
pygame.display.update()
pygame.quit()
main()