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 Vector::Vector ():QPointF ()
55 Vector::Vector (const QPointF &p):QPointF (p)
59 Vector::Vector (qreal x, qreal y):QPointF (x,y)
64 void Vector::normalize ()
66 if (x()==0 && y()==0) return;
67 qreal l=sqrt ( x()*x() + y()*y() );
72 //! Dot product of two vectors
73 qreal Vector::dotProduct (const QPointF &b)
75 return x()*b.x() + y()*b.y();
79 void Vector::scale (const qreal &f)
85 void Vector::invert ()
91 QPointF Vector::toQPointF ()
93 return QPointF (x(),y());
96 /*! Calculate the projection of a polygon on an axis
97 and returns it as a [min, max] interval */
98 ConvexPolygon::ConvexPolygon ()
102 ConvexPolygon::ConvexPolygon (QPolygonF p):QPolygonF (p)
106 void ConvexPolygon::calcCentroid()
108 // Calculate area and centroid
109 // http://en.wikipedia.org/wiki/Centroid
115 for (int i=0;i<size()-1;i++)
117 p=at(i).x() * at(i+1).y() - at(i+1).x() * at(i).y();
119 cx+=(at(i).x()+at(i+1).x()) * p;
120 cy+=(at(i).y()+at(i+1).y()) * p;
123 // area is negative if vertices ordered counterclockwise
124 // (in mirrored graphicsview!)
127 _centroid.setX (cx/p);
128 _centroid.setY (cy/p);
131 QPointF ConvexPolygon::centroid() const
136 qreal ConvexPolygon::weight() const
141 std::string ConvexPolygon::toStdString()
144 for (int i=0;i<size();++i)
146 s+=QString("(%1,%2)").arg(at(i).x()).arg(at(i).y());
147 if (i<size()-1) s+=",";
150 return s.toStdString();
153 Vector ConvexPolygon::at(const int &i) const
155 return Vector (QPolygonF::at(i).x(),QPolygonF::at(i).y());
158 void ConvexPolygon::translate ( const Vector & offset )
159 { translate (offset.x(),offset.y());}
161 void ConvexPolygon::translate ( qreal dx, qreal dy )
163 QPolygonF::translate (dx,dy);
164 _centroid=_centroid+QPointF (dx,dy);
167 void projectPolygon(Vector axis, ConvexPolygon polygon, qreal &min, qreal &max)
169 // To project a point on an axis use the dot product
171 //cout << "Projecting on "<< axis<<endl;
172 qreal d = axis.dotProduct(polygon.at(0));
175 for (int i = 0; i < polygon.size(); i++)
177 d= polygon.at(i).dotProduct (axis);
182 // cout << "p="<<polygon.at(i)<<" d="<<d<<" (min, max)=("<<min<<","<<max<<")\n";
186 // Calculate the signed distance between [minA, maxA] and [minB, maxB]
187 // The distance will be negative if the intervals overlap
189 qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB) {
198 Check if polygon A is going to collide with polygon B.
199 The last parameter is the *relative* velocity
200 of the polygons (i.e. velocityA - velocityB)
203 PolygonCollisionResult polygonCollision(ConvexPolygon polygonA,
204 ConvexPolygon polygonB, Vector velocity)
206 PolygonCollisionResult result;
207 result.intersect = true;
208 result.willIntersect = true;
210 int edgeCountA = polygonA.size();
211 int edgeCountB = polygonB.size();
212 qreal minIntervalDistance = 1000000000;
213 QPointF translationAxis;
218 for (int k=0; k<edgeCountA;k++)
219 cout <<polygonA.at(k);
221 for (int k=0; k<edgeCountB;k++)
222 cout <<polygonB.at(k);
226 // Loop through all the edges of both polygons
227 for (int i=0;i<edgeCountA + edgeCountB;i++)
231 // Loop through polygon A
234 polygonA.at(i+1).x()-polygonA.at(i).x(),
235 polygonA.at(i+1).y()-polygonA.at(i).y());
238 polygonA.at(0).x()-polygonA.at(i).x(),
239 polygonA.at(0).y()-polygonA.at(i).y());
242 // Loop through polygon B
243 if (i < edgeCountA +edgeCountB -1 )
245 polygonB.at(i-edgeCountA+1).x() - polygonB.at(i-edgeCountA).x(),
246 polygonB.at(i-edgeCountA+1).y() - polygonB.at(i-edgeCountA).y());
249 polygonB.at(0).x() - polygonB.at(i-edgeCountA).x(),
250 polygonB.at(0).y() - polygonB.at(i-edgeCountA).y());
253 // ===== 1. Find if the polygons are currently intersecting =====
255 // Find the axis perpendicular to the current edge
257 Vector axis (-edge.y(), edge.x());
260 // Find the projection of the polygon on the current axis
262 qreal minA = 0; qreal minB = 0; qreal maxA = 0; qreal maxB = 0;
263 projectPolygon(axis, polygonA, minA, maxA);
264 projectPolygon(axis, polygonB, minB, maxB);
266 // Check if the polygon projections are currentlty intersecting
268 qreal d = intervalDistance(minA, maxA, minB, maxB);
269 if (d > 0) result.intersect = false;
271 // ===== 2. Now find if the polygons *will* intersect =====
274 // Project the velocity on the current axis
276 qreal velocityProjection = axis.dotProduct(velocity);
278 // Get the projection of polygon A during the movement
280 if (velocityProjection < 0)
281 minA += velocityProjection;
283 maxA += velocityProjection;
285 // Do the same test as above for the new projection
287 // d = intervalDistance(minA, maxA, minB, maxB);
288 //if (d > 0) result.willIntersect = false;
291 cout << "edge="<<edge<<" ";
292 cout <<"axis="<<axis<<" ";
293 cout <<"dA=("<<minA<<","<<maxA<<") dB=("<<minB<<","<<maxB<<")";
294 cout <<" d="<<d<<" ";
295 //cout <<"minD="<<minIntervalDistance<<" ";
296 cout <<"int="<<result.intersect<<" ";
297 //cout <<"wint="<<result.willIntersect<<" ";
298 //cout <<"velProj="<<velocityProjection<<" ";
302 if (result.intersect )// || result.willIntersect)
304 // Check if the current interval distance is the minimum one. If so
305 // store the interval distance and the current distance. This will
306 // be used to calculate the minimum translation vector
309 if (d < minIntervalDistance) {
310 minIntervalDistance = d;
311 //translationAxis = axis;
312 //cout << "tAxix="<<translationAxis<<endl;
314 //QPointF t = polygonA.Center - polygonB.Center;
315 //QPointF t = polygonA.at(0) - polygonB.at(0);
316 //if (dotProduct(t,translationAxis) < 0)
317 // translationAxis = -translationAxis;
322 // The minimum translation vector
323 // can be used to push the polygons appart.
325 if (result.willIntersect)
326 result.minTranslation =
327 translationAxis * minIntervalDistance;
332 /* The function can be used this way:
333 QPointF polygonATranslation = new QPointF();
338 PolygonCollisionResult r = PolygonCollision(polygonA, polygonB, velocity);
341 // Move the polygon by its velocity, then move
342 // the polygons appart using the Minimum Translation Vector
343 polygonATranslation = velocity + r.minTranslation;
345 // Just move the polygon by its velocity
346 polygonATranslation = velocity;
348 polygonA.Offset(polygonATranslation);