Determine if a point is inside a DisplayObject

Does anyone know if it is possible to determine if a point is inside a DisplayObject? I know I can get the contentBounds property to determine if a point is inside the bounding rectangle, but if I have a shape created by say, display.newPolygon() or a rectangle that has undergone quadrilateral distortion, checking the contentBounds is not really suitable.

The Tap event listener (i.e. rect:addEventListener(“tap”, function ()…)  actually seems to respect the vertices of the object when determining if there is a hit, so it seems that there is a way to do it, but I can’t find the API.

Any help is greatly appreciated.

Albert

For a polygon, you can use the Separating Axis Theorem. See: http://gamedevelopment.tutsplus.com/tutorials/collision-detection-using-the-separating-axis-theorem–gamedev-169

I’m pretty sure there is a Lua example out there somewhere.

Rob

Hi Rob,

Thanks for the answer. Unfortunately this would not easily apply with a Rectangle that has undergone quadrilateral distortion, or for a polygon in which I don’t know the vertices (since they are not retrievable on the polygon.)

I know that I could save the data separately, but if I’m going to offer this as a plugin, it’s not practical to ask consumers of the API to save the vertices to each object.

Since Corona already performs these checks within the Tap handler, is it possible to expose this on an individual DisplayObject basis? Since it’s possible to get the contentBounds, contentToLocal and localToContent, something like isPointInside() or another API certainly makes sense.

Thanks!

Albert

You should be able to make a vertices table from the x1, y1, x2, y2, etc points and make it part of the object:

object.vertices = {…}

I understand those basics, however I have a lot of objects with vertices, I’d have to go through all of my code and fix those places. And if I want to release some of this code as plugins, I’ll have to require the consumer to make the same assumptions.

I’m just asking for an API at this point, since the functionality is already in use CoronaSDK in the Tap event, otherwise I am forced to use heuristics for determining if an object is a rectangle, polygon, circle, rounded rectangle (of which there is no way to test this object easily), or text (of which there is also no way to test this since we can’t access the mask.)

I’ve created a feature request for this here: http://feedback.coronalabs.com/forums/188732-corona-sdk-feature-requests-feedback/suggestions/13866753-ability-to-determine-if-a-point-is-inside-a-displa

Thanks!

I want to add that the reason for this is because my code renders objects off to an external buffer, and because of that I lose the ability to set a tap handler. Using the bounding boxes is sufficient for plain rectangles, but does not work for more deformed rectangle or polygons.

Since Corona has demonstrated this functionality, it would be nice to have access to those functions.

So, are you interested in a custom solution, or do you want to wait for a direct Corona implementation?

Because I got the functions for most “is point in …” functions. (not for text of course)

Of course you would need to do the implementation (storing object data and checking object type) and optimization.

Hi, Thanks for responding. I’ll wait for an official implementation (if ever), as I wrote all the functionality previously.

I think Corona’s implementation would be best because a decently written C version will out perform Lua by at least 4-5x or more. I lose about 3-4 fps in my own code due to the enormous level and overhead from the game.

Thanks again,

Albert

For a polygon, you can use the Separating Axis Theorem. See: http://gamedevelopment.tutsplus.com/tutorials/collision-detection-using-the-separating-axis-theorem–gamedev-169

I’m pretty sure there is a Lua example out there somewhere.

Rob

Hi Rob,

Thanks for the answer. Unfortunately this would not easily apply with a Rectangle that has undergone quadrilateral distortion, or for a polygon in which I don’t know the vertices (since they are not retrievable on the polygon.)

I know that I could save the data separately, but if I’m going to offer this as a plugin, it’s not practical to ask consumers of the API to save the vertices to each object.

Since Corona already performs these checks within the Tap handler, is it possible to expose this on an individual DisplayObject basis? Since it’s possible to get the contentBounds, contentToLocal and localToContent, something like isPointInside() or another API certainly makes sense.

Thanks!

Albert

You should be able to make a vertices table from the x1, y1, x2, y2, etc points and make it part of the object:

object.vertices = {…}

I understand those basics, however I have a lot of objects with vertices, I’d have to go through all of my code and fix those places. And if I want to release some of this code as plugins, I’ll have to require the consumer to make the same assumptions.

I’m just asking for an API at this point, since the functionality is already in use CoronaSDK in the Tap event, otherwise I am forced to use heuristics for determining if an object is a rectangle, polygon, circle, rounded rectangle (of which there is no way to test this object easily), or text (of which there is also no way to test this since we can’t access the mask.)

I’ve created a feature request for this here: http://feedback.coronalabs.com/forums/188732-corona-sdk-feature-requests-feedback/suggestions/13866753-ability-to-determine-if-a-point-is-inside-a-displa

Thanks!

I want to add that the reason for this is because my code renders objects off to an external buffer, and because of that I lose the ability to set a tap handler. Using the bounding boxes is sufficient for plain rectangles, but does not work for more deformed rectangle or polygons.

Since Corona has demonstrated this functionality, it would be nice to have access to those functions.

So, are you interested in a custom solution, or do you want to wait for a direct Corona implementation?

Because I got the functions for most “is point in …” functions. (not for text of course)

Of course you would need to do the implementation (storing object data and checking object type) and optimization.

Hi, Thanks for responding. I’ll wait for an official implementation (if ever), as I wrote all the functionality previously.

I think Corona’s implementation would be best because a decently written C version will out perform Lua by at least 4-5x or more. I lose about 3-4 fps in my own code due to the enormous level and overhead from the game.

Thanks again,

Albert