needing help with collision and detecting if collision has hit "displayMask"

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
nicholasrickys
Prole
Posts: 1
Joined: Sun May 14, 2023 10:01 am

needing help with collision and detecting if collision has hit "displayMask"

Post by nicholasrickys »

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

Code: Select all

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?
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: needing help with collision and detecting if collision has hit "displayMask"

Post by darkfrei »

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:

Code: Select all

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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 11 guests