duke-compsci308-spring2013/vooga — pull request #40 by jackmatt16 (2013-04-26 22:21:59+00:00)
Title: Collision detector

Hi Everyone,

After realizing my team needed a Collision detector - we were just using the Rectangle's intersect method- I asked around and it seemed like there might have been some interest in a collision detector utility.

To address some of Duvall's concerns, I tried to get rid of all of the duplicated code between checking hits between particular sides, and I did my best to minimize the booleans used. Unfortunately, using geometry usually requires special casing, but I've done my best to cut them back. That said, there is always more refactoring to be done, as I'm sure I will learn of when I submit this request :)

Below is the message I sent out earlier, so please look it over:

So I went ahead and created a CollisionDetector class, as well as a ShapeMeasurement class, the latter of which really just has static convenience methods to return locations of specific points on a given shape.

The CollisionDetector itself is fairly rudimentary, but is well commented and is quite extensible. I chose to give it non-static methods so that you (or more likely, the developer) could choose to subclass this class and rewrite any method of their choosing (or add they're own method!).

All of the methods deal strictly with Shapes, the class from which Rectangle is extended, and Locations. Therefore if you are using Rectangles, or any other Shape, for collisions, this class can be integrated very easily into your engine. I provide some convenience methods specifically assuming the objects are Rectangular, like boolean hitTop(Shape1,Shape2), which returns a boolean based on whether Shape1's top was hit by Shape2. I also provide some "precision" methods that are a bit more robust and intricate algorithm for more complicated shapes (say if you wanted to collide two ellipses).

Anyway, please someone look this over and let me know what you think!

Best,
Jack