Checking pixels in tmx files

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

Re: Checking pixels in tmx files

Post by szmol96 »

I want pixel collision so much, but I don't know what to do... :(
All your code are belong to us.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Checking pixels in tmx files

Post by HugoBDesigner »

szmol96 wrote:I want pixel collision so much, but I don't know what to do... :(
The code I gave you is not working? Have you (or me) made something wrong? Because I'm 99% sure this new code should work. Oh, and in this code we check y before x, just so you know...
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: Checking pixels in tmx files

Post by szmol96 »

I haven't tried it yet, but it looks a bit complicated to me.
All your code are belong to us.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Checking pixels in tmx files

Post by HugoBDesigner »

szmol96 wrote:I haven't tried it yet, but it looks a bit complicated to me.
I tried to make it really simple. All you need to do is replace your current code by this (make a backup, though, for safety):

(I also added comments for better understanding)
HugoBDesigner wrote:
On love.load:

Code: Select all

pixelmap = {} -- This is where the pixels will be stored
for y = 1, #map do -- Checks each column
    for x = 1, #map[y] do -- Checks each line
        local a = map[y][x] -- Get the pixel from the map you already have
        if a == 1 then --Checks if the tile is "1" and if it should collide
            pixelmap[x.."-"..y] = true --Adds the tile to the collision detector, so you don't need to check every time
        end
    end
end
On love.update:

Code: Select all

for y = math.max(1, playery-50), math.min(#map, playery+50) do --Checks each column around the player
    for x = math.max(1, playerx-50), math.min(#map[y], playerx+50) do --Checks each line around the player
        if pixelmap[x.."-"..y] then --Checks if the tile can collide
            --your collision code here
        end
    end
end

The rest you already have (I think), so, since you already have a table for the map, it becomes a lot easier to solve problems :)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: Checking pixels in tmx files

Post by szmol96 »

OK, I will see it tomorrow. Thanks for your time and effort. :)
All your code are belong to us.
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: Checking pixels in tmx files

Post by szmol96 »

I have found an alternate solution. Checking pixels in a 640x480 image doesn't lag, does it? So, I came up with the idea of dividing my map to 640x480 segments and only check the segment the player is currently in. I have made a table of the map segments.

Code: Select all

map = {
	a1 = {
		x = 0,
		y = 0,
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapa1.png")
	}
	a2 = {
		x = a1.x + a1.width,
		y = a1.y,
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapa2.png")
	}
	b1 = {
		x = a1.x,
		y = a1.y + a1.height,
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapb1.png")
	}
	b2 = {
		x = a1.x + a1.width,
		y = a1.y + a1.height,
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapb2.png")
	}
}
How can I efficiently return the current map segment? So I could use it in the maskedPixel function.
All your code are belong to us.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], KayleMaster and 58 guests