-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (58 loc) · 1.93 KB
/
main.py
File metadata and controls
84 lines (58 loc) · 1.93 KB
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
import pygame, sys
import numpy as np
pygame.init()
clock = pygame.time.Clock()
screen_width, screen_height = 900, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("PhysicsEngine")
obj_list = []
class Object():
def __init__(self, pos, vel, mass):
self.pos = np.array(pos, dtype=float)
self.vel = np.array(vel, dtype=float)
self.mass = mass
self.gravity = np.array([0, 9.81])
obj_list.append(self)
def update_pos(self,dt):
self.pos += self.vel * dt + 1/2 * self.gravity * dt**2
class Solver():
def update(self,dt):
self.update_pos(dt)
self.applyBoundary()
def update_pos(dt):
for obj in obj_list:
obj.update_pos(dt)
def applyBoundary():
center = np.array([screen_width/2,screen_height/2])
radius = 300
for obj in obj_list:
center_to_obj = obj.pos - center
dist = np.sqrt(center_to_obj.dot(center_to_obj))
if dist > (radius - 10):
n = center_to_obj/dist
obj.pos = center + n * (radius - 10)
def draw(surface):
screen.fill((0,0,0))
pygame.draw.circle(surface, (255,255,255),[screen_width/2, screen_height/2], 300)
for obj in obj_list:
pygame.draw.circle(surface, (255,0,0), obj.pos, 10)
def fps_counter(font_fps):
fps = str(int(clock.get_fps()))
text = font_fps.render(fps, True, (255,0,0))
screen.blit(text, (20,20))
def main():
Object([400,200],[0,0],1)
Object([200,200],[0,0],1)
font_fps = pygame.font.SysFont("Arial", 25)
while True:
for events in pygame.event.get():
if events.type == pygame.QUIT:
pygame.quit()
sys.exit()
Solver.update(Solver,1)
draw(screen)
fps_counter(font_fps)
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
main()