Best Way to Hit Test Against Irregular Object

Hi there,

I’ve read a lot about the physics engine prior to starting my next project but I’m curious to know if there is a better (and simpler) way of achieving what I need.

This is for a simple top down car racing style game and I’d like to be able to hit test between the car and the track.

In Flash, I would have just created a vector graphic of my track and used Flash’s built in hit testing to check if the car was touching any part of that vector shape, slowing it down if it was off the track.

Am I better pushing down the physics path with Corona to achieve this by creating a bunch of irregular polygons and checking if car is still touching them? Or is this overkill and there is in fact some much simpler method that I’m missing…? Perhaps to do with alpha values of pixels within a “track” PNG graphic or something?

I want to be able to create fairly irregular shaped tracks, with islands etc. in the middle of roads. I’ll be using tiles to achieve the size of track I want.

Thanks in advance for any help.

Ian

You can use the 3rd party tool Physics Editor to create the track shapes needed to define the track’s boundaries.

Rob

Thanks Rob - so what you’re saying is that physics is the best way to approach this… i.e. there is no “simpler” way of hit testing against an irregular shape like hitTestPoint in Flash for example?

From what I’ve read, creating a physics shape for the track and then setting it as a sensor would be the best approach? Then it can detect collisions without actually interacting in any other way.

It just feels like overkill to use a physics engine for something that isn’t going actually require any physics. But if that’s the only way…

Thanks for getting back to me.

Ian

We published a tutorial a while back on doing collision detection without using physics and provided a bounding box test (overlapping rectangles) and an overlapping circle method.  The math is rather easy on these two and cover many of the circumstances needed for answering “did I just drag my card over the discard pile” type tests.  I used both in my space shooter to determine if blasters were hitting my target or not or if things ran into each other.

But you’re wanting to do irregular shapes which ramps up the complexity of the math quite a bit.  There is a detection method known as the “Separating Axis Theorem” method that can take any two convex polygons and see if they are overlapping.  I tried to make a go of converting some C code to lua for this once, but my math fu wasn’t good, so I gave up on it.  There are many tutorials out there on using this method and it could be used to check a bunch of polygons to see if they are overlapping.

I’m pretty sure Box2D physics uses this under the hood for its collision detection though I’m not 100% sure of it but their “must be convex shapes” and points defined in a clockwise method rules makes me think so. 

If you want to research SAT and see if that will work for you then you can avoid physics.  But at the end of the day you will have to create all the polygons and get them positioned correctly which is where Physics Editor does all that dirty work for you and then you can use the built in collision detection.

I would love for someone to get SAT working in Lua. It would be a great addition to the community code.

Rob

@Rob, it looks like someone has ported SAT to Lua!

https://bitbucket.org/RudenkoPaint/sat-lua

That looks like it.  The one gotcha I noticed is that code wants it’s polygons counter-clockwise.  I know Box2D (and ergo Corona physics bodies that are “shape”'s are clockwise.  I looked at display.newPolygon but it didn’t say what order they had to be in. 

Rob

Interesting. Thanks for all the replies. I did read some stuff on SAT the other day. Whether going down that route is worth it when I could just use physics with the irregular shape I’m testing against set as a sensor is debatable. But it does look interesting and I’d like to understand it better.

If I go down that route and get it working I’ll post back.

Thanks again.

You can use the 3rd party tool Physics Editor to create the track shapes needed to define the track’s boundaries.

Rob

Thanks Rob - so what you’re saying is that physics is the best way to approach this… i.e. there is no “simpler” way of hit testing against an irregular shape like hitTestPoint in Flash for example?

From what I’ve read, creating a physics shape for the track and then setting it as a sensor would be the best approach? Then it can detect collisions without actually interacting in any other way.

It just feels like overkill to use a physics engine for something that isn’t going actually require any physics. But if that’s the only way…

Thanks for getting back to me.

Ian

We published a tutorial a while back on doing collision detection without using physics and provided a bounding box test (overlapping rectangles) and an overlapping circle method.  The math is rather easy on these two and cover many of the circumstances needed for answering “did I just drag my card over the discard pile” type tests.  I used both in my space shooter to determine if blasters were hitting my target or not or if things ran into each other.

But you’re wanting to do irregular shapes which ramps up the complexity of the math quite a bit.  There is a detection method known as the “Separating Axis Theorem” method that can take any two convex polygons and see if they are overlapping.  I tried to make a go of converting some C code to lua for this once, but my math fu wasn’t good, so I gave up on it.  There are many tutorials out there on using this method and it could be used to check a bunch of polygons to see if they are overlapping.

I’m pretty sure Box2D physics uses this under the hood for its collision detection though I’m not 100% sure of it but their “must be convex shapes” and points defined in a clockwise method rules makes me think so. 

If you want to research SAT and see if that will work for you then you can avoid physics.  But at the end of the day you will have to create all the polygons and get them positioned correctly which is where Physics Editor does all that dirty work for you and then you can use the built in collision detection.

I would love for someone to get SAT working in Lua. It would be a great addition to the community code.

Rob

@Rob, it looks like someone has ported SAT to Lua!

https://bitbucket.org/RudenkoPaint/sat-lua

That looks like it.  The one gotcha I noticed is that code wants it’s polygons counter-clockwise.  I know Box2D (and ergo Corona physics bodies that are “shape”'s are clockwise.  I looked at display.newPolygon but it didn’t say what order they had to be in. 

Rob

Interesting. Thanks for all the replies. I did read some stuff on SAT the other day. Whether going down that route is worth it when I could just use physics with the irregular shape I’m testing against set as a sensor is debatable. But it does look interesting and I’d like to understand it better.

If I go down that route and get it working I’ll post back.

Thanks again.

Hey did you figure out how to use SAT and corona together … 
I have a situation where i am trying to detect intersection between two images while they are rotating at random . so can you help me on this?

I found this with a quick Google search:

https://bitbucket.org/RudenkoPaint/sat-lua

thanks it worked

Hey did you figure out how to use SAT and corona together … 
I have a situation where i am trying to detect intersection between two images while they are rotating at random . so can you help me on this?

I found this with a quick Google search:

https://bitbucket.org/RudenkoPaint/sat-lua

thanks it worked