12 QRectF addBBox(QRectF r1, QRectF r2)
14 // Find smallest QRectF containing given rectangles
18 if (r1.left() <= r2.left() )
19 n.setLeft(r1.left() );
21 n.setLeft(r2.left() );
24 if (r1.top() <= r2.top() )
30 if (r1.right() <= r2.right() )
31 n.setRight(r2.right() );
33 n.setRight(r1.right() );
36 if (r1.bottom() <= r2.bottom() )
37 n.setBottom(r2.bottom() );
39 n.setBottom(r1.bottom() );
43 bool isInBox(const QPointF &p, const QRectF &box)
45 if (p.x() >= box.left() && p.x() <= box.right()
46 && p.y() <= box.bottom() && p.y() >= box.top() )
51 qreal distance (const QPointF &p, const QPointF &q)
53 return sqrt (p.x()*q.x() + p.y()*q.y());
56 Vector::Vector ():QPointF ()
60 Vector::Vector (const QPointF &p):QPointF (p)
64 Vector::Vector (qreal x, qreal y):QPointF (x,y)
69 void Vector::normalize ()
71 if (x()==0 && y()==0) return;
72 qreal l=sqrt ( x()*x() + y()*y() );
77 //! Dot product of two vectors
78 qreal Vector::dotProduct (const QPointF &b)
80 return x()*b.x() + y()*b.y();
84 void Vector::scale (const qreal &f)
90 void Vector::invert ()
96 QPointF Vector::toQPointF ()
98 return QPointF (x(),y());
101 /*! Calculate the projection of a polygon on an axis
102 and returns it as a [min, max] interval */
103 ConvexPolygon::ConvexPolygon ()
107 ConvexPolygon::ConvexPolygon (QPolygonF p):QPolygonF (p)
111 void ConvexPolygon::calcCentroid()
113 // Calculate area and centroid
114 // http://en.wikipedia.org/wiki/Centroid
120 for (int i=0;i<size()-1;i++)
122 p=at(i).x() * at(i+1).y() - at(i+1).x() * at(i).y();
124 cx+=(at(i).x()+at(i+1).x()) * p;
125 cy+=(at(i).y()+at(i+1).y()) * p;
128 // area is negative if vertices ordered counterclockwise
129 // (in mirrored graphicsview!)
132 _centroid.setX (cx/p);
133 _centroid.setY (cy/p);
136 QPointF ConvexPolygon::centroid() const
141 qreal ConvexPolygon::weight() const
146 std::string ConvexPolygon::toStdString()
149 for (int i=0;i<size();++i)
151 s+=QString("(%1,%2)").arg(at(i).x()).arg(at(i).y());
152 if (i<size()-1) s+=",";
155 return s.toStdString();
158 Vector ConvexPolygon::at(const int &i) const
160 return Vector (QPolygonF::at(i).x(),QPolygonF::at(i).y());
163 void ConvexPolygon::translate ( const Vector & offset )
164 { translate (offset.x(),offset.y());}
166 void ConvexPolygon::translate ( qreal dx, qreal dy )
168 QPolygonF::translate (dx,dy);
169 _centroid=_centroid+QPointF (dx,dy);
172 void projectPolygon(Vector axis, ConvexPolygon polygon, qreal &min, qreal &max)
174 // To project a point on an axis use the dot product
176 //cout << "Projecting on "<< axis<<endl;
177 qreal d = axis.dotProduct(polygon.at(0));
180 for (int i = 0; i < polygon.size(); i++)
182 d= polygon.at(i).dotProduct (axis);
187 // cout << "p="<<polygon.at(i)<<" d="<<d<<" (min, max)=("<<min<<","<<max<<")\n";
191 // Calculate the signed distance between [minA, maxA] and [minB, maxB]
192 // The distance will be negative if the intervals overlap
194 qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB) {
203 Check if polygon A is going to collide with polygon B.
204 The last parameter is the *relative* velocity
205 of the polygons (i.e. velocityA - velocityB)
208 PolygonCollisionResult polygonCollision(ConvexPolygon polygonA,
209 ConvexPolygon polygonB, Vector velocity)
211 PolygonCollisionResult result;
212 result.intersect = true;
213 result.willIntersect = true;
215 int edgeCountA = polygonA.size();
216 int edgeCountB = polygonB.size();
217 qreal minIntervalDistance = 1000000000;
218 QPointF translationAxis;
223 for (int k=0; k<edgeCountA;k++)
224 cout <<polygonA.at(k);
226 for (int k=0; k<edgeCountB;k++)
227 cout <<polygonB.at(k);
231 // Loop through all the edges of both polygons
232 for (int i=0;i<edgeCountA + edgeCountB;i++)
236 // Loop through polygon A
239 polygonA.at(i+1).x()-polygonA.at(i).x(),
240 polygonA.at(i+1).y()-polygonA.at(i).y());
243 polygonA.at(0).x()-polygonA.at(i).x(),
244 polygonA.at(0).y()-polygonA.at(i).y());
247 // Loop through polygon B
248 if (i < edgeCountA +edgeCountB -1 )
250 polygonB.at(i-edgeCountA+1).x() - polygonB.at(i-edgeCountA).x(),
251 polygonB.at(i-edgeCountA+1).y() - polygonB.at(i-edgeCountA).y());
254 polygonB.at(0).x() - polygonB.at(i-edgeCountA).x(),
255 polygonB.at(0).y() - polygonB.at(i-edgeCountA).y());
258 // ===== 1. Find if the polygons are currently intersecting =====
260 // Find the axis perpendicular to the current edge
262 Vector axis (-edge.y(), edge.x());
265 // Find the projection of the polygon on the current axis
267 qreal minA = 0; qreal minB = 0; qreal maxA = 0; qreal maxB = 0;
268 projectPolygon(axis, polygonA, minA, maxA);
269 projectPolygon(axis, polygonB, minB, maxB);
271 // Check if the polygon projections are currentlty intersecting
273 qreal d = intervalDistance(minA, maxA, minB, maxB);
274 if (d > 0) result.intersect = false;
276 // ===== 2. Now find if the polygons *will* intersect =====
279 // Project the velocity on the current axis
281 qreal velocityProjection = axis.dotProduct(velocity);
283 // Get the projection of polygon A during the movement
285 if (velocityProjection < 0)
286 minA += velocityProjection;
288 maxA += velocityProjection;
290 // Do the same test as above for the new projection
292 // d = intervalDistance(minA, maxA, minB, maxB);
293 //if (d > 0) result.willIntersect = false;
296 cout << "edge="<<edge<<" ";
297 cout <<"axis="<<axis<<" ";
298 cout <<"dA=("<<minA<<","<<maxA<<") dB=("<<minB<<","<<maxB<<")";
299 cout <<" d="<<d<<" ";
300 //cout <<"minD="<<minIntervalDistance<<" ";
301 cout <<"int="<<result.intersect<<" ";
302 //cout <<"wint="<<result.willIntersect<<" ";
303 //cout <<"velProj="<<velocityProjection<<" ";
307 if (result.intersect )// || result.willIntersect)
309 // Check if the current interval distance is the minimum one. If so
310 // store the interval distance and the current distance. This will
311 // be used to calculate the minimum translation vector
314 if (d < minIntervalDistance) {
315 minIntervalDistance = d;
316 //translationAxis = axis;
317 //cout << "tAxix="<<translationAxis<<endl;
319 //QPointF t = polygonA.Center - polygonB.Center;
320 //QPointF t = polygonA.at(0) - polygonB.at(0);
321 //if (dotProduct(t,translationAxis) < 0)
322 // translationAxis = -translationAxis;
327 // The minimum translation vector
328 // can be used to push the polygons appart.
330 if (result.willIntersect)
331 result.minTranslation =
332 translationAxis * minIntervalDistance;
337 /* The function can be used this way:
338 QPointF polygonATranslation = new QPointF();
343 PolygonCollisionResult r = PolygonCollision(polygonA, polygonB, velocity);
346 // Move the polygon by its velocity, then move
347 // the polygons appart using the Minimum Translation Vector
348 polygonATranslation = velocity + r.minTranslation;
350 // Just move the polygon by its velocity
351 polygonATranslation = velocity;
353 polygonA.Offset(polygonATranslation);