-
Notifications
You must be signed in to change notification settings - Fork 0
/
Verlet Physics Sandbox.pyw
218 lines (178 loc) · 8.39 KB
/
Verlet Physics Sandbox.pyw
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from time import perf_counter
from math import sqrt
from verlet.constraint import distance
import pygame
import verlet
from tkinter import Tk
temp = Tk()
SCREENWIDTH = temp.winfo_screenwidth()
SCREENHEIGHT = temp.winfo_screenheight()
temp.destroy()
del Tk, temp
FPS = 60
class App:
def __init__(self):
self.initializeWindow()
self.initializeWorld()
top = self.world.newNode(SCREENWIDTH / 2, SCREENHEIGHT / 2 - 300, pinned=True)
middle = self.world.newNode(SCREENWIDTH / 2, SCREENHEIGHT / 2)
bottom = self.world.newNode(SCREENWIDTH / 2, SCREENHEIGHT / 2 + 300, xVel=1000)
self.world.newConstraint(top, middle, stiffness=1)
self.world.newConstraint(middle, bottom, stiffness=1)\
while self.running:
self.frame()
pygame.quit()
def initializeWorld(self):
self.mousePressed = False
self.mouseX, self.mouseY = None, None
self.clickX, self.clickY = None, None
self.clickedNode = None
self.selectedNodes = []
self.showDebugText = False
self.visualizeForces = True
self.renderNodes = True
self.renderConstraints = True
self.running = True
self.playing = True
self.airFriction = 0
self.constraintIterations = 1
# Each pixel is equivalent to a foot (9.8 meters -> 32.1522 feet)
self.world = verlet.World((0, 32.1522), 1 - self.airFriction, self.boundary, 1 / FPS)
def initializeWindow(self):
pygame.init()
pygame.display.set_caption('2D Verlet Physics Sandbox')
self.screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT), pygame.FULLSCREEN)
self.font = pygame.font.SysFont('Consolas', 15)
self.clock = pygame.time.Clock()
def boundary(self, node):
x, y = node.x, node.y
if x < node.radius:
xVel = node.oldX - node.x
node.x = node.radius
if y < node.radius:
yVel = node.oldY - node.y
node.y = node.radius
if x > SCREENWIDTH - node.radius:
xVel = node.x - node.oldX
node.x = SCREENWIDTH - node.radius
if y > SCREENHEIGHT - node.radius:
yVel = node.y - node.oldY
node.y = SCREENHEIGHT - node.radius
def handleKeys(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
self.mousePressed = True
self.clickX, self.clickY = pygame.mouse.get_pos()
elif event.type == pygame.MOUSEBUTTONUP:
self.mousePressed = False
self.clickedNode = None
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.playing = not self.playing
elif event.key == pygame.K_d:
self.showDebugText = not self.showDebugText
def drawText(self, text, x, y):
textSurface = self.font.render(text, True, (50, 255, 100))
textRect = textSurface.get_rect()
textRect.topleft = x, y
self.screen.blit(textSurface, textRect)
def drawDebugText(self, t1, t2, t3):
uncappedTime = round(t3 - t1, 5)
physicsTime = round(t2 - t1, 5)
renderingTime = round(t3 - t2, 5)
physicsPercentage = round(physicsTime / uncappedTime * 100, 1)
renderingPercentage = round(renderingTime / uncappedTime * 100, 1)
squared = u'\u00b2'
gravityX = f'{str(self.world.gravityX)} u/sec{squared}' if self.world.gravityX else 'None'
gravityY = f'{str(self.world.gravityY)} u/sec{squared}' if self.world.gravityY else 'None'
text1 = f'{"Playing" if self.playing else "Paused"} | FPS: {self.clock.get_fps():.1f}/{FPS} ({round(1 / uncappedTime)})'
text2 = f'Physics: {str(physicsTime).ljust(9)} ({physicsPercentage}%)'
text3 = f' Nodes: {self.world.countNodes()}'
text4 = f' Constraints: {self.world.countConstraints()}'
text5 = f'Rendering: {str(renderingTime).ljust(7)} ({round(renderingTime / self.world.timeStep * 100)}%)'
text6 = f'Gravity: ({gravityX}, {gravityY})'
text7 = f'Aerial drag: {self.airFriction * 100}%'
text8 = f'Constraint iterations: {self.constraintIterations}'
self.drawText(text1, 15, 15)
self.drawText(text2, 15, 40)
self.drawText(text3, 15, 60)
self.drawText(text4, 15, 80)
self.drawText(text5, 15, 100)
self.drawText(text6, 15, 125)
self.drawText(text7, 15, 145)
self.drawText(text8, 15, 165)
def render(self):
self.screen.fill((10, 25, 50))
if self.renderConstraints:
for constraint in self.world.getConstraints():
x1, y1 = constraint.startNode.x, constraint.startNode.y
x2, y2 = constraint.endNode.x, constraint.endNode.y
actualDistance = distance(x1, y1, x2, y2)
color = [255, 255, 255]
if self.visualizeForces:
if actualDistance > constraint.length and not constraint.allowTension:
amount = (actualDistance - constraint.length) / actualDistance * 255 if actualDistance else 0
color[1] -= amount
color[2] -= amount
color[1] = max(color[1], 0)
color[2] = max(color[2], 0)
if actualDistance < constraint.length and not constraint.allowCompression:
amount = (constraint.length - actualDistance) / actualDistance * 255 if actualDistance else 0
color[0] -= amount
color[1] -= amount
color[0] = max(color[0], 0)
color[1] = max(color[1], 0)
pygame.draw.line(self.screen, color, (x1, y1), (x2, y2), width=3)
if self.renderNodes:
for node in self.world.getNodes():
pygame.draw.circle(self.screen, (255, 255, 255), (node.x, node.y), node.radius)
self.handleHighlighting()
def frame(self):
t1 = perf_counter()
self.mouseX, self.mouseY = pygame.mouse.get_pos()
self.handleDragging()
if self.playing:
self.world.update(exclusions=((self.clickedNode,), ()), constraintIterations=self.constraintIterations)
t2 = perf_counter()
self.render()
t3 = perf_counter()
self.clock.tick(FPS)
if self.showDebugText:
self.drawDebugText(t1, t2, t3)
pygame.display.flip()
self.handleKeys()
def handleDragging(self):
if not self.clickedNode:
if self.mousePressed:
closestNode = self.getClosestNode(self.clickX, self.clickY)
if closestNode:
self.clickedNode = closestNode
if self.clickedNode:
self.clickedNode.oldX, self.clickedNode.oldY = self.clickedNode.x, self.clickedNode.y
self.clickedNode.x, self.clickedNode.y = self.mouseX, self.mouseY
def handleHighlighting(self):
if self.clickedNode:
pygame.draw.circle(self.screen, (150, 150, 150), (self.clickedNode.x, self.clickedNode.y), self.clickedNode.radius + 8, width=3)
else:
closestNode = self.getClosestNode(self.mouseX, self.mouseY)
if closestNode:
pygame.draw.circle(self.screen, (70, 70, 70), (closestNode.x, closestNode.y), closestNode.radius + 8, width=3)
def getClosestNode(self, x, y):
nodesCloseToPoint = []
for node in self.world.getNodes():
distanceToNode = distance(x, y, node.x, node.y)
if distanceToNode <= 40:
nodesCloseToPoint.append(node)
if nodesCloseToPoint:
closestNode = nodesCloseToPoint[0]
for node in nodesCloseToPoint:
distanceToNode = distance(x, y, node.x, node.y)
if distanceToNode <= distance(x, y, closestNode.x, closestNode.y):
closestNode = node
else:
return None
return closestNode
if __name__ == '__main__':
App()