Search code examples
sdkcollision-detectioncoronasdktargetoverlapping

How to detect overlapping objects in Corona?


What would be the best way to detect that a crosshair is aimed at a round target? I`ve tried adding a small circle in the middle of the crosshair, and detect when that circle and the target collide. But I cant get it to work.

I`ve tried using the x and y coordinates of the target with some luck, but it would be so much better to be able to detect when the crosshair "circle" and the round target are overlapping, and when they are not.

I´ve tried this for overlapping images: http://developer.coronalabs.com/code/flashs-hittestobject-emulated-using-contentbounds but this does not recognize round shapes, only rectangles.


Solution

  • The crosshairs is on the object if the distance from the x,y of the crosshairs to the center of the object is less than or equal to the radius of the object. Pythagoras is your friend here. Check this in enterFrame event.

    You don't need to do the square root calculation for this, by the way. Just compute ((x1-x2)*(x1-x2)) + ((y1-y2)*(y1-y2)) each time through enterFrame, and compare it to the object's radius squared, which will be constant and can therefore be calculated just once.