## Collision by color

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Hitsuji
Prole
Posts: 3
Joined: Fri Aug 04, 2017 6:43 pm

### Collision by color

Hi lövilin ones! Newcomer here.
Startin this week, when wrote my first 'Hellö Löve!', did advanced little by little, until this bump which made me claim for your knowledge.

The thing is, after some efforts I could make my character (a 32x32 rectangle) to say: when he steps in white and when he steps in black on a .png picture. Now been tryin to make the white color be collidible (to only walks in black), but couldn't do it!

Just read a lot of tutorial about collisions and stuff, and a lot of tests... The majority is about tile/grid movement with I'd dislike.
Hope to get a clue here!
This my first time programming something, can't give up, yet!

Мэтю
Prole
Posts: 31
Joined: Mon Jan 06, 2014 1:24 pm
Location: Espírito Santo, Brazil
Contact:

### Re: Collision by color

I suggest you to use some libraries that are made by community. A example is bump.lua by Kikito, which can be found here: https://github.com/kikito/bump.lua

World needs love.

Hitsuji
Prole
Posts: 3
Joined: Fri Aug 04, 2017 6:43 pm
Afraidin that I was not clear.

I want help to make a collision in the colors of picture. This picture may contains polygonals forms, even formless, that's why the general collision tutorials won't help (they're mostly to non-circular shapes).

So, theres any way to declare a color a collision?

Don't send me tutorials , cause they're not specific to my issue.
Show how to compare a player position with a color in a picture, if it's possible.

This is how I did to get color by the character:

Code: Select all

  if character then
local r, g, b = imgdata:getPixel(character.x, character.y)
if r == 255 then
print 'red!'

zorg
Party member
Posts: 2732
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Collision by color

The links provided shows you how you should code collisions, since doing it per-pixel has tons of issues to go with it.
The issues are detailed in older posts i don't wish to (most probably errorenously) explain here; maybe someone else will.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Hitsuji
Prole
Posts: 3
Joined: Fri Aug 04, 2017 6:43 pm
Erroneously? Well, checked this forum by a week, I may be missin a keyword in particular.
In my search, only found stuff about collidin Fixture and Tile-based objects.

So, problably, theres no way to call a color as collision, in Lua/Löve.

zorg
Party member
Posts: 2732
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Collision by color

i meant that if i were to detail why pixel-color based logic is bad i'd probably mess up in my explanation.

If you're adamant, there are ways; like loadig an image as an imagedata, and creating a table of pixels colors however you want, and using that table for collision checking; that does assume your image will be the only "map" so to speak, and it won't be modified.

Then again, i think image objects still have a refresh function that rebuilds them from the imagedata they were created from, so in that way, i guess you could update your "map" as well, though calling it every frame may be a bit hard on the framerate.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Мэтю
Prole
Posts: 31
Joined: Mon Jan 06, 2014 1:24 pm
Location: Espírito Santo, Brazil
Contact:

### Re:

Hitsuji wrote:
Sun Aug 06, 2017 5:35 pm
...
So, problably, theres no way to call a color as collision, in Lua/Löve.
There's a way, but maybe it has not a good performance. Btw, I was toying around with some code to try to satiate your doubt.

Then I come to a code that tries to do a collision pixel-per-pixel for a certain color. I'll attach it here, then you download and analyze if that is what you looking for.

It's a .love file, just rename it to "whateveryouwant.zip" and extract

Hope it helps you
Attachments
per-pixel.love
World needs love.

Skeletonxf
Citizen
Posts: 87
Joined: Tue Dec 30, 2014 6:07 pm

### Re: Collision by color

If you want to get collisions from premade image files rather than whatever is on the screen you can make the images, then load them and then query over the entire image using https://love2d.org/wiki/ImageData:getPixel in two for loops going through the x and y to decode them into a 1 pixel sized grid of squares using a nested table data structure you then use in the conventional way. You would be able to have something resembling

Code: Select all

character[x][y]

after decoding the images.

I know you want complicated shapes, but literally anything can be reduced into squares to produce non squares.

### Who is online

Users browsing this forum: Bing [Bot] and 10 guests