geometry.cpp
changeset 656 53ef954e90b6
parent 650 65c5a0c28d20
child 662 9abdd5e6c3dc
     1.1 --- a/geometry.cpp	Wed Jan 16 15:45:19 2008 +0000
     1.2 +++ b/geometry.cpp	Wed Jan 16 15:45:19 2008 +0000
     1.3 @@ -1,5 +1,7 @@
     1.4  #include "geometry.h"
     1.5  
     1.6 +#include <math.h>
     1.7 +
     1.8  
     1.9  QRectF addBBox(QRectF r1, QRectF r2)
    1.10  {	
    1.11 @@ -39,3 +41,180 @@
    1.12  		return true;
    1.13      return false;	
    1.14  }
    1.15 +
    1.16 +QPointF normalize (const QPointF &p)
    1.17 +{
    1.18 +	qreal n=sqrt ( p.x()*p.x() + p.y()*p.y() );
    1.19 +	return QPointF (p.x()/n,p.y()/n);
    1.20 +}
    1.21 +
    1.22 +// Dot product of two vectors
    1.23 +qreal dotProduct (const QPointF &a, const QPointF &b)
    1.24 +{
    1.25 +	return a.x()*b.x() + a.y()*b.y();
    1.26 +}
    1.27 +
    1.28 +// Structure that stores the results of the PolygonCollision function
    1.29 +class PolygonCollisionResult {
    1.30 +public:
    1.31 +    // Are the polygons going to intersect forward in time?
    1.32 +    bool WillIntersect;
    1.33 +
    1.34 +    // Are the polygons currently intersecting?
    1.35 +    bool Intersect;
    1.36 +
    1.37 +    // The translation to apply to the first polygon to push the polygons apart.
    1.38 +    QPointF MinimumTranslationVector;
    1.39 +};
    1.40 +
    1.41 +
    1.42 +/* Calculate the projection of a polygon on an axis
    1.43 +   and returns it as a [min, max] interval
    1.44 +*/
    1.45 +void ProjectPolygon(QPointF axis, QPolygonF polygon, qreal &min, qreal &max) 
    1.46 +{
    1.47 +    // To project a point on an axis use the dot product
    1.48 +
    1.49 +    qreal d = dotProduct(axis,polygon.at(0));
    1.50 +    min = d;
    1.51 +    max = d;
    1.52 +    for (int i = 0; i < polygon.size(); i++) {
    1.53 +        d= dotProduct (polygon.at(i),axis);
    1.54 +        if (d < min) 
    1.55 +            min = d;
    1.56 +        else 
    1.57 +		{
    1.58 +            if (d> max) max = d;
    1.59 +        }
    1.60 +    }
    1.61 +}
    1.62 +
    1.63 +/* Calculate the signed distance between [minA, maxA] and [minB, maxB]
    1.64 +   The distance will be negative if the intervals overlap
    1.65 +*/
    1.66 +
    1.67 +
    1.68 +qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB) {
    1.69 +    if (minA < minB) {
    1.70 +        return minB - maxA;
    1.71 +    } else {
    1.72 +        return minA - maxB;
    1.73 +    }
    1.74 +}
    1.75 +/*
    1.76 + Check if polygon A is going to collide with polygon B.
    1.77 + The last parameter is the *relative* velocity 
    1.78 + of the polygons (i.e. velocityA - velocityB)
    1.79 +
    1.80 +*/
    1.81 +PolygonCollisionResult PolygonCollision(QPolygonF polygonA, 
    1.82 +                              QPolygonF polygonB, QPointF velocity) {
    1.83 +    PolygonCollisionResult result;
    1.84 +    result.Intersect = true;
    1.85 +    result.WillIntersect = true;
    1.86 +
    1.87 +    int edgeCountA = polygonA.size();
    1.88 +    int edgeCountB = polygonB.size();
    1.89 +    qreal minIntervalDistance = 1000000000;
    1.90 +    QPointF translationAxis;
    1.91 +    QPointF edge;
    1.92 +
    1.93 +    // Loop through all the edges of both polygons
    1.94 +
    1.95 +    for (int i=0; i < edgeCountA + edgeCountB; i++) 
    1.96 +	{
    1.97 +        if (i< edgeCountA) 
    1.98 +            edge = polygonA.at(i);
    1.99 +        else 
   1.100 +            edge = polygonB.at(i - edgeCountA);
   1.101 +
   1.102 +        // ===== 1. Find if the polygons are currently intersecting =====
   1.103 +
   1.104 +
   1.105 +        // Find the axis perpendicular to the current edge
   1.106 +
   1.107 +        QPointF axis (-edge.y(), edge.x());
   1.108 +        normalize(axis);
   1.109 +
   1.110 +        // Find the projection of the polygon on the current axis
   1.111 +
   1.112 +        qreal minA = 0; qreal minB = 0; qreal maxA = 0; qreal maxB = 0;
   1.113 +        ProjectPolygon(axis, polygonA, minA, maxA);
   1.114 +        ProjectPolygon(axis, polygonB, minB, maxB);
   1.115 +
   1.116 +        // Check if the polygon projections are currentlty intersecting
   1.117 +
   1.118 +        if (intervalDistance(minA, maxA, minB, maxB) > 0)\
   1.119 +            result.Intersect = false;
   1.120 +
   1.121 +        // ===== 2. Now find if the polygons *will* intersect =====
   1.122 +
   1.123 +
   1.124 +        // Project the velocity on the current axis
   1.125 +
   1.126 +        qreal velocityProjection = dotProduct(axis,velocity);
   1.127 +
   1.128 +        // Get the projection of polygon A during the movement
   1.129 +
   1.130 +        if (velocityProjection < 0) {
   1.131 +            minA += velocityProjection;
   1.132 +        } else {
   1.133 +            maxA += velocityProjection;
   1.134 +        }
   1.135 +
   1.136 +        // Do the same test as above for the new projection
   1.137 +
   1.138 +        qreal d = intervalDistance(minA, maxA, minB, maxB);
   1.139 +        if (d > 0) result.WillIntersect = false;
   1.140 +
   1.141 +        // If the polygons are not intersecting and won't intersect, exit the loop
   1.142 +
   1.143 +        if (!result.Intersect && !result.WillIntersect) break;
   1.144 +
   1.145 +        // Check if the current interval distance is the minimum one. If so store
   1.146 +        // the interval distance and the current distance.
   1.147 +        // This will be used to calculate the minimum translation vector
   1.148 +
   1.149 +        if (d<0) d=-d;
   1.150 +        if (d < minIntervalDistance) {
   1.151 +            minIntervalDistance = d;
   1.152 +            translationAxis = axis;
   1.153 +
   1.154 +            //QPointF t = polygonA.Center - polygonB.Center;
   1.155 +            QPointF t = polygonA.at(0) - polygonB.at(0);
   1.156 +            if (dotProduct(t,translationAxis) < 0)
   1.157 +                translationAxis = -translationAxis;
   1.158 +        }
   1.159 +    }
   1.160 +
   1.161 +    // The minimum translation vector
   1.162 +    // can be used to push the polygons appart.
   1.163 +
   1.164 +    if (result.WillIntersect)
   1.165 +        result.MinimumTranslationVector = 
   1.166 +               translationAxis * minIntervalDistance;
   1.167 +    
   1.168 +    return result;
   1.169 +}
   1.170 +
   1.171 +/* The function can be used this way: 
   1.172 +   QPointF polygonATranslation = new QPointF();
   1.173 +*/   
   1.174 +
   1.175 +
   1.176 +/*
   1.177 +PolygonCollisionResult r = PolygonCollision(polygonA, polygonB, velocity);
   1.178 +
   1.179 +if (r.WillIntersect) 
   1.180 +  // Move the polygon by its velocity, then move
   1.181 +  // the polygons appart using the Minimum Translation Vector
   1.182 +  polygonATranslation = velocity + r.MinimumTranslationVector;
   1.183 +else 
   1.184 +  // Just move the polygon by its velocity
   1.185 +  polygonATranslation = velocity;
   1.186 +
   1.187 +polygonA.Offset(polygonATranslation);
   1.188 +
   1.189 +*/
   1.190 +
   1.191 +