qafCollisionStruct.h

00001 /* 
00002 ** Qaf Framework 1.2
00003 ** June 2006
00004 ** 
00005 ** Pedro Luchini de Moraes, Public Domain - Free Lunch Code
00006 */
00007 
00008 #ifndef QAF_COLLISIONSTRUCT_H
00009 #define QAF_COLLISIONSTRUCT_H   
00010 
00011 #include <hgesprite.h>
00012 #include "qafutil/qafContainer.h"
00013 #include "qafutil/qafVector2D.h"
00014 
00015 
00016 namespace qaf {
00017     
00034     class CollisionStruct {
00035         public:
00036             
00041             virtual bool collidesWith ( CollisionStruct * otherStruct ) = 0;
00042             
00047             virtual bool hasPoint ( float x, float y ) = 0;
00048             
00061             virtual bool pointIntersection ( float x1, float y1,
00062                                              float x2, float y2,
00063                                              Vector2D * pContact,
00064                                              Vector2D * pNormal ) = 0;
00065             
00069             virtual void move ( float dx, float dy ) = 0;
00070             
00075             virtual void render ( float scrollX, float scrollY, unsigned long color ) = 0;
00076             
00077             virtual ~CollisionStruct ();
00078             
00079             class Box;
00080             class Circle;
00081             class Composite;
00082             class Polygon;
00083         
00084         private:
00085             static bool collide_Box_Circle     ( Box    *pBox,  Circle  *pCirc );
00086             static bool collide_Box_Polygon    ( Box    *pBox,  Polygon *pPoly );
00087             static bool collide_Circle_Polygon ( Circle *pCirc, Polygon *pPoly );
00088             
00089             static bool collide_PolyPoints ( const Vector2D * points1, int nPoints1, const Vector2D * points2, int nPoints2 );
00090     };
00091     
00092     
00093     
00094     
00098     class CollisionStruct::Box : public CollisionStruct {
00099         public:
00103             Box ( float x, float y, float w, float h );
00104             
00108             Box ();
00109             
00110             
00111             inline void setX ( float _x ) {
00112                 x = _x;
00113             }
00114             inline void setY ( float _y ) {
00115                 y = _y;
00116             }
00117             inline void setWidth ( float _w ) {
00118                 w = _w;
00119             }
00120             inline void setHeight ( float _h ) {
00121                 h = _h;
00122             }
00123             
00124             inline float getX () {
00125                 return x;
00126             }
00127             inline float getY () {
00128                 return y;
00129             }
00130             inline float getWidth () {
00131                 return w;
00132             }
00133             inline float getHeight () {
00134                 return h;
00135             }
00136             
00137             void render ( float scrollX, float scrollY, unsigned long color );
00138             bool collidesWith ( CollisionStruct * otherStruct );
00139             bool hasPoint ( float x, float y );
00140             bool pointIntersection ( float x1, float y1,
00141                                      float x2, float y2,
00142                                      Vector2D * pContact,
00143                                      Vector2D * pNormal );
00144             void move ( float dx, float dy );
00145             
00146         private:
00147             float x, y, w, h;
00148             
00149             friend CollisionStruct;
00150             
00151     };
00152     
00153     
00154     
00155     
00159     class CollisionStruct::Circle : public CollisionStruct {
00160         public:
00164             Circle ( float xCenter, float yCenter, float radius );
00165             
00169             Circle ();
00170             
00171             inline void setXCenter ( float x ) {
00172                 xCenter = x;
00173             }
00174             inline void setYCenter ( float y ) {
00175                 yCenter = y;
00176             }
00177             inline void setRadius ( float r ) {
00178                 radius = r;
00179             }
00180             
00181             inline float getXCenter () {
00182                 return xCenter;
00183             }
00184             inline float getYCenter () {
00185                 return yCenter;
00186             }
00187             inline float getRadius () {
00188                 return radius;
00189             }
00190             
00191             void render ( float scrollX, float scrollY, unsigned long color );
00192             bool collidesWith ( CollisionStruct * otherStruct );
00193             bool hasPoint ( float x, float y );
00194             bool pointIntersection ( float x1, float y1,
00195                                      float x2, float y2,
00196                                      Vector2D * pContact,
00197                                      Vector2D * pNormal );
00198             void move ( float dx, float dy );
00199             
00200         private:
00201             float xCenter, yCenter, radius;
00202             
00203             friend CollisionStruct;
00204     };
00205     
00206     
00207     
00208     
00223     class CollisionStruct::Composite : public CollisionStruct, public Container<CollisionStruct*> {
00224         public:
00228             Composite ( CollisionStruct * const elems[], int nCount );
00229             
00233             Composite ();
00234             
00235             void render ( float scrollX, float scrollY, unsigned long color );
00236             bool collidesWith ( CollisionStruct * otherStruct );
00237             bool hasPoint ( float x, float y );
00238             bool pointIntersection ( float x1, float y1,
00239                                      float x2, float y2,
00240                                      Vector2D * pContact,
00241                                      Vector2D * pNormal );
00242             void move ( float dx, float dy );
00243             
00244         private:
00245             friend CollisionStruct;
00246     };
00247     
00248     
00249     
00250     
00266     class CollisionStruct::Polygon : public CollisionStruct {
00267         public:
00269 
00274             Polygon ( const Vector2D * points, int nPoints );
00275             Polygon ( const Container<Vector2D> & points );
00277             
00283             Polygon ( const hgeSprite * pSprite );
00284             
00288             Polygon ();
00289             
00290             
00292 
00295             void setPoints( const Vector2D * points, int nPoints );
00296             
00297             inline void setPoints( const Container<Vector2D> & points ) {
00298                 setPoints( points.getData(), points.getCount() );
00299             }
00301             
00306             inline void getTransformedPoints ( Container<Vector2D> * points ) {
00307                 *points = realPoints;
00308             }
00309             
00313             void setPosition ( float x, float y );
00314             
00318             void getPosition ( float * x, float * py );
00319             
00323             void setRotation ( float radians );
00324             
00328             float getRotation ();
00329             
00333             void setScale ( float s );
00334             
00338             void getScale ();
00339              
00340             
00341             
00342             void render ( float scrollX, float scrollY, unsigned long color );
00343             bool collidesWith ( CollisionStruct * otherStruct );
00344             bool hasPoint ( float x, float y );
00345             bool pointIntersection ( float x1, float y1,
00346                                      float x2, float y2,
00347                                      Vector2D * pContact,
00348                                      Vector2D * pNormal );
00349             void move ( float dx, float dy );
00350             
00351         private:
00352             Container<Vector2D> userPoints, realPoints;
00353             Vector2D pos;
00354             float rotation, scale;
00355             
00356             void updateRealPoints ();
00357             
00358             friend CollisionStruct;
00359     };
00360     
00361 }
00362 
00363 
00364 #endif

Generated on Sun Mar 25 12:32:12 2007 for Qaf Framework by  doxygen 1.5.1-p1