Hittest against polygon or transparent image?

10 replies [Last post]
tsuriyathep's picture
tsuriyathep
User offline. Last seen 7 hours 58 min ago. Offline
Joined: 21 Oct 2011

Hi Corona developers,

I have many draggable objects, they are non-rectangular. What is the best way to touch and drag (hitTestPoint?) those non-rectangular objects. This is not physics based but if that is the only way let me know. What I did in Flash is rough-trace my transparent images and use that polygon as the hitArea. How would I do that in Corona?

Thanks!

Replies

JayantV's picture
JayantV
User offline. Last seen 1 hour 39 min ago. Offline
Joined: 31 Oct 2009

You are a Flash developer, so you can use math to determine if a point is in a polygon. You can still have an overlay area that has the alpha property of 0.01 to emit touch events.

cheers,

?:)

tsuriyathep's picture
tsuriyathep
User offline. Last seen 7 hours 58 min ago. Offline
Joined: 21 Oct 2011

Hi thanks very much. What do you mean by overlay area with an alpha property of 0.01? My sprite is transparent, a triangular shape, with transparency, the touch events go to the bounding box only, so it seems alpha has no effect on the hit area. Sorry I'm a newbie :)

JayantV's picture
JayantV
User offline. Last seen 1 hour 39 min ago. Offline
Joined: 31 Oct 2009

your image is transparent, I understand, however the boundingbox for hitText is the rectangular area, not the non-transparent area.

Now if you were creating a boundingBox that was a primitive then you could be using a roundedrect, or a circle, etc which you can set the alpha to 0.01 this overlays onto your graphic and handles the touch events rather than your image.

You will understand this the more you play around with CoronaSDK.

cheers,

?:)

tsuriyathep's picture
tsuriyathep
User offline. Last seen 7 hours 58 min ago. Offline
Joined: 21 Oct 2011

Ok but rectangles and circles are not an option in my case. So it seems like I won't be able to use polygons or image transparency for hit areas. Thanks!

renvis@technowand's picture
renvis@technowand
User offline. Last seen 1 week 4 days ago. Offline
Joined: 22 Jun 2011
tsuriyathep's picture
tsuriyathep
User offline. Last seen 7 hours 58 min ago. Offline
Joined: 21 Oct 2011

I already tried that. Unless I used it wrong, it has no effect on the hit area, only effects the graphics.

renvis@technowand's picture
renvis@technowand
User offline. Last seen 1 week 4 days ago. Offline
Joined: 22 Jun 2011

it should work. I have tried it once and got it working

1
2
3
4
5
6
7
8
9
local img=display.newRect(40,40,85,85)
local mask = graphics.newMask("mask.png")
img:setMask(mask)
 
local function moveObject(event)
event.target.x = event.x
event.target.y =event.y
end
img:addEventListener("touch",moveObject)

tsuriyathep's picture
tsuriyathep
User offline. Last seen 7 hours 58 min ago. Offline
Joined: 21 Oct 2011

I still can't get it to work. I have the latest build and the simulator always crashes when I try to drag something that has a mask. Maybe this is a bug? Could you please download this and let me know what I'm doing wrong? Basically I want to drag the star only by its hitarea/mask, not its bounding box.

http://suriyathep.com/dl/hitsample.zip

The star and its mask are divisible by 4, not sure if thats needed anymore. Thanks!

thom801
User offline. Last seen 19 hours 25 min ago. Offline
Joined: 7 Nov 2011

Did you ever come up with a solution for this? I am running into a similar problem and hoping that bitmap mask will be the answer. Thanks!

thom801
User offline. Last seen 19 hours 25 min ago. Offline
Joined: 7 Nov 2011

Update: I got this working using the code that was provided a few comments earlier. Thanks guys! I am not dragging the object though. I can definitely see how buggy it is for that from my testing. All I had to do was check if the user was hitting a complex polygon hit area.

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.