im making a pizza tower fangame, pizza tower has these masks for detecting if something has hit a wall. im trying to add this into LOVE2D via a CheckCollision function, but it doesnt seem to work.
function Player:CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
local ct = {left = false, right = false, up = false, down = false}
local col_x,col_y = nil,nil
if x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1 then
if x1 < x2 then
ct.left = true
col_x = x2
elseif x1 > x2 then
ct.right = true
col_x = x2+w2
end if y1 > y2+(h2/2) then
ct.down = true
col_y = y2
elseif y2+(h2/2) > y1 then
ct.up = true
col_y = y2+h2
end
if self.displayMask:getPixel(x1 - col_x, y1 - col_y) then
return ct
else
return false
end
end
return false
end
it display as a out ot bounds pixel, and im not even sure if i did the collision direction detection right. can someone help?
nicholasrickys wrote: ↑Sun May 14, 2023 10:05 am
first forum post
im making a pizza tower fangame, pizza tower has these masks for detecting if something has hit a wall. im trying to add this into LOVE2D via a CheckCollision function, but it doesnt seem to work.
heres my code
it display as a out ot bounds pixel, and im not even sure if i did the collision direction detection right. can someone help?
I have no idea what you make, but it must be like:
function Player:CheckCollision(x2,y2,w2,h2)
local x1,y1,w1,h1 = self.x, self.y, self.w, self.h
local dx, dy = nil,nil
if x1 < x2+w2 then
dx = 0
elseif x2 < x1+w1 then
dx = w1
else
return false
end
if y1 < y2+h2 then
dy = 0
elseif y2 < y1+h1 then
dy = h1
else
return false
end
if self.displayMask:getPixel(x1+dx-1, y1+dy-1) then
return x1+dx, y1+dy
else
return false
end
end