-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics.py
More file actions
41 lines (33 loc) · 969 Bytes
/
Copy pathphysics.py
File metadata and controls
41 lines (33 loc) · 969 Bytes
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
GRAVITY = -981.0
PHYSICS_STEP = 1 / 120
class PhysicsEngine:
def __init__(self):
self.objects = []
def add_object(self, obj):
self.objects.append(obj)
def update(self, dt):
steps = int(dt // PHYSICS_STEP)
remainder = dt % PHYSICS_STEP
for _ in range(steps):
self._step(PHYSICS_STEP)
if remainder > 0:
self._step(remainder)
def _step(self, dt):
for obj in self.objects:
vx, vy, vz = getattr(obj, "velocity", (0, 0, 0))
x, y, z = obj.position
vy += GRAVITY * dt
x += vx * dt
y += vy * dt
z += vz * dt
if y < 0:
y = 0
vy = 0
if x < -5000:
x = -5000
vx = 0
elif x > 5000:
x = 5000
vx = 0
obj.velocity = (vx, vy, vz)
obj.position = (x, y, z)