Skip to content

Commit 40aaaee

Browse files
authored
Merge pull request #76 from JHay0112/better-sim
More Stable Physics Simulation
2 parents aef4d6e + 8eec87f commit 40aaaee

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

jmath/physics/mechanics.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ def acceleration(self) -> Vector:
193193
# F = ma -> a = F/m
194194
return self.force() / self.mass
195195

196-
def increment_position(self, time: float = 0.1):
196+
def update_position(self, time: float = 0.1):
197197
"""
198198
Increments the position kinematically over a time interval.
199-
Also updates velocity.
199+
DOES NOT update velocity.
200200
201201
Parameters
202202
----------
@@ -205,6 +205,18 @@ def increment_position(self, time: float = 0.1):
205205
The time interval to approximate over.
206206
"""
207207
self.position = kinematic_position(self.position, self.velocity, self.acceleration(), time)
208+
209+
def update_velocity(self, time: float = 0.1):
210+
"""
211+
Updates the velocity kinematically over a time interval.
212+
DOES NOT update position.
213+
214+
Parameters
215+
----------
216+
217+
time
218+
The time interval to approximate over.
219+
"""
208220
self.velocity = kinematic_velocity(self.velocity, self.acceleration(), time)
209221

210222
@__non_zero_distance

jmath/physics/simulation.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,13 @@ def mainloop(self, time_interval, *args):
108108

109109
for object in self.objects:
110110

111-
object.increment_position(time_interval)
111+
object.update_velocity(time_interval)
112112

113-
if self.width < object.position[0] or object.position[0] < 0 or self.height < object.position[1] or object.position[1] < 0:
113+
for object in self.objects:
114+
115+
object.update_position(time_interval)
116+
117+
if self.width < object.shape.x or object.shape.x < 0 or self.height < object.shape.y or object.shape.y < 0:
114118
self.canvas.delete(object.shape.canvas_obj)
115119
self.objects.remove(object)
116120
else:

0 commit comments

Comments
 (0)