This commit is contained in:
Simon Cambier 2025-04-07 13:16:26 +02:00
parent ac60c09612
commit 71e3f6157e
2 changed files with 43 additions and 22 deletions

18
aabb.p8
View File

@ -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

View File

@ -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