geometry.h
author insilmaril
Fri, 11 Sep 2009 12:56:15 +0000
changeset 794 d922fb6ea482
parent 792 7d67be709091
child 798 d251c7b2de54
permissions -rw-r--r--
more fixes for collisions
     1 #ifndef GEOMETRY
     2 #define GEOMETRY
     3 
     4 #include <QPolygonF>
     5 
     6 QRectF addBBox(QRectF r1, QRectF r2);
     7 bool isInBox(const QPointF &p, const QRectF &box);
     8 
     9 
    10 class Vector:public QPointF
    11 {
    12 public:
    13 	Vector ();
    14 	Vector (const QPointF &p);
    15 	Vector (qreal x, qreal y);
    16 
    17 	friend inline bool operator==(const Vector &v1, const Vector &v2 )
    18 	{ return v1.x()==v2.x() && v1.y()==v2.y(); }
    19 
    20 	virtual void normalize ();
    21 	virtual qreal dotProduct (const QPointF &b);
    22 	virtual void scale  (const qreal &f);
    23 	virtual void invert ();
    24 };
    25 
    26 class ConvexPolygon:public QPolygonF
    27 {
    28 public:
    29 	ConvexPolygon ();
    30 	ConvexPolygon (QPolygonF p);
    31 	void calcCentroid() ;
    32 	QPointF centroid() const;
    33 	qreal weight() const;
    34 	std::string toStdString ();
    35 	Vector at (const int &i) const ; 
    36 	virtual void translate ( const Vector &offset );
    37 	virtual void translate ( qreal dx, qreal dy );
    38 private:
    39 	Vector _centroid;
    40 	qreal _area;
    41 };
    42 
    43 class PolygonCollisionResult {
    44 public:
    45     // Are the polygons going to intersect forward in time?
    46     bool willIntersect;
    47 
    48     // Are the polygons currently intersecting?
    49     bool intersect;
    50 
    51     // The translation to apply to the first polygon to push the polygons apart.
    52     QPointF minTranslation;
    53 };
    54 
    55 
    56 void projectPolygon(Vector axis, ConvexPolygon polygon, qreal &min, qreal &max) ;
    57 
    58 qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB);
    59 PolygonCollisionResult polygonCollision(ConvexPolygon polygonA, 
    60                               ConvexPolygon polygonB, Vector velocity);
    61 
    62 #endif