]> Skullheadx's Git Forge - Collision-Simulation.git/commitdiff
Clean up files
authorSkullheadx <704277@pdsb.net>
Mon, 16 Jan 2023 23:27:31 +0000 (18:27 -0500)
committerSkullheadx <704277@pdsb.net>
Mon, 16 Jan 2023 23:27:31 +0000 (18:27 -0500)
box.py
collision.py
display.py
particle.py

diff --git a/box.py b/box.py
index cd0bf5b9ae0f0625313e06e5c92782af96a5e3c4..2e146309cd89b2f76a6914f66b4948dace7dd8c4 100644 (file)
--- a/box.py
+++ b/box.py
@@ -1,6 +1,7 @@
-from colours import *
 import pygame
 
+from colours import *
+
 
 class Box:
     line_thickness = 5
index 2ad00d2fd861f36419feaea379342e1456dd29a5..7bd892c79a2d0f843f8b052a7c96a4543e137df5 100644 (file)
@@ -1,4 +1,4 @@
-def handleBoxCollision(particle, box):
+def handleBoxCollision(particle, box):  # Discrete Collision Detection
     if particle.left <= box.left or particle.right >= box.right:
         particle.velocity.x *= -1
     if particle.bottom >= box.bottom or particle.top <= box.top:
index e5fd63e72c20d0bc43df4afe4128781ba6e8c11d..77f738e3006f1af0949b2708db00a0883f78b4cc 100644 (file)
@@ -1,7 +1,8 @@
+import pygame
+
+from box import Box
 from colours import *
 from particle import Particle
-from box import Box
-import pygame
 
 pygame.init()
 
index 0f71de8ba3d1655b1898a4eb5f286d1ec5c2f08f..6e6f742349b0873068b71a7f4d84e880245d6e9c 100644 (file)
@@ -1,8 +1,10 @@
-import pygame
 from math import copysign
-from colours import *
+
+import pygame
+
 from box import Box
 from collision import handleBoxCollision
+from colours import *
 
 
 class Particle:
@@ -21,14 +23,22 @@ class Particle:
 
         self.collision_layer = collision_layer
 
-    def update(self, delta):
-        self.velocity.x = min(self.speed_limit, abs(self.velocity.x + self.acceleration.x * delta))\
-                          * copysign(1, self.velocity.x + self.acceleration.x * delta)
-        self.velocity.y = min(self.speed_limit, abs(self.velocity.y + self.acceleration.y * delta))\
-                          * copysign(1, self.velocity.y + self.acceleration.y * delta)
+    def get_next_frame(self, position, velocity, delta):
+        vel = pygame.Vector2()
+        pos = pygame.Vector2()
+
+        vel.x = min(self.speed_limit, abs(velocity.x + self.acceleration.x * delta)) \
+                * copysign(1, velocity.x + self.acceleration.x * delta)
+        vel.y = min(self.speed_limit, abs(velocity.y + self.acceleration.y * delta)) \
+                * copysign(1, velocity.y + self.acceleration.y * delta)
+
+        pos.x = position.x + velocity.x * delta
+        pos.y = position.y + velocity.y * delta
 
-        self.position.x += self.velocity.x * delta
-        self.position.y += self.velocity.y * delta
+        return pos, vel
+
+    def update(self, delta):
+        self.position, self.velocity = self.get_next_frame(self.position, self.velocity, delta)
 
         self.left = self.position.x - self.radius
         self.right = self.position.x + self.radius