getPixel in a line

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
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

getPixel in a line

Post by szmol96 »

Hi guys.
I have bumped into a problem while making my own collision system. I know how to check for non-transparent pixels, but i have no idea how to check if there are any in a horizontal line. I have tried a for loop, but my character falls through the ground. Here is the function code:

Code: Select all

function maskedHLine(x, y, length, image)
	for i = 1, length do
		local r, g, b, a = imageData:getPixel( x + i, y )
		if not a == 0 then
			return true
		end
	end
	return false
end
and here is the code for falling:

Code: Select all

if maskedHLine(player.x -16, player.y + 16, 32, "img/map.png") == false then
		player.ySpeed = 200
		player.onGround = false
	else
		player.ySpeed = 0
		player.onGround = true
	end
Could someone help me out?
All your code are belong to us.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: getPixel in a line

Post by Robin »

Your boolean expressions are weird. The problem's here

Code: Select all

if not a == 0 then
which means the same as:

Code: Select all

if (not a) == 0 then
which will always be false if a is a number.
Use this:

Code: Select all

if a ~= 0 then
or

Code: Select all

if a > 0 then
The other one is bad too, but it's merely confusing, not wrong: x == false is better written as not x.

EDIT: also, maskedHLine isn't a very good name, consider renaming it to something like collidesWIthLine or something. It took me a while to figure out when it was supposed to return true. Also, pixel collision is s-l-o-w, know what you're getting yourself into.
Help us help you: attach a .love.
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: getPixel in a line

Post by szmol96 »

It worked, thank you very much. :)
All your code are belong to us.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], glass2d, Google [Bot] and 73 guests