Page 1 of 1

Collision with transparent images

Posted: Sat Aug 13, 2022 1:36 am
by Pombo
Is there any way to detect collision on transparent images with any kind of shape?

Re: Collision with transparent images

Posted: Sat Aug 13, 2022 3:27 am
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.

Re: Collision with transparent images

Posted: Sat Aug 13, 2022 4:43 am
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.

Re: Collision with transparent images

Posted: Sat Aug 13, 2022 7:02 am
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

Re: Collision with transparent images

Posted: Sat Aug 13, 2022 9:11 am
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.

Re: Collision with transparent images

Posted: Sat Aug 13, 2022 12:20 pm
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

Re: Collision with transparent images

Posted: Sat Aug 13, 2022 4:34 pm
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.

Re: Collision with transparent images

Posted: Sat Aug 13, 2022 7:24 pm
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.