Collision with transparent images
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Collision with transparent images
Is there any way to detect collision on transparent images with any kind of shape?
- BrotSagtMist
- Party member
- Posts: 657
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Collision with transparent images
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.
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
Re: Collision with transparent images
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:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
Re: Collision with transparent images
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
https://love2d.org/wiki/ImageData
Re: Collision with transparent images
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."
"If each mistake being made is a new one, then progress is being made."
Re: Collision with transparent images
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
Quads, use them. https://love2d.org/wiki/Quad
Re: Collision with transparent images
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.
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.
- BrotSagtMist
- Party member
- Posts: 657
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Collision with transparent images
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.
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
Who is online
Users browsing this forum: No registered users and 3 guests