]> Skullheadx's Git Forge - Pygame-Jam.git/commitdiff
understood the physics bug
authorSkullheadx <704277@pdsb.net>
Fri, 8 Jul 2022 01:26:59 +0000 (21:26 -0400)
committerSkullheadx <704277@pdsb.net>
Fri, 8 Jul 2022 01:26:59 +0000 (21:26 -0400)
when an actor tries to cross axis lines, the move and collide method in Actors.py will not move as intended. its weird.

Actors.py
Block.py
Enemy.py
Game.py
PhysicsBody.py
Player.py
Setup.py
TODO
main.py

index 3673b60e7ff229c86a366187893f9dde56a23421..0e37724483bc67711428441688b876af6c6aa308 100644 (file)
--- a/Actors.py
+++ b/Actors.py
@@ -91,13 +91,12 @@ class Actor:
                             thing.position.x = pos.x + self.width
                         elif vel.x < 0:
                             thing.position.x = pos.x - thing.width
-                    else:
-                        if vel.x > 0:
-                            pos.x = thing.position.x - self.width
-                            vel.x = min(vel.x, 0)
-                        elif vel.x < 0:
-                            pos.x = thing.position.x + thing.width
-                            vel.x = max(vel.x, 0)
+                    if vel.x > 0:
+                        pos.x = thing.position.x - self.width
+                        vel.x = min(vel.x, 0)
+                    elif vel.x < 0:
+                        pos.x = thing.position.x + thing.width
+                        vel.x = max(vel.x, 0)
                     collision_rect = self.get_collision_rect(pos)
 
         self.on_ground = False
@@ -111,6 +110,7 @@ class Actor:
                     if area.is_colliding(thing):
                         area.signal(thing)
                 if collision_rect.colliderect(thing.get_collision_rect()):
+                    # print(thing, pos, vel, thing.position, thing.velocity)
                     if vel.y > 0:
                         pos.y = thing.position.y - self.height
                         vel.y = min(vel.y, 0)
index 6e77b193da44001cc1dec20c6227a9f82a43f593..fcf4a4e2f8cf084fce20e677888bd58014029be5 100644 (file)
--- a/Block.py
+++ b/Block.py
@@ -2,7 +2,7 @@ from Setup import *
 
 
 class Block:
-    width, height = SCREEN_WIDTH, 50
+    width, height = SCREEN_WIDTH*4/5, 50
     colour = (71, 77, 97)
 
     def __init__(self, pos, collision_layer):
index 054fcb5e955963554e033d8111274d51d18081b4..39d422bc3751d512f58a0bd7fe11472c92d02e5f 100644 (file)
--- a/Enemy.py
+++ b/Enemy.py
@@ -32,6 +32,7 @@ class Enemy(Actor):
         self.dizzy_time -= delta
         self.dizzy_time = max(0,self.dizzy_time)
 
+
         # Deals with collision and applying velocity
         self.position, self.velocity = self.move_and_collide(self.position.copy(), self.velocity.copy(), delta)
 
diff --git a/Game.py b/Game.py
index 20098f068ff0f3943a3c21fd037e5bd0321bb5bd..936e4f0433c7344e8fc1b8a8bcec8d36e4c5c404 100644 (file)
--- a/Game.py
+++ b/Game.py
@@ -8,17 +8,19 @@ from Block import Block
 from PhysicsBody import PhysicsBody
 from EndScreen import EndScreen
 
+
 class Game:
 
     def __init__(self):
-        self.collision_layer = {"world":set(),"player": set(), "enemy":set(), "pet":set()}
+        self.collision_layer = {"world": set(), "player": set(), "enemy": set(), "pet": set()}
 
-        self.player = Player(center, self.collision_layer["player"], [self.collision_layer["enemy"], self.collision_layer["world"]])
+        self.player = Player(center, self.collision_layer["player"],
+                             [self.collision_layer["enemy"], self.collision_layer["world"]])
         # self.pet = Pet(center, self.collision_layer["pet"], [self.collision_layer["world"]])
 
-        self.enemies = [Enemy((SCREEN_WIDTH * 3 /4, 0),self.collision_layer["enemy"], [self.collision_layer["player"], self.collision_layer["world"]])]
-        self.blocks = [Block((0, SCREEN_HEIGHT * 3 / 4),self.collision_layer["world"]),
-                       Block((SCREEN_WIDTH, SCREEN_HEIGHT * 3 / 4 - 25),self.collision_layer["world"])]
+        self.enemies = [Enemy((SCREEN_WIDTH * 3 / 4, SCREEN_HEIGHT / 2), self.collision_layer["enemy"],
+                              [self.collision_layer["player"], self.collision_layer["world"]])]
+        self.blocks = [Block((0, SCREEN_HEIGHT * 3 / 4), self.collision_layer["world"])]
 
         self.scene = EndScreen()
         self.level = 1
@@ -29,17 +31,17 @@ class Game:
         if self.player.dead:
             self.level = self.scene.level
 
-        for i,enemy in enumerate(self.enemies):
+        for i, enemy in enumerate(self.enemies):
             enemy.update(delta, self.player)
             if enemy.dead:
-                self.enemies[i] = PhysicsBody(enemy.position,enemy.velocity,enemy.width,enemy.height,enemy.colour,enemy.collision_layer,enemy.collision_mask)
+                self.enemies[i] = PhysicsBody(enemy.position, enemy.velocity, enemy.width, enemy.height, enemy.colour,
+                                              enemy.collision_layer, enemy.collision_mask)
                 self.collision_layer["enemy"].remove(enemy)
                 self.collision_layer["enemy"].add(self.enemies[i])
 
         for block in self.blocks:
             block.update(delta)
 
-
         # self.pet.update(delta, self.player, self.camera_pos)
 
     def draw(self, surf):
@@ -54,4 +56,8 @@ class Game:
         if self.player.dead:
             self.scene.update()
             self.scene.draw()
+
+        # Debug Lines. DO NOT CROSS THEM!
+        pg.draw.line(surf, (255, 0, 0), -Setup.camera_offset, pg.Vector2(SCREEN_WIDTH, -Setup.camera_offset.y), 10)
+        pg.draw.line(surf, (255, 0, 0), -Setup.camera_offset, pg.Vector2(-Setup.camera_offset.x, SCREEN_HEIGHT), 10)
         # self.pet.draw(surf)
index 0d9555447b65cb581642039cbeab7426c2da006d..e827a1fb9e98d597b0d29c07c127d55c1183d52c 100644 (file)
@@ -31,7 +31,8 @@ class PhysicsBody:
         # Apply gravity
         self.velocity.y += self.gravity
 
-        self.position, self.velocity = self.move_and_collide(self.position, self.velocity, delta)
+        self.position, self.velocity = self.move_and_collide(self.position.copy(), self.velocity.copy(), delta)
+        # print(self.position)
 
     def move_and_collide(self, pos, vel, delta):
         pos.x += vel.x * delta
@@ -42,13 +43,16 @@ class PhysicsBody:
                     continue
                 if collision_rect.colliderect(thing.get_collision_rect()):
                     if thing.movable:
-                        thing.velocity.x = vel.x
+                        if vel.x > 0:
+                            thing.position.x = pos.x + self.width
+                        elif vel.x < 0:
+                            thing.position.x = pos.x - thing.width
                     if vel.x > 0:
                         pos.x = thing.position.x - self.width
-                        vel.x = min(vel.x+ thing.velocity.x, 0)
+                        vel.x = min(vel.x, 0)
                     elif vel.x < 0:
                         pos.x = thing.position.x + thing.width
-                        vel.x = max(vel.x+ thing.velocity.x, 0)
+                        vel.x = max(vel.x, 0)
                     collision_rect = self.get_collision_rect(pos)
         self.on_ground = False
         pos.y += vel.y * delta
@@ -58,16 +62,13 @@ class PhysicsBody:
                 if thing == self:
                     continue
                 if collision_rect.colliderect(thing.get_collision_rect()):
-                    if thing.movable:
-                        thing.velocity.y = vel.y
-
                     if vel.y > 0:
                         pos.y = thing.position.y - self.height
-                        vel.y = min(vel.y+ thing.velocity.y, 0)
+                        vel.y = min(vel.y, 0)
                         self.on_ground = True
                     elif vel.y < 0:
                         pos.y = thing.position.y + thing.height
-                        vel.y = max(vel.y+ thing.velocity.y, 0)
+                        vel.y = max(vel.y, 0)
                     collision_rect = self.get_collision_rect(pos)
         return pos, vel
 
index 83889de5b7f3e9f7c4735e3f682ce419fe1b9f1c..49742dfe40341e394e7f27a82928f735a7011a0c 100644 (file)
--- a/Player.py
+++ b/Player.py
@@ -51,6 +51,7 @@ class Player(Actor):
 
         # Deals with collision and applying velocity
         self.position, self.velocity = self.move_and_collide(self.position.copy(), self.velocity.copy(), delta)
+        # print(self.velocity)
         return self.position - self.initial_position
 
         
index 93544b6109916caaecd6570a947f6b5819ea4027..98afeeda4968c4a25fed1b551484acc2fe9f38f4 100644 (file)
--- a/Setup.py
+++ b/Setup.py
@@ -17,7 +17,6 @@ pg.display.set_caption("Jam")
 
 clock = pg.time.Clock()
 fps = 60
-
 screen = pg.display.set_mode(dimensions, pg.SCALED)
 
 
diff --git a/TODO b/TODO
index 8b0519c7d27d364f35e156501d9411e1bc358158..c9f5640ecb2adf54b3a233949484e157ae4be89b 100644 (file)
--- a/TODO
+++ b/TODO
@@ -6,6 +6,7 @@ Gameplay:
 Movement:
 - Custom Dash movement function for dashing
 - Dash bar that refills when the player can dash again
+- fix collision bug. push enemy off left side
 
 Art:
 - Finish assets
diff --git a/main.py b/main.py
index 5ae4753b65c14632ab5064043d47a560017879d1..e6927446db6e88cd995302ff3f8bdaabe7aba38d 100644 (file)
--- a/main.py
+++ b/main.py
@@ -7,6 +7,7 @@ delta = 1000//fps
 is_running = True
 
 scene = Menu()
+# scene = Game()
 old_level = 0
 level = 1