Pixel precise object picking

General discussion about LÖVE, Lua, game development, puns, and unicorns.
GarbagePillow
Prole
Posts: 41
Joined: Wed Sep 26, 2012 9:19 pm

Pixel precise object picking

Post 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
EJBve.png (147.69 KiB) Viewed 585 times
edit* ack, I meant to post this in the Projects and Demos forum, maybe its not appropriate here
Attachments
ObjectPicking.love
(195.69 KiB) Downloaded 160 times
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: Pixel precise object picking

Post 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
GarbagePillow
Prole
Posts: 41
Joined: Wed Sep 26, 2012 9:19 pm

Re: Pixel precise object picking

Post 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?
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: Pixel precise object picking

Post 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
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Pixel precise object picking

Post by Nixola »

You'd have to take a screenshot (an imagedata) to be sure the mouse is overlapping the object
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: Pixel precise object picking

Post 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
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Pixel precise object picking

Post 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?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
GarbagePillow
Prole
Posts: 41
Joined: Wed Sep 26, 2012 9:19 pm

Re: Pixel precise object picking

Post 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.
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: Pixel precise object picking

Post 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
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Pixel precise object picking

Post by Ref »

Hmm... where does the imagedata come from?
image_data = love.image.newImageData( "images/"..file )
img = G.newImage( image_data )
Post Reply

Who is online

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