From: Skullheadx <704277@pdsb.net> Date: Fri, 8 Jul 2022 01:26:59 +0000 (-0400) Subject: understood the physics bug X-Git-Url: http://git.skullheadx.com/pfp.webp?a=commitdiff_plain;h=c9b669429a1eafe000bb3e5bde1c4a616f3f9f9d;p=Pygame-Jam.git understood the physics bug when an actor tries to cross axis lines, the move and collide method in Actors.py will not move as intended. its weird. --- diff --git a/Actors.py b/Actors.py index 3673b60..0e37724 100644 --- 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) diff --git a/Block.py b/Block.py index 6e77b19..fcf4a4e 100644 --- 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): diff --git a/Enemy.py b/Enemy.py index 054fcb5..39d422b 100644 --- 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 20098f0..936e4f0 100644 --- 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) diff --git a/PhysicsBody.py b/PhysicsBody.py index 0d95554..e827a1f 100644 --- a/PhysicsBody.py +++ b/PhysicsBody.py @@ -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 diff --git a/Player.py b/Player.py index 83889de..49742df 100644 --- 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 diff --git a/Setup.py b/Setup.py index 93544b6..98afeed 100644 --- 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 8b0519c..c9f5640 100644 --- 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 5ae4753..e692744 100644 --- 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