Page 1 of 2
Pixel precise object picking
Posted: Fri Oct 05, 2012 4:33 pm
by GarbagePillow
Hey guys.
Sometimes I want to be able to select an object using the mouse. Maybe there is some trick to doing this that is not obvious to me, but I have come up with a solution in this demo that seems to work. To do this I have a canvas that contains ID information, and a 1 pixel canvas for retrieving an ID.
Let me know what you think.
- EJBve.png (147.69 KiB) Viewed 579 times
edit* ack, I meant to post this in the Projects and Demos forum, maybe its not appropriate here
Re: Pixel precise object picking
Posted: Fri Oct 05, 2012 6:21 pm
by Xgoff
i would think you could simply just do a getPixel() on an imagedata and check if a fully transparent pixel is under the mouse. you wouldn't need shaders or canvases for it either
Re: Pixel precise object picking
Posted: Fri Oct 05, 2012 6:33 pm
by GarbagePillow
Xgoff wrote:i would think you could simply just do a getPixel() on an imagedata and check if a fully transparent pixel is under the mouse. you wouldn't need shaders or canvases for it either
Hmm... where does the imagedata come from?
Re: Pixel precise object picking
Posted: Fri Oct 05, 2012 6:38 pm
by Xgoff
GarbagePillow wrote:Xgoff wrote:i would think you could simply just do a getPixel() on an imagedata and check if a fully transparent pixel is under the mouse. you wouldn't need shaders or canvases for it either
Hmm... where does the imagedata come from?
love.image.newImageData
Re: Pixel precise object picking
Posted: Fri Oct 05, 2012 6:48 pm
by Nixola
You'd have to take a screenshot (an imagedata) to be sure the mouse is overlapping the object
Re: Pixel precise object picking
Posted: Fri Oct 05, 2012 6:52 pm
by Xgoff
Nixola wrote:You'd have to take a screenshot (an imagedata) to be sure the mouse is overlapping the object
not really, as long as the mouse pointer is within the bounding box of the image you can check it
Re: Pixel precise object picking
Posted: Fri Oct 05, 2012 6:56 pm
by Nixola
I was going to say that there's an easier/faster way... Then I tried to think and I stopped.
That means you're right; anyway, are you able to read this without quoting?
Re: Pixel precise object picking
Posted: Fri Oct 05, 2012 6:59 pm
by GarbagePillow
Xgoff wrote:Nixola wrote:You'd have to take a screenshot (an imagedata) to be sure the mouse is overlapping the object
not really, as long as the mouse pointer is within the bounding box of the image you can check it
Ahh. I see what you are saying. Two points though: You would have to traverse through all the images overlapping the point you are checking until you hit one that did not have a transparent pixel at the location. Also, you would need to have imagedata for each image you loaded. This could easily take more space than maintaining an extra canvas.
Re: Pixel precise object picking
Posted: Fri Oct 05, 2012 7:02 pm
by Xgoff
GarbagePillow wrote:Xgoff wrote:Nixola wrote:You'd have to take a screenshot (an imagedata) to be sure the mouse is overlapping the object
not really, as long as the mouse pointer is within the bounding box of the image you can check it
Ahh. I see what you are saying. Two points though: You would have to traverse through all the images overlapping the point you are checking until you hit one that did not have a transparent pixel at the location. Also, you would need to have imagedata for each image you loaded. This could easily take more space than maintaining an extra canvas.
yes, that's a drawback. however, you'd probably need to implement it anyway as a fallback since not everybody's hardware can use canvases, and apparently some can't use shaders either
Re: Pixel precise object picking
Posted: Fri Oct 05, 2012 7:02 pm
by Ref
Hmm... where does the imagedata come from?
image_data = love.image.newImageData( "images/"..file )
img = G.newImage( image_data )