중력 물리엔진 질문에 오류가 발생하는데 이유를 모르겠어요

조회수 136회

import pygame from pygame import * import math class orb: def init(self, mass=1, s0=[0,0], v0=[0,0], a=[0,0], r=10, color=(255, 255, 255), fix = False, F=0, Fx=0, Fy=0): self.a= a self.mass=mass self.r=r self.t = 0 self.v0=v0 self.v=[self.v0[0], self.v0[1]] self.s0=s0 self.s=[self.s0[0], self.s0[1]] self.color=color self.fix = fix self.F=F self.Fx = Fx self.Fy = Fy

def comPos(self, dt = 0.01):
  if not self.fix:  
    self.v[0] = self.v0[0] + self.a[0] * dt
    self.v[1] = self.v0[1] + self.a[1] * dt
    ds_x = self.v0[0] * dt + (1/2) * self.a[0] * dt**2
    ds_y = self.v0[1] * dt + (1/2) * self.a[1] * dt**2
    self.s[0] = self.s0[0] + ds_x
    self.s[1] = self.s0[1] + ds_y
    self.v0[0] = self.v[0]
    self.v0[1] = self.v[1]
    self.s0[0] = self.s[0]
    self.s0[1] = self.s[1]

def comForce(self, other, G = 10000.0, F = 0.0, dist_cr = 0.0, Fx = 0.0, Fy = 0.0):
    self.F = G * self.mass * other.mass / ( (self.s[0]- other.s[0])**2 + (self.s[1] - other.s[1])**2 )
    self.Fx = (self.F * math.fabs(math.cos(math.atan((other.s[1] - self.s[1]) / (other.s[0] - self.s[0])))))
    self.Fy = (self.F * math.fabs(math.sin(math.atan((other.s[1] - self.s[1]) / (other.s[0] - self.s[0])))))
    if ((other.s[0] - self.s[0])**2 + (other.s[1] - self.s[1])**2)**(1/2) >= dist_cr:
        self.a[0] = (( (other.s[0] - self.s[0]) / math.fabs(other.s[0] - self.s[0])) * self.Fx / self.mass)
        if other.s[1] - self.s[1] == 0:
            self.a[1] = 0
        else:
            self.a[1] = (( (other.s[1] - self.s[1]) / math.fabs(other.s[1] - self.s[1])) * self.Fy / self.mass)

def a_reset(self):
    self.a=[0, 0]

screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() earth = orb(s0=[150, 300], v0=[0,60], r=10, mass= 3) sun = orb(s0=[400, 300], r=70, color = [255, 0, 0], fix=True, mass=100) orb = [sun, earth]

running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((0, 0, 0)) pygame.draw.circle(screen, earth.color, earth.s, earth.r, 0) pygame.draw.circle(screen, sun.color, sun.s, sun.r, 0) earth.comForce(sun) earth.comPos()

pygame.display.update()
clock.tick(720)

이 파이썬 코드를 작동시키면 흰 공이 빨간 공 주위를 돌게 되는데 시간이 지날수록 점점 도는 궤도가 달라져요. 그런데 원래는 그러면 안 되는데 변하는 이유를 도저히 모르겠어요.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)