smirea / Multi-Pong

A dynamic multiplayer pong with (potential) unlimited number of players
GNU General Public License v2.0
0 stars 0 forks source link

Implement .intersect() to all base classes #3

Open smirea opened 11 years ago

smirea commented 11 years ago

So, in Ray.draw() there is a lot of intersection logic. By default a Ray know how to intersect Rectangles. The way intersection should be done is by every base class (Rectangle, Circle, Polygon) to implement .intersect() methods where they define each intersection they can do with what kind of other classes (preferably all combinations - although this does scale exponentially which is not nice). If an instance has an intersect() method it overrides the default line intersection

Polygon already implements this so that should be a good start. For performance considerations, Polygon.intersectRay() does an estimation to see if the ray intersects with the outer and inner circle and if not return no intersection. If the initial check happens, then the standard go through all Rectangle components and compile all the intersections and return them at the end.

Wee need similar features for Rectangle, Circle. Check the included intersection Math library: http://www.kevlindev.com/gui/math/intersection/index.htm

dmitrc commented 11 years ago

Wait a second: why implementing all that, if the included library has almost all of it? For ex.: Ray-Circle, Circle-Polygon, etc, are perfectly included there.

smirea commented 11 years ago

Generally you are going to need to do a lot of intersection logic to see how stuff bounces off where You have different classes each which intersect differently with others. Some are straightforward like Circle-Rectangle etc and are covered in the library and other (like Polygon-anything) is not

When you are going the animate an object the typical workflow is:

If you have all the intersection logic for all shapes in a function then it is on one side not efficient because it will be quite a big function, not easily extensible and harder to understand. Plus if you want to animate a different kind of object later on (say you want to rotate a square or whatever) then you would have to duplicate a lot of code. We can skip all that by having intersection methods per object and then just calling Object1.intersect(Object2) and have them to the inner logic themselves - by providing more specialized methods like .intersectRay .intersectCircle, etc. If it happens that there is no intersection method defined for the 2 objects (say o1 is Polygon and o2 is Rectangle and Polygon does not implement .intersectRectangle) then intersect should print a warning and return no intersections which would scale a lot better later on

Granted, most .intersect methods will simply be wrappers for the math functions but others wont and will require more work