太阳系模拟 - 我的计算哪里出错了?
Solar System Simulation - Where are my calculations going wrong?
我正在使用物理方程、x 和 y 坐标以及使用二维列表的字典为项目制作二维太阳系模拟。我正在使用 tkinter canvas 来构建动画。
行星'earth'似乎从屏幕上消失了,起初加速度很慢,不久之后加速度很大。我看不出问题。
之前,我只有地球绕着太阳转,这成功地以类似的方式使用方程式,并通过计算出的位移的 x 和 y 分量在 canvas 上移动地球。字典按 {(-body):[[x, y], [x velocity, y velocity], mass, [change in x displacement, change in y displacement]}
排序。如果需要,将计算出的值存储或添加到该字典中的某些值。这是我正在使用的代码:
G = 6.67384 * 10 ** -11
scale = 10 ** 13
speed = 1
global user_status
screen = Tk()
screen.title('Solar System' + ' - ' + user_status)
screen.geometry('1300x700')
ani = Canvas(screen, width=1300, height=700)
ani.pack()
ani.create_rectangle(0, 0, 1300, 700, fill='Black')
sunx = 636
suny = 343
sun = ani.create_oval(sunx-10, suny-10, sunx+10, suny+10, fill='yellow')
earthx = 746
earthy = 343
moonx = 747
moony = 343
bodies = {'sun': [[sunx, suny], [0, 0], 1.989 * 10 ** 30 * speed / scale, [0, 0]],
'earth':[[earthx, earthy], [0, 347.3833062 * 1.033447099], 5.972 * 10 ** 24 * speed / scale, [0, 0]],
'moon': [[moonx, moony], [0, 360], 7.34767309 * 10 ** 22 * speed / scale, [0, 0]]
}
body_names = []
for Ω in bodies.keys():
body_names.append(Ω)
moon = ani.create_oval(moonx - 4, moony - 4, moonx + 4, moony + 4, fill='grey70')
earth = ani.create_oval(earthx-6, earthy-6, earthx+6, earthy+6, fill='sky blue')
timestep = 0.0001
while True:
for i in range(len(body_names)):
body1 = body_names[i]
x1 = bodies[body1][0][0]
y1 = bodies[body1][0][1]
total_Fx = 0
total_Fy = 0
body1_mass = bodies[body1][2]
for j in range(len(body_names)):
body2 = body_names[j]
if body1 != body2:
x2 = bodies[body2][0][0]
y2 = bodies[body2][0][1]
body2_mass = bodies[body2][2]
r = sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
rx = (x1 - x2)
angle = (acos(rx/r))
F = (G * body1_mass * body2_mass) / (r ** 2)
Fx = F * cos(angle)
Fy = F * sin(angle)
total_Fx += Fx
total_Fy += Fy
ax = (total_Fx / body1_mass)
ay = (total_Fy / body1_mass)
ux = bodies[body1][1][0]
uy = bodies[body1][1][1]
vx = ux - (ax * timestep)
if x1 <= sunx:
vy = uy + (ay * timestep)
else:
vy = uy - (ay * timestep)
sx = vx * timestep * speed
sy = vy * timestep * speed
bodies[body1][3][0] = sx
bodies[body1][3][1] = sy
bodies[body1][1][0] += vx
bodies[body1][1][1] += vy
bodies[body1][0][0] = x1 + sx
bodies[body1][0][1] = y1 + sy
print(bodies[body1][1], body1)
move_e_x = bodies['earth'][3][0]
move_e_y = bodies['earth'][3][1]
ani.move(earth, move_e_x, move_e_y)
move_m_x = bodies['moon'][3][0]
move_m_y = bodies['moon'][3][1]
ani.move(moon, move_m_x, move_m_y)
ani.update()
我希望物体同时绕太阳公转并相互环绕,但在物体远离屏幕后我收到此错误消息:
Traceback (most recent call last):
File "/Users/apple/Documents/School/Computer Science/NEA/NEA Programming/Solar System Simulator.py", line 380, in <module>
simulate() # Calls the 'simulate' function
File "/Users/apple/Documents/School/Computer Science/NEA/NEA Programming/Solar System Simulator.py", line 290, in simulate
r = sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
OverflowError: (34, 'Result too large')
我知道这可能不是一段非常有效的代码,但我所需要的只是一些关于如何以这种方式进行的帮助。问题似乎出现在位移或速度计算中。有什么想法吗?
初始速度被接收两次:在每个循环中,初始速度被调用和使用,所以它本身和新速度加在一起,导致它每次都增加一倍以上。
现在计算新添加的速度,然后仅将其添加到初始速度一次。行星现在绕轨道运行。目前有 9 个天体绕太阳公转。
我正在使用物理方程、x 和 y 坐标以及使用二维列表的字典为项目制作二维太阳系模拟。我正在使用 tkinter canvas 来构建动画。
行星'earth'似乎从屏幕上消失了,起初加速度很慢,不久之后加速度很大。我看不出问题。
之前,我只有地球绕着太阳转,这成功地以类似的方式使用方程式,并通过计算出的位移的 x 和 y 分量在 canvas 上移动地球。字典按 {(-body):[[x, y], [x velocity, y velocity], mass, [change in x displacement, change in y displacement]}
排序。如果需要,将计算出的值存储或添加到该字典中的某些值。这是我正在使用的代码:
G = 6.67384 * 10 ** -11
scale = 10 ** 13
speed = 1
global user_status
screen = Tk()
screen.title('Solar System' + ' - ' + user_status)
screen.geometry('1300x700')
ani = Canvas(screen, width=1300, height=700)
ani.pack()
ani.create_rectangle(0, 0, 1300, 700, fill='Black')
sunx = 636
suny = 343
sun = ani.create_oval(sunx-10, suny-10, sunx+10, suny+10, fill='yellow')
earthx = 746
earthy = 343
moonx = 747
moony = 343
bodies = {'sun': [[sunx, suny], [0, 0], 1.989 * 10 ** 30 * speed / scale, [0, 0]],
'earth':[[earthx, earthy], [0, 347.3833062 * 1.033447099], 5.972 * 10 ** 24 * speed / scale, [0, 0]],
'moon': [[moonx, moony], [0, 360], 7.34767309 * 10 ** 22 * speed / scale, [0, 0]]
}
body_names = []
for Ω in bodies.keys():
body_names.append(Ω)
moon = ani.create_oval(moonx - 4, moony - 4, moonx + 4, moony + 4, fill='grey70')
earth = ani.create_oval(earthx-6, earthy-6, earthx+6, earthy+6, fill='sky blue')
timestep = 0.0001
while True:
for i in range(len(body_names)):
body1 = body_names[i]
x1 = bodies[body1][0][0]
y1 = bodies[body1][0][1]
total_Fx = 0
total_Fy = 0
body1_mass = bodies[body1][2]
for j in range(len(body_names)):
body2 = body_names[j]
if body1 != body2:
x2 = bodies[body2][0][0]
y2 = bodies[body2][0][1]
body2_mass = bodies[body2][2]
r = sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
rx = (x1 - x2)
angle = (acos(rx/r))
F = (G * body1_mass * body2_mass) / (r ** 2)
Fx = F * cos(angle)
Fy = F * sin(angle)
total_Fx += Fx
total_Fy += Fy
ax = (total_Fx / body1_mass)
ay = (total_Fy / body1_mass)
ux = bodies[body1][1][0]
uy = bodies[body1][1][1]
vx = ux - (ax * timestep)
if x1 <= sunx:
vy = uy + (ay * timestep)
else:
vy = uy - (ay * timestep)
sx = vx * timestep * speed
sy = vy * timestep * speed
bodies[body1][3][0] = sx
bodies[body1][3][1] = sy
bodies[body1][1][0] += vx
bodies[body1][1][1] += vy
bodies[body1][0][0] = x1 + sx
bodies[body1][0][1] = y1 + sy
print(bodies[body1][1], body1)
move_e_x = bodies['earth'][3][0]
move_e_y = bodies['earth'][3][1]
ani.move(earth, move_e_x, move_e_y)
move_m_x = bodies['moon'][3][0]
move_m_y = bodies['moon'][3][1]
ani.move(moon, move_m_x, move_m_y)
ani.update()
我希望物体同时绕太阳公转并相互环绕,但在物体远离屏幕后我收到此错误消息:
Traceback (most recent call last):
File "/Users/apple/Documents/School/Computer Science/NEA/NEA Programming/Solar System Simulator.py", line 380, in <module>
simulate() # Calls the 'simulate' function
File "/Users/apple/Documents/School/Computer Science/NEA/NEA Programming/Solar System Simulator.py", line 290, in simulate
r = sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
OverflowError: (34, 'Result too large')
我知道这可能不是一段非常有效的代码,但我所需要的只是一些关于如何以这种方式进行的帮助。问题似乎出现在位移或速度计算中。有什么想法吗?
初始速度被接收两次:在每个循环中,初始速度被调用和使用,所以它本身和新速度加在一起,导致它每次都增加一倍以上。
现在计算新添加的速度,然后仅将其添加到初始速度一次。行星现在绕轨道运行。目前有 9 个天体绕太阳公转。