-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
113 lines (82 loc) · 3.2 KB
/
Copy pathmain.py
File metadata and controls
113 lines (82 loc) · 3.2 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
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
import pygame as pg
import math
from pygame.math import Vector2 as vect2
import random as rand
pg.init()
w = pg.display.set_mode((700, 600))
pg.display.set_caption("Verlet Physics")
Running = True
clock = pg.time.Clock()
fps = 60
obj = []
class obj_:
def __init__(self, pos_current, pos_old, accel):
self.pos_current: vect2 = pos_current
self.pos_old: vect2 = pos_old
self.accel: vect2 = accel
obj.append(self)
def update(self, gravity, dt):
self.accel += gravity
self.vel = self.pos_current - self.pos_old
self.pos_old = self.pos_current
self.pos_current = self.pos_current + self.vel + self.accel * (dt ** 2)
self.accel = vect2()
def accelerate(self, acc):
self.accel += acc
c = vect2(700 / 2, 600 /2)
class Solver:
def __init__(self, gravity):
self.gravity: vect2 = gravity
def update(self, dt):
self.sub_steps = 3
self.sub_dt = dt / float(self.sub_steps)
for i in range(self.sub_steps, 0, -1):
self.apply_gravity(dt)
self.apply_cons()
self.solve_collision()
self.update_pos(dt)
def update_pos(self, dt):
for objs in obj:
objs.update(self.gravity, dt)
def apply_gravity(self, dt):
global w
for objs in obj:
pg.draw.circle(w, "#03a66b", objs.pos_current, 10.0, 0)
def apply_cons(self):
self.position = vect2(700 / 2, 600 / 2)
self.radius = 200
for objs in obj:
self.to_obj = objs.pos_current - self.position
self.dist: float = math.sqrt(self.to_obj.x ** 2 + self.to_obj.y ** 2)
if self.dist > self.radius - 50.0:
self.n = self.to_obj / self.dist
objs.pos_current = self.position + self.n * (self.radius - 50.0)
def solve_collision(self):
self.obj_count = len(obj)
for i in range(self.obj_count):
for k in range(i + 1, self.obj_count):
self.coll_axis: vect2 = obj[i].pos_current - obj[k].pos_current
self.dist = math.sqrt(self.coll_axis.x ** 2 + self.coll_axis.y ** 2)
if self.dist < 20:
self.n = self.coll_axis / self.dist
self.delta: float = 20 - self.dist
obj[i].pos_current += 0.5 * self.delta * self.n
obj[k].pos_current -= 0.5 * self.delta * self.n
gravity = vect2(0, 1000.0)
solver = Solver(gravity)
while Running:
clock.tick(60)
dt = clock.tick(fps) / 1000
w.fill("#296baa")
pg.draw.circle(w, "#0a0d22", (700 // 2, 600 // 2), 158.55, 0)
solver.update(dt)
mouse_x = pg.mouse.get_pos()[0]
mouse_y = pg.mouse.get_pos()[1]
mouse_pos = vect2(mouse_x,mouse_y)
for event in pg.event.get():
if event.type == pg.QUIT:
Running = False
if event.type == pg.MOUSEBUTTONUP:
if math.sqrt((mouse_pos.x - 700 / 2) ** 2 + (mouse_pos.y - 600 / 2) ** 2) <= 158.55:
new_obj = obj_(mouse_pos, mouse_pos, vect2(0, 0))
pg.display.update()