-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathphysics.py
207 lines (163 loc) · 7.11 KB
/
physics.py
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
import sys
from math import copysign, inf
import pygame
from vectors import Vector2D
# from https://stackoverflow.com/a/20677983
def line_intersection(line1, line2):
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
def det(a, b):
return a[0] * b[1] - a[1] * b[0]
div = det(xdiff, ydiff)
if div == 0:
return None
d = (det(*line1), det(*line2))
x = det(d, xdiff) / div
y = det(d, ydiff) / div
return Vector2D(x, y)
class RigidBody:
def __init__(self, width, height, x, y, angle=0.0, mass=None, restitution=0.5):
self.position = Vector2D(x, y)
self.width = width
self.height = height
self.angle = angle
self.velocity = Vector2D(0.0, 0.0)
self.angular_velocity = 0.0
self.torque = 0.0
self.forces = Vector2D(0.0, 0.0)
if mass is None:
mass = width * height
self.mass = mass
self.restitution = restitution
self.inertia = mass * (width ** 2 + height ** 2) / 12
self.sprite = pygame.Surface((width, height))
self.sprite.set_colorkey((0, 0, 0))
self.sprite.fill((0, 0, 0))
pygame.draw.rect(self.sprite, (255, 255, 255), (0, 0, width - 2, height - 2), 2)
def draw(self, surface):
rotated = pygame.transform.rotate(self.sprite, self.angle)
rect = rotated.get_rect()
surface.blit(rotated, self.position - (rect.width / 2, rect.height / 2))
def add_world_force(self, force, offset):
if abs(offset[0]) <= self.width / 2 and abs(offset[1]) <= self.height / 2:
self.forces += force
self.torque += offset.cross(force.rotate(self.angle))
def add_torque(self, torque):
self.torque += torque
def reset(self):
self.forces = Vector2D(0.0, 0.0)
self.torque = 0.0
def update(self, dt):
acceleration = self.forces / self.mass
self.velocity += acceleration * dt
self.position += self.velocity * dt
angular_acceleration = self.torque / self.inertia
self.angular_velocity += angular_acceleration * dt
self.angle += self.angular_velocity * dt
self.reset()
@property
def vertices(self):
return [
self.position + Vector2D(v).rotate(-self.angle) for v in (
(-self.width / 2, -self.height / 2),
(self.width / 2, -self.height / 2),
(self.width / 2, self.height / 2),
(-self.width / 2, self.height / 2)
)
]
@property
def edges(self):
return [
Vector2D(v).rotate(self.angle) for v in (
(self.width, 0),
(0, self.height),
(-self.width, 0),
(0, -self.height),
)
]
def collide(self, other):
# Exit early for optimization
if (self.position - other.position).length() > max(self.width, self.height) + max(other.width, other.height):
return False, None, None
def project(vertices, axis):
dots = [vertex.dot(axis) for vertex in vertices]
return Vector2D(min(dots), max(dots))
collision_depth = sys.maxsize
collision_normal = None
for edge in self.edges + other.edges:
axis = Vector2D(edge).orthogonal().normalize()
projection_1 = project(self.vertices, axis)
projection_2 = project(other.vertices, axis)
min_intersection = max(min(projection_1), min(projection_2))
max_intersection = min(max(projection_1), max(projection_2))
overlapping = min_intersection <= max_intersection
if not overlapping:
return False, None, None
else:
overlap = max_intersection - min_intersection
if overlap < collision_depth:
collision_depth = overlap
collision_normal = axis
return True, collision_depth, collision_normal
def get_collision_edge(self, normal):
max_projection = -sys.maxsize
support_point = None
vertices = self.vertices
length = len(vertices)
for i, vertex in enumerate(vertices):
projection = vertex.dot(normal)
if projection > max_projection:
max_projection = projection
support_point = vertex
if i == 0:
right_vertex = vertices[-1]
else:
right_vertex = vertices[i - 1]
if i == length - 1:
left_vertex = vertices[0]
else:
left_vertex = vertices[i + 1]
if right_vertex.dot(normal) > left_vertex.dot(normal):
return (right_vertex, support_point)
else:
return (support_point, left_vertex)
class PhysicsWorld:
def __init__(self):
self.bodies = []
def add(self, *bodies):
self.bodies += bodies
for body in bodies:
print("Body added", id(body))
def remove(self, body):
self.bodies.remove(body)
print("Body removed", id(body))
def update(self, dt):
tested = []
for body in self.bodies:
for other_body in self.bodies:
if other_body not in tested and other_body is not body:
collision, depth, normal = body.collide(other_body)
if collision:
normal = normal.normalize()
rel_vel = (body.velocity - other_body.velocity)
j = -(1 + body.restitution) * rel_vel.dot(normal) / normal.dot(
normal * (1 / body.mass + 1 / other_body.mass))
direction = body.position - other_body.position
magnitude = normal.dot(direction)
if body.mass != inf:
body.position += normal * depth * copysign(1, magnitude)
if other_body.mass != inf:
other_body.position -= normal * depth * copysign(1, magnitude)
body.velocity = body.velocity + j / body.mass * normal
other_body.velocity = other_body.velocity - j / other_body.mass * normal
body_collision_edge = body.get_collision_edge(-direction)
other_body_collision_edge = other_body.get_collision_edge(direction)
contact_point = line_intersection(body_collision_edge, other_body_collision_edge)
if contact_point:
radius = (body.position - contact_point)
body.angular_velocity = body.angular_velocity + (radius.dot(j * normal / body.inertia))
radius = (other_body.position - contact_point)
other_body.angular_velocity = other_body.angular_velocity - (
radius.dot(j * normal / other_body.inertia))
tested.append(body)
body.update(dt)