From 71e3f6157ef2448a4af0934c030f48c0debb9f88 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Mon, 7 Apr 2025 13:16:26 +0200 Subject: [PATCH] Fixes --- aabb.p8 | 18 +++++++++--------- aabb2.p8 | 47 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/aabb.p8 b/aabb.p8 index c52266c..ba330a3 100644 --- a/aabb.p8 +++ b/aabb.p8 @@ -3,7 +3,7 @@ version 42 __lua__ local player = { -- position - x = 64, + x = 67, y = 80, -- velocity @@ -15,23 +15,23 @@ local player = { self.dy = self.dy + 1 self.dy = min(self.dy, 10) - -- collision right - if self.dx > 0 and (isColliding(self.x + self.dx + 7, self.y) or isColliding(self.x + self.dx + 7, self.y + 7)) then - self.dx = 0 - end -- collision left if self.dx < 0 and (isColliding(self.x + self.dx, self.y) or isColliding(self.x + self.dx, self.y + 7)) then self.dx = 0 end - - -- collision down - if self.dy > 0 and (isColliding(self.x, self.y + self.dy + 7) or isColliding(self.x + 7, self.y + self.dy + 7)) then - self.dy = 0 + -- collision right + if self.dx > 0 and (isColliding(self.x + self.dx + 7, self.y) or isColliding(self.x + self.dx + 7, self.y + 7)) then + self.dx = 0 end + -- collision up if self.dy < 0 and (isColliding(self.x, self.y + self.dy) or isColliding(self.x + 7, self.y + self.dy)) then self.dy = 0 end + -- collision down + if self.dy > 0 and (isColliding(self.x, self.y + self.dy + 7) or isColliding(self.x + 7, self.y + self.dy + 7)) then + self.dy = 0 + end -- apply movement self.x = self.x + self.dx diff --git a/aabb2.p8 b/aabb2.p8 index 5629f3d..2e55452 100644 --- a/aabb2.p8 +++ b/aabb2.p8 @@ -3,7 +3,7 @@ version 42 __lua__ local player = { -- position - x = 64, + x = 67, y = 50, -- velocity @@ -16,20 +16,17 @@ local player = { -- cap the falling speed to 7px/frame self.dy = min(self.dy, 7) - -- collision right - if self.dx > 0 and (isColliding(self.x + self.dx + 7, self.y) or isColliding(self.x + self.dx + 7, self.y + 7)) then - self.dx = 0 - end -- collision left if self.dx < 0 and (isColliding(self.x + self.dx, self.y) or isColliding(self.x + self.dx, self.y + 7)) then + -- move the player against the left wall + self.x = self.x % 8 == 0 and self.x or flr(self.x / 8) * 8 self.dx = 0 end - - -- collision down - if self.dy > 0 and (isColliding(self.x, self.y + self.dy + 7) or isColliding(self.x + 7, self.y + self.dy + 7)) then - -- clip the player to the nearest block down - self.y = self.y % 8 == 0 and self.y or flr((self.y + 8) / 8) * 8 - self.dy = 0 + -- collision right + if self.dx > 0 and (isColliding(self.x + self.dx + 7, self.y) or isColliding(self.x + self.dx + 7, self.y + 7)) then + -- move the player against the right wall + self.x = self.x % 8 == 0 and self.x or flr((self.x + 8) / 8) * 8 + self.dx = 0 end -- collision up if self.dy < 0 and (isColliding(self.x, self.y + self.dy) or isColliding(self.x + 7, self.y + self.dy)) then @@ -37,6 +34,22 @@ local player = { self.y = self.y % 8 == 0 and self.y or flr(self.y / 8) * 8 self.dy = 0 end + -- collision down + if self.dy > 0 and (isColliding(self.x, self.y + self.dy + 7) or isColliding(self.x + 7, self.y + self.dy + 7)) then + -- align the player with the nearest block down + self.y = self.y % 8 == 0 and self.y or flr((self.y + 8) / 8) * 8 + self.dy = 0 + end + + -- diagonals + if self.dx ~= 0 and self.dy ~= 0 and isCollidingRect(self.x + self.dx, self.y + self.dy) then + self.dx = 0 + self.dy = 0 + -- align with the current cell + self.x = flr(self.x / 8) * 8 + self.y = flr(self.y / 8) * 8 + printh(self.x .. ", " .. self.y) + end -- apply movement self.x = self.x + self.dx @@ -52,6 +65,14 @@ function isColliding(x, y) return fget(mget(flr(x / 8), flr(y / 8)), 0) end +-- same as isColliding(), but will check the 4 corners at once +function isCollidingRect(x, y) + return isColliding(x, y) + or isColliding(x + 7, y) + or isColliding(x, y + 7) + or isColliding(x + 7, y + 7) +end + function getBlockCoords(x, y) return flr(x / 8) * 8, flr(y / 8) * 8 end @@ -62,12 +83,12 @@ end function _update() --left if btn(0) then - player.dx = -1 + player.dx = -2 end -- right if btn(1) then - player.dx = 1 + player.dx = 2 end -- Up