-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhysicsObjects.py
72 lines (54 loc) · 1.77 KB
/
PhysicsObjects.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
from dataclasses import dataclass, field
import numpy as np
@dataclass
class PhysicsObject:
_position: np.ndarray
_velocity: np.ndarray
_acceleration: np.ndarray
@property
def position(self):
return self._position
@property
def velocity(self):
return self._velocity
@property
def acceleration(self):
return self._acceleration
@position.setter
def position(self, position):
self._position = position
@velocity.setter
def velocity(self, velocity):
self._velocity = velocity
@acceleration.setter
def acceleration(self, acceleration):
self._acceleration = acceleration
def update(self, delta_time):
pass
class EulerPhysicsObject(PhysicsObject):
def update(self, delta_time):
self.position += self.velocity * delta_time
self.velocity += self.acceleration * delta_time
self.acceleration = np.zeros_like(self.acceleration)
@dataclass
class VerletPhysicsObject(PhysicsObject):
_old_position: np.ndarray = field(init=False)
def __post_init__(self):
self._old_position = self.position
@property
def old_position(self):
return self._old_position
@old_position.setter
def old_position(self, position):
self._old_position = position
@property
def velocity(self):
return self._velocity
@velocity.setter
def velocity(self, velocity):
self.position = self.position + velocity
def update(self, delta_time):
self.velocity = self.position - self.old_position
self.old_position = np.array(self.position)
self.position = self.position + self.velocity * delta_time + self.acceleration * delta_time
self.acceleration = np.zeros_like(self.acceleration)