You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
785 B

  1. import math
  2. G = 6.67408E-11
  3. class RigidMass:
  4. def __init__(self, mass, vx, vy, x, y, tick):
  5. self.mass = mass
  6. self.vx = vx
  7. self.vy = vy
  8. self.x = x
  9. self.y = y
  10. self.dvx, self.dvy = 0, 0
  11. self.tick = tick #tick length, in seconds
  12. def gravity(self, p): #p is other particle
  13. v = -G * p.mass / self.distance_squared(p) * self.tick
  14. #print(self.distance_squared(p))
  15. #self.v = v #debugging
  16. dx = self.x - p.x
  17. dy = self.y - p.y
  18. hyp = math.sqrt(dx**2 + dy**2)
  19. self.dvx += v * (dx) / hyp
  20. self.dvy += v * (dy) / hyp
  21. def process_tick(self):
  22. self.vx += self.dvx
  23. self.vy += self.dvy
  24. self.dvx, self.dvy = 0, 0
  25. self.x += self.vx * self.tick
  26. self.y += self.vy * self.tick
  27. def distance_squared(self, p):
  28. return abs((self.x - p.x)**2 + (self.y - p.y)**2)