Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
hitTestPoint alpha? — Gideros Forum

hitTestPoint alpha?

moopfmoopf Guru
edited February 2012 in General questions
Curious if there's any way currently to work out if the point touched on a sprite is fully transparent or not? I just want to limit touch detection to the actual sprite, not the transparent box it is contained in.

Dislikes: ExCx

+1 -1 (+0 / -1 )Share on Facebook

Comments

  • Well alpha and hidden objects still receive touch events, I think there's nothing to be done.

    What you can do, is to determine, the target of event - which object triggered it.

    For example, you have sprite called mySprite, set it's attribue to something, for example mySprite.id = "clickable"

    then in your even handler get target and check if it is clickable like this:
    function (e)
       local target = e:getTarget()
       if target.id and target.id == "clickable" then
            --do your stuff here
       end
    end
    Just something from the top of my head, didn't test it. :)
  • Sorry, I think you've missed my point. What I want to do is only register a touch on a sprite if the part of the sprite that is touched is not fully transparent - basically, masking the touch to only the visible area of a sprite. Currently Gideros uses the complete bounding box when using hitTestPoint, for example, which if you have objects in front of others, can make registering a touch on an item behind, partly hidden by a sprite bounding box in front of it even if the area touched is transparent, difficult.

    Being able to test if the hit point for a sprite is fully transparent or not would help to give more control over which object the user wants to touch.
  • MikeHartMikeHart Guru
    edited February 2012
    How about using Box2D? Make a fixture in the shape of your sprite. Then during the touch, move an invisible body (small circle) to the position of the touch. If they contact, you have a winner.
  • Whilst that's certainly an ingenious idea, I don't think that implementing physics for something like this is the way to go. I would have to deal with lots of fixtures, for lots of different sprites throughout the app - I just don't think that's efficient at all.

    I have no other need for physics in the app either.
  • atilimatilim Maintainer
    edited February 2012
  • Hi @atilim, yup that sort of solution was my fallback although, again, it's not a particularly efficient workflow method and prone to bugs e.g. updating imagery requires new polygons to be mapped out, different sprite frames (if they vary wildly) could also require different polygons etc.

    I was really just interested to see if there was a simple solution already built into Gideros to detect the alpha of the point touched in the sprite. If not, no problem. I'll work with what we've got :D
  • Of course this solution is made all the more complex by having a play area that can be pinch zoomed and panned around. Ugh.
  • atilimatilim Maintainer
    Yes, you're right. It's not a very easy solution.

    But in case of pinch and zoom, you can use globalToLocal function to transform the coordinates of the test point to the local coordinate system and then do the point-in-polygon test:
    function pointInPolygon(vertices, x, y, sprite)
    	if sprite ~= nil then
    		x, y = sprite:globalToLocal(x, y)
    	end
     
    	local intersectionCount = 0
     
    	local x0 = vertices[#vertices].x - x
    	local y0 = vertices[#vertices].y - y
     
    	for i=1,#vertices do
    		local x1 = vertices[i].x - x
    		local y1 = vertices[i].y - y
     
    		if y0 > 0 and y1 <= 0 and x1 * y0 > y1 * x0 then
    			intersectionCount = intersectionCount + 1
    		end
     
    		if y1 > 0 and y0 <= 0 and x0 * y1 > y0 * x1 then
    			intersectionCount = intersectionCount + 1
    		end
     
    		x0 = x1
    		y0 = y1	
    	end
     
    	return (intersectionCount % 2) == 1
    end

    Likes: Dale, dominikusdp

    +1 -1 (+2 / -0 )Share on Facebook
  • Bingo, that saves me looking in the manual ;) I was just sitting here wondering if there was a function to transform coordinates like that. Many thanks @atilim
  • atilimatilim Maintainer
    edited February 2012
    you're welcome. I always find globalToLocal/localToGlobal very useful :)
Sign In or Register to comment.