Need a hittest function

Just want to know if a point is in the content of an object.
Is there a hittest function I can use?
Thanks!

My wish is:

obj:hitTest(x,y)

it returns true, if x,y is in the obj’s content bounds [import]uid: 35642 topic_id: 14836 reply_id: 314836[/import]

you could try making a function like:
if x > obj.x - obj.width/2 and x< obj.x + obj.width/2 and y > obj.y - obj.height/2 and y < obj.y + obj.height/2 then return true else return false [import]uid: 57170 topic_id: 14836 reply_id: 54826[/import]

take a look at this thread:

http://developer.anscamobile.com/forum/2011/09/05/make-hitbox-inside-newimage

:wink:

-finefin [import]uid: 70635 topic_id: 14836 reply_id: 54828[/import]

@lorddstructive

Yes, I am using such kind of code now. But it is not perfect.

  1. Only objects which is center referenced can be applied
  2. It is not very accurate when rect object is rotated [import]uid: 35642 topic_id: 14836 reply_id: 55014[/import]

@canupa.com

Thank you. It is a way to calculate hittest by ourselves.

But I think such kind of function must exist in Corona source code, it is convenient for ansca to open this interface function for us. [import]uid: 35642 topic_id: 14836 reply_id: 55015[/import]

i did not say this was perfect…
you can make a much more complex function:
you need the angle of the rotation of the object

local dx = x - obj.x
local dy = y - obj.y
local distance = sqrt(dx*dx + dy*dy)

//distance from center of object to the point

local ang1=atan2(dx,dy)

//angle of the point relational to the center of the object

local dist=cos(ang1-angobj)*obj.width/2

//this calculates distance from center of object to the margin on the line connecting the center to the point, angobj is the angle of rotation of the object

if distance-dist>0 return false else return true end

// if the distance from center of the object to the point is greater then the distance from center to the margin the point is outside of the object
be aware that this might not be complete…this function work for only for points situated on the right side of the center of object (i don’t have time right now to make this complete…i will try today o tomorrow to make the actual function that works on all situations) [import]uid: 57170 topic_id: 14836 reply_id: 55032[/import]

The internet is full of such functions. But I can see the value for such a build in function. It is speed! Let’s say you have to calculate a decent collision detection. Something like axxis aligned rectangles or even better polygons. Do that with more than 2-4 objects and your lua code will make your device crawl. [import]uid: 5712 topic_id: 14836 reply_id: 55048[/import]

Sometimes it helps to make-up a larger object of several smaller rectangles. More to rotate and move, but you can get more accurate hit-test results. [import]uid: 85633 topic_id: 14836 reply_id: 55104[/import]