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.
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.
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.
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
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 ~=nilthen
x, y = sprite:globalToLocal(x, y)endlocal intersectionCount =0local x0 = vertices[#vertices].x - x
local y0 = vertices[#vertices].y - y
for i=1,#vertices dolocal x1 = vertices[i].x - x
local y1 = vertices[i].y - y
if y0 >0and y1 <=0and x1 * y0 > y1 * x0 then
intersectionCount = intersectionCount + 1endif y1 >0and y0 <=0and x0 * y1 > y0 * x1 then
intersectionCount = intersectionCount + 1end
x0 = x1
y0 = y1
endreturn(intersectionCount %2)==1end
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
Comments
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:
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.
I have no other need for physics in the app either.
point in polygon test can be good a solution: http://www.giderosmobile.com/forum/discussion/218/how-to-get-spritehittestpoint-to-ignore-transparent-areas-of-image#Item_1 And PhysicsEditor http://www.physicseditor.de/ is great about creating such polygons.
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
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:
Likes: Dale, dominikusdp