Monday, December 14, 2009

Precision Collision Detection

I was playing around with Corey O'Neil's collision detection engine and while it is very good for working with most simple shapes; I was missing a crucial piece of information when it came to working with more irregular shapes.

What I'm referring to here is the 'normal of collision'.
With Corey's kit, we can acquire an 'angle of collision'; this angle is useful if working with circles because the angle of collision in this case is equivalent to the angle made by the collision normal.
In some more complex cases, however, the angle of collision doesn't give us the necessary information to determine the 'bounce angle'.

It's a bit difficult to explain, so I'll just show you.
Here is corey's kit (the red line shows the angle of collision):

^ The number of milliseconds it takes to calculate the collision is shown in the white textbox.

Basically, with Corey's kit, we can only get a precise angle of collision between two shapes if one of those shapes is a circle.

Here is my version:

Notice that now we can get an accurate bounce angle from the normal of collision.
Also, check out the improvement in speed from Corey's kit!
I'm considering releasing my code publicly, but I'll have to do a bit of code commenting first ;p

I followed Skinner's concept of using BitmapData to perform the collision testing and used a great deal of bitmap processing and vector math to evaluate the normal of collision. I still have to do a bit of tweaking to optimize performance. But so far it gets the job done.

No comments:

Post a Comment