Collision with transparent images

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
Pombo
Prole
Posts: 5
Joined: Sun Jul 17, 2022 1:16 am

Collision with transparent images

Post by Pombo »

Is there any way to detect collision on transparent images with any kind of shape?
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: Collision with transparent images

Post by BrotSagtMist »

Images have nothing to do with collisions at all.
A collision function works on geometric shapes, not pixel.
At least for 90% of our usecases. A collision checker that works on pixel will barely manage to run fullscreen at full speed with only one image to check.
obey
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Collision with transparent images

Post by togFox »

You would need to know the size of the image and then do 'bouding-box' checks every cycle. You can google that and see if that helps.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Collision with transparent images

Post by darkfrei »

Pombo wrote: Sat Aug 13, 2022 1:36 am Is there any way to detect collision on transparent images with any kind of shape?
1) load the Imagehttps://love2d.org/wiki/love.graphics.newImage
2) create the Image data f4om it https://love2d.org/wiki/Canvas:newImageData
3) parse the Image data to any format of table, for example

Code: Select all

map[y][x] = true -- for collision
See example here:
https://love2d.org/wiki/ImageData
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Collision with transparent images

Post by ReFreezed »

Load the image with love.image.newImageData and use ImageData:getPixel or ImageData:mapPixel to check pixel values. For larger images this is very slow, as BrotSagtMist suggests, but could work fine for lower resolutions. As for collision response, that's quite a big topic and depends completely on what exactly you want.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Collision with transparent images

Post by darkfrei »

For collision optimization use the splitted images to subimages, so first check if you probably have collision with this subimage and if yes, then check the collision to every pixel in this subimage.
Quads, use them. https://love2d.org/wiki/Quad
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Collision with transparent images

Post by pgimeno »

There's lots of bikeshedding going on, so I'll add my bit ;)

Thrust II Reloaded uses pixel-perfect collisions and there's plenty of time for everything else. https://codeberg.org/pgimeno/Thrust-II- ... r/game.lua

I make a canvas as small as possible, just big enough to fit the image of the sprite to detect collision with. Then I draw everything except the sprite to check, with a special tileset image which only has a collision mask. Then I draw the sprite to check in multiplicative mode (well, also a version that only has the collision mask).

If the resulting image is not fully black, there's a collision. I detect that by converting it to ImageData and then getting the data as a string and comparing with a string made of all zeros.
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: Collision with transparent images

Post by BrotSagtMist »

String compare? Interesting approach.
I used getPixel() before but only to probe points. Hower on a huge full sized canvas.

My main concern is that moving data from the gpu memory back to the cpu memory could trigger some freak bugs.
Games that fried players gpus exists after all.
obey
Post Reply

Who is online

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